movies.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package movies
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. )
  7. type Movie struct {
  8. Title string
  9. Director string
  10. Poster string
  11. Rating *Rating
  12. IMDBID string
  13. Country string
  14. Year string
  15. Runtime string
  16. Files []string
  17. OMDB OMDBMovie
  18. }
  19. type Rating struct {
  20. Note float64
  21. Votes int64
  22. }
  23. type OMDBMovie struct {
  24. Title string
  25. Year string
  26. Rated string
  27. Released string
  28. Runtime string
  29. Genre string
  30. Director string
  31. Writer string
  32. Actors string
  33. Plot string
  34. Language string
  35. Country string
  36. Awards string
  37. Poster string
  38. Ratings []OMDBRating
  39. Metascore string
  40. IMDBRating string `json: "imdbRating"`
  41. IMDBVotes string `json: "imdbVotes"`
  42. IMDBID string `json: "imdbID"`
  43. Type string
  44. DVD string
  45. BoxOffice string
  46. Production string
  47. Website string
  48. Response string
  49. }
  50. type OMDBRating struct {
  51. Source, Value string
  52. }
  53. func Unmarshal(b []byte) (*Movie, error) {
  54. var omdb OMDBMovie
  55. if err := json.Unmarshal(b, &omdb); err != nil {
  56. return nil, err
  57. }
  58. m := &Movie{
  59. OMDB: omdb,
  60. }
  61. if err := m.FillFromOMDB(); err != nil {
  62. return nil, err
  63. }
  64. return m, nil
  65. }
  66. func (m *Movie) FillFromOMDB() error {
  67. if m.Title == "" {
  68. m.Title = m.OMDB.Title
  69. }
  70. if m.Director == "" {
  71. m.Director = m.OMDB.Director
  72. }
  73. if m.Poster == "" {
  74. m.Poster = m.OMDB.Poster
  75. }
  76. if m.IMDBID == "" {
  77. m.IMDBID = m.OMDB.IMDBID
  78. }
  79. if m.Country == "" {
  80. m.Country = m.OMDB.Country
  81. }
  82. if m.Year == "" {
  83. m.Year = m.OMDB.Year
  84. }
  85. if m.Runtime == "" {
  86. m.Runtime = m.OMDB.Runtime
  87. }
  88. if m.Rating == nil {
  89. m.Rating = &Rating{}
  90. if note, err := strconv.ParseFloat(m.OMDB.IMDBRating, 64); err == nil {
  91. m.Rating.Note = note
  92. }
  93. if votes, err := strconv.ParseInt(strings.ReplaceAll(m.OMDB.IMDBVotes, ",", ""), 10, 64); err == nil {
  94. m.Rating.Votes = votes
  95. }
  96. }
  97. return nil
  98. }