-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdb.go
217 lines (190 loc) · 4.81 KB
/
imdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import "net/http"
import "compress/gzip"
import "io"
import "os"
import "path"
import "fmt"
import "bufio"
import "strconv"
import "strings"
import "log"
import (
"github.com/boltdb/bolt"
)
func CreateIMDB(c *Config) *IMDB{
i := IMDB{conf: c}
i.SetupDB()
return &i
}
type IMDB struct {
conf *Config
db *bolt.DB
tx *bolt.Tx
}
/*
Server string `gorm:"index:pckg"`
Channel string `gorm:"index:pckg"`
Bot string `gorm:"index:pckg"`
Package string `gorm:"index:pckg"`
*/
type TitleBasic struct {
Tconst string `gorm:"index"`
TitleType string `gorm:"index:tt,mv,show"`
PrimaryTitle string `gorm:"index:pt,mv,show"`
OriginalTitle string
IsAdult string
StartYear int `gorm:"index:sy,mv"`
EndYear int
RuntimeMinutes int
Genres string
}
type TitleRating struct {
Tconst string `gorm:"index"`
AverageRating float64
NumVotes int
}
func (i *IMDB) GetIdForMovie(title string, year int) string {
v := ""
i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("imdbid"))
v = fmt.Sprintf("%s",b.Get([]byte(i.key("movie", year, title))))
return nil
})
return v
}
func (i *IMDB) GetIdForShow(title string) string {
v := ""
i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("imdbid"))
v = fmt.Sprintf("%s", b.Get([]byte(i.key("tvSeries", 0, title))))
return nil
})
return v
}
func (i *IMDB) GetRating(id string) (float64, int) {
v := 0.0
i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("rating"))
v,_ = strconv.ParseFloat(fmt.Sprintf("%s", b.Get([]byte(id))), 64)
return nil
})
return v, 0
}
func (i *IMDB) key(kind string, year int, title string) string {
if kind == "movie" {
return fmt.Sprintf("%s:%d:%s", kind, year, strings.ToLower(title))
} else {
return fmt.Sprintf("%s:%s", kind, strings.ToLower(title))
}
}
func (i *IMDB) SetupDB() {
fmt.Println("setting up db")
p := path.Join(i.conf.DBPath, ".imdb.bdb")
db, err := bolt.Open(p, 0600, nil)
if err != nil {
panic("failed to connect database")
}
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucketIfNotExists([]byte("imdbid"))
tx.CreateBucketIfNotExists([]byte("rating"))
return nil
})
i.db = db
fmt.Println("done setting up db")
}
func (i *IMDB) UpdateData() (suc bool) {
log.Print("Updating data")
suc = true
i.downloadData()
err := i.db.Update(func(tx *bolt.Tx) error {
tx.DeleteBucket([]byte("imdbid"))
tx.CreateBucketIfNotExists([]byte("imdbid"))
tx.DeleteBucket([]byte("rating"))
tx.CreateBucketIfNotExists([]byte("rating"))
i.tx = tx
i.readTSV(path.Join(i.conf.TempPath, "title.basics.tsv.gz"), handleTitleBasics)
i.readTSV(path.Join(i.conf.TempPath, "title.ratings.tsv.gz"), handleTitleRatings)
return nil
})
if err != nil {
panic(err)
}
log.Print("Done updating data")
return suc
}
func (i* IMDB) downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func (i *IMDB) downloadData() {
basePath := "https://datasets.imdbws.com/"
files := []string{"title.basics.tsv.gz", "title.ratings.tsv.gz"}
for _,f := range files {
targetFile := path.Join(i.conf.TempPath,f)
err := i.downloadFile(targetFile, basePath + f)
if err != nil {
panic(err)
}
}
}
func handleTitleBasics(imdbid *bolt.Bucket, ratings *bolt.Bucket,i *IMDB,vals []string) {
StartYear, _:= strconv.Atoi(vals[5])
imId := vals[0]
TitleType := vals[1]
if TitleType == "movie" || TitleType == "tvSeries" {
err := imdbid.Put([]byte(i.key(TitleType,StartYear, cleanTitle(vals[2]))), []byte(imId))
if err != nil {
panic(err)
}
}
}
func handleTitleRatings(imdbid *bolt.Bucket, ratings *bolt.Bucket, i *IMDB,vals []string) {
err := ratings.Put([]byte(vals[0]), []byte(vals[1]))
if err != nil {
panic(err)
}
}
func (i *IMDB) readTSV(file string, handle func(*bolt.Bucket, *bolt.Bucket,*IMDB, []string)) {
f, _ := os.Open(file)
reader, _ := gzip.NewReader(f)
scanner := bufio.NewScanner(reader)
first := true
for scanner.Scan() {
if first {
first = false
continue
}
imdbid := i.tx.Bucket([]byte("imdbid"))
ratings := i.tx.Bucket([]byte("rating"))
handle(imdbid, ratings, i, strings.Split(strings.Replace(scanner.Text(), "\\N", "", -1), "\t"))
}
}
// nned to export from releaseparser at some point
func cleanTitle(name string) string {
name = strings.Replace(name, ".", " ", -1)
name = strings.Replace(name, "_", " ", -1)
name = strings.Replace(name, "-", " ", -1)
name = strings.Trim(name, " ")
return name
}