-
Notifications
You must be signed in to change notification settings - Fork 19
/
subtitle.go
269 lines (243 loc) · 7.62 KB
/
subtitle.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package osdb
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"os"
"path"
"sort"
"strconv"
"strings"
"golang.org/x/text/encoding"
)
// A Subtitle with its many OSDB attributes...
type Subtitle struct {
IDMovie string `xmlrpc:"IDMovie"`
IDMovieImdb string `xmlrpc:"IDMovieImdb"`
IDSubMovieFile string `xmlrpc:"IDSubMovieFile"`
IDSubtitle string `xmlrpc:"IDSubtitle"`
IDSubtitleFile string `xmlrpc:"IDSubtitleFile"`
ISO639 string `xmlrpc:"ISO639"`
LanguageName string `xmlrpc:"LanguageName"`
MatchedBy string `xmlrpc:"MatchedBy"`
MovieByteSize string `xmlrpc:"MovieByteSize"`
MovieFPS string `xmlrpc:"MovieFPS"`
MovieHash string `xmlrpc:"MovieHash"`
MovieImdbRating string `xmlrpc:"MovieImdbRating"`
MovieKind string `xmlrpc:"MovieKind"`
MovieName string `xmlrpc:"MovieName"`
MovieNameEng string `xmlrpc:"MovieNameEng"`
MovieReleaseName string `xmlrpc:"MovieReleaseName"`
MovieTimeMS string `xmlrpc:"MovieTimeMS"`
MovieYear string `xmlrpc:"MovieYear"`
MovieFileName string `xmlrpc:"MovieName"`
QueryNumber string `xmlrpc:"QueryNumber"`
SeriesEpisode string `xmlrpc:"SeriesEpisode"`
SeriesIMDBParent string `xmlrpc:"SeriesIMDBParent"`
SeriesSeason string `xmlrpc:"SeriesSeason"`
SubActualCD string `xmlrpc:"SubActualCD"`
SubAddDate string `xmlrpc:"SubAddDate"`
SubAuthorComment string `xmlrpc:"SubAuthorComment"`
SubAutoTranslation string `xmlrpc:"SubAutoTranslation"`
SubBad string `xmlrpc:"SubBad"`
SubComments string `xmlrpc:"SubComments"`
SubDownloadLink string `xmlrpc:"SubDownloadLink"`
SubDownloadsCnt string `xmlrpc:"SubDownloadsCnt"`
SubFeatured string `xmlrpc:"SubFeatured"`
SubFileName string `xmlrpc:"SubFileName"`
SubFormat string `xmlrpc:"SubFormat"`
SubHash string `xmlrpc:"SubHash"`
SubHD string `xmlrpc:"SubHD"`
SubHearingImpaired string `xmlrpc:"SubHearingImpaired"`
SubLanguageID string `xmlrpc:"SubLanguageID"`
SubRating string `xmlrpc:"SubRating"`
SubSize string `xmlrpc:"SubSize"`
SubSumCD string `xmlrpc:"SubSumCD"`
SubEncoding string `xmlrpc:"SubEncoding"`
SubForeignPartsOnly string `xmlrpc:"SubForeignPartsOnly"`
SubFromTrusted string `xmlrpc:"SubFromTrusted"`
SubtitlesLink string `xmlrpc:"SubtitlesLink"`
UserID string `xmlrpc:"UserID"`
UserNickName string `xmlrpc:"UserNickName"`
UserRank string `xmlrpc:"UserRank"`
ZipDownloadLink string `xmlrpc:"ZipDownloadLink"`
subFilePath string
}
func (s *Subtitle) toUploadParams() map[string]string {
return map[string]string{
"subhash": s.SubHash,
"subfilename": s.SubFileName,
"moviehash": s.MovieHash,
"moviebytesize": s.MovieByteSize,
"moviefilename": s.MovieFileName,
}
}
func (s *Subtitle) encodeFile() (string, error) {
fh, err := os.Open(s.subFilePath)
if err != nil {
return "", err
}
defer fh.Close()
dest := new(bytes.Buffer)
enc := base64.NewEncoder(base64.StdEncoding, dest)
gzWriter := gzip.NewWriter(enc)
_, err = io.Copy(gzWriter, fh)
if err != nil {
return "", err
}
gzWriter.Flush()
gzWriter.Close()
return dest.String(), nil
}
// Subtitles is a collection of subtitles.
type Subtitles []Subtitle
// ByDownloads implements sort interface for Subtitles, by download count.
type ByDownloads Subtitles
func (s ByDownloads) Len() int { return len(s) }
func (s ByDownloads) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByDownloads) Less(i, j int) bool {
iCnt, err := strconv.Atoi(s[i].SubDownloadsCnt)
if err != nil {
return false
}
jCnt, err := strconv.Atoi(s[j].SubDownloadsCnt)
if err != nil {
return true
}
return iCnt > jCnt
}
// Best finds the best subsitle in a Subtitles collection. Of course
// "best" is hardly an absolute concept: here, we just take the most
// downloaded file.
func (subs Subtitles) Best() *Subtitle {
if len(subs) > 0 {
sort.Sort(ByDownloads(subs))
return &subs[0]
}
return nil
}
// SubtitleFile contains file data as returned by OSDB's API, that is to
// say: gzip-ped and base64-encoded text.
type SubtitleFile struct {
ID string `xmlrpc:"idsubtitlefile"`
Data string `xmlrpc:"data"`
Encoding encoding.Encoding
reader io.ReadCloser
}
// Reader interface for SubtitleFile. Subtitle's contents are
// decompressed, and usually encoded to UTF-8: if encoding info is
// missing, no re-encoding is done.
func (sf *SubtitleFile) Reader() (r io.ReadCloser, err error) {
if sf.reader != nil {
return sf.reader, err
}
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(sf.Data))
gzReader, err := gzip.NewReader(dec)
if err != nil {
return nil, err
}
if sf.Encoding == nil {
sf.reader = gzReader
} else {
sf.reader = newCloseableReader(
sf.Encoding.NewDecoder().Reader(gzReader),
gzReader.Close,
)
}
return sf.reader, nil
}
// NewSubtitles builds a Subtitles from a movie path and a slice of
// subtitles paths. Intended to be used with for osdb.HasSubtitles() and
// osdb.UploadSubtitles().
func NewSubtitles(moviePath string, subPaths []string, langID string) (Subtitles, error) {
subs := Subtitles{}
for _, subPath := range subPaths {
sub, err := NewSubtitle(moviePath, subPath, langID)
if err != nil {
return nil, err
}
subs = append(subs, sub)
}
return subs, nil
}
// NewSubtitle builds a Subtitle struct.
func NewSubtitle(moviePath string, subPath string, langID string) (s Subtitle, err error) {
s.subFilePath = subPath
s.SubLanguageID = langID
s.SubFileName = path.Base(subPath)
// Subs are identified using md5 hashes... ¬¬
subIO, err := os.Open(subPath)
if err != nil {
return
}
defer subIO.Close()
h := md5.New()
_, err = io.Copy(h, subIO)
if err != nil {
return
}
s.SubHash = fmt.Sprintf("%x", h.Sum(nil))
// Movie filename, byte-size, & hash.
s.MovieFileName = path.Base(moviePath)
movieIO, err := os.Open(moviePath)
if err != nil {
return
}
defer movieIO.Close()
stat, err := movieIO.Stat()
if err != nil {
return
}
s.MovieByteSize = strconv.FormatInt(stat.Size(), 10)
movieHash, err := HashFile(movieIO)
if err != nil {
return
}
s.MovieHash = fmt.Sprintf("%x", movieHash)
return
}
// Serialize Subtitle to OSDB's XMLRPC params when trying to upload.
func (subs *Subtitles) toTryUploadParams() (map[string]interface{}, error) {
subMap := map[string]interface{}{}
for i, s := range *subs {
key := "cd" + strconv.Itoa(i+1) // keys are cd1, cd2, ...
subMap[key] = s.toUploadParams()
}
return subMap, nil
}
// Serialize Subtitle to OSDB's XMLRPC params when uploading.
func (subs *Subtitles) toUploadParams() (map[string]interface{}, error) {
langID := (*subs)[0].SubLanguageID
params := map[string]interface{}{}
params["baseinfo"] = map[string]string{
"sublanguageid": langID,
// FIXME add "idmovieimdb"
}
for i, s := range *subs {
key := "cd" + strconv.Itoa(i+1) // keys are cd1, cd2, ...
subParam := s.toUploadParams()
encoded, err := s.encodeFile()
if err != nil {
return nil, err
}
subParam["subcontent"] = encoded
params[key] = subParam
}
return params, nil
}
// Implement io.ReadCloser by wrapping io.Reader
type closeableReader struct {
io.Reader
close func() error
}
// Close the reader by calling a preset close function
func (c *closeableReader) Close() error {
return c.close()
}
// Create a ReadCloser which will read from r and call close() upon closing
func newCloseableReader(r io.Reader, close func() error) io.ReadCloser {
return &closeableReader{r, close}
}