decode.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package common
  2. import (
  3. "errors"
  4. "github.com/beevik/etree"
  5. PTN "github.com/middelink/go-parse-torrent-name"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. )
  13. func getImdbMovieXml(movieFilePath string) (string, error) {
  14. doc := etree.NewDocument()
  15. if err := doc.ReadFromFile(movieFilePath); err != nil {
  16. return "", err
  17. }
  18. for _, t := range doc.FindElements("//IMDB") {
  19. return t.Text(), nil
  20. }
  21. return "", CanNotFindIMDBID
  22. }
  23. func getImdbNfo(nfoFilePath string) (string, error) {
  24. doc := etree.NewDocument()
  25. if err := doc.ReadFromFile(nfoFilePath); err != nil {
  26. return "", err
  27. }
  28. for _, t := range doc.FindElements("//uniqueid[@type='Imdb']") {
  29. return t.Text(), nil
  30. }
  31. return "", CanNotFindIMDBID
  32. }
  33. func GetImdbId(dirPth string) (string ,error) {
  34. dir, err := ioutil.ReadDir(dirPth)
  35. if err != nil {
  36. return "", err
  37. }
  38. pathSep := string(os.PathSeparator)
  39. // 优先找 movie.xml 这个是 raddarr 下载的电影会存下来的,可以在 Metadata 设置 Emby
  40. var movieFilePath = ""
  41. // 这个是使用 tinyMediaManager 削刮器按 Kodi 来存储的
  42. var nfoFilePath = ""
  43. for _, fi := range dir {
  44. if fi.IsDir() == true {
  45. continue
  46. }
  47. upperName := strings.ToUpper(fi.Name())
  48. // 找 movie.xml
  49. if upperName == strings.ToUpper(metadataFileEmby) {
  50. movieFilePath = dirPth + pathSep + fi.Name()
  51. }
  52. // 找 *.nfo
  53. ok := strings.HasSuffix(fi.Name(), suffixNameNfo)
  54. if ok {
  55. nfoFilePath = dirPth + pathSep + fi.Name()
  56. }
  57. }
  58. // 根据找到的开始解析
  59. if movieFilePath == "" && nfoFilePath == "" {
  60. return "", NoMetadataFile
  61. }
  62. if movieFilePath != "" {
  63. outId, err := getImdbMovieXml(movieFilePath)
  64. if err != nil {
  65. GetLogger().Errorln("getImdbMovieXml error, move on:", err)
  66. } else {
  67. return outId, nil
  68. }
  69. }
  70. if nfoFilePath != "" {
  71. outId, err := getImdbNfo(nfoFilePath)
  72. if err != nil {
  73. return "", err
  74. } else {
  75. return outId, nil
  76. }
  77. }
  78. return "", CanNotFindIMDBID
  79. }
  80. //GetVideoInfo 从文件名推断视频文件的信息
  81. func GetVideoInfo(videoFileName string) (*PTN.TorrentInfo, error) {
  82. parse, err := PTN.Parse(filepath.Base(videoFileName))
  83. if err != nil {
  84. return nil, err
  85. }
  86. compile, err := regexp.Compile(regFixTitle2)
  87. if err != nil {
  88. return nil, err
  89. }
  90. match := compile.ReplaceAllString(parse.Title, "")
  91. match = strings.TrimRight(match, "")
  92. parse.Title = match
  93. return parse, nil
  94. }
  95. func GetNumber2Float(input string) (float32, error) {
  96. compile := regexp.MustCompile(regGetNumber)
  97. params := compile.FindStringSubmatch(input)
  98. if len(params) == 0 {
  99. return 0, errors.New("get number not match")
  100. }
  101. fNum, err := strconv.ParseFloat(params[0],32)
  102. if err != nil {
  103. return 0, errors.New("get number ParseFloat error")
  104. }
  105. return float32(fNum), nil
  106. }
  107. func GetNumber2int(input string) (int, error) {
  108. compile := regexp.MustCompile(regGetNumber)
  109. params := compile.FindStringSubmatch(input)
  110. if len(params) == 0 {
  111. return 0, errors.New("get number not match")
  112. }
  113. fNum, err := strconv.Atoi(params[0])
  114. if err != nil {
  115. return 0, errors.New("get number ParseFloat error")
  116. }
  117. return fNum, nil
  118. }
  119. const (
  120. metadataFileEmby = "movie.xml"
  121. suffixNameXml = ".xml"
  122. suffixNameNfo = ".nfo"
  123. // 去除特殊字符,仅仅之有中文
  124. regFixTitle = "[^\u4e00-\u9fa5a-zA-Z0-9\\s]"
  125. // 去除特殊字符,把特殊字符都写进去
  126. regFixTitle2 = "[`~!@#$%^&*()+-=|{}';'\\[\\].<>/?~!@#¥%……&*()——+|{}【】';”“’。、?]"
  127. // 获取数字
  128. regGetNumber = "(?:\\-)?\\d{1,}(?:\\.\\d{1,})?"
  129. )