-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensubtitles.go
166 lines (139 loc) · 3.22 KB
/
opensubtitles.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
package opensubtitles
import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"github.com/PietroCarrara/sublime/pkg/guessit"
"github.com/PietroCarrara/sublime/pkg/sublime"
"github.com/oz/osdb"
"golang.org/x/text/language"
)
const name = "opensubtitles"
var langToISO639 = map[language.Tag][]string{
language.BrazilianPortuguese: {"pob", "pb"},
}
type OpenSubtitles struct {
c *osdb.Client
username string
password string
}
type OpenSubtitlesSubtitle struct {
s osdb.Subtitle
t *sublime.FileTarget
c *osdb.Client
}
func init() {
o := &OpenSubtitles{}
sublime.Services[o.GetName()] = o
}
func (o *OpenSubtitles) GetName() string {
return name
}
func (o *OpenSubtitles) GetCandidatesForFiles(files []*sublime.FileTarget, langs []language.Tag) <-chan sublime.SubtitleCandidate {
langList := make([]string, len(langs))
for i, lang := range langs {
if iso639, ok := langToISO639[lang]; ok {
langList[i] = iso639[0]
} else {
l, _ := lang.Base()
langList[i] = l.ISO3()
}
}
langsString := strings.Join(langList, ",")
channel := make(chan sublime.SubtitleCandidate)
go func() {
// Loop over every file
for _, file := range files {
args := map[string]string{
"query": file.GetName(),
"sublanguageid": langsString,
}
params := []interface{}{
o.c.Token,
[]map[string]string{args},
}
res, err := o.c.SearchSubtitles(¶ms)
if err != nil {
log.Printf("opensubtitles: %s\n", err)
// Go to the next file
continue
}
for _, sub := range res {
// Check if seasons match
if season := file.GetInfo().Season; season != 0 {
if sub.SeriesSeason != strconv.Itoa(season) {
continue
}
}
candidate := OpenSubtitlesSubtitle{
s: sub,
t: file,
c: o.c,
}
channel <- candidate
}
}
close(channel)
}()
return channel
}
func (o *OpenSubtitles) SetConfig(name, value string) error {
switch name {
case "username":
o.username = value
case "password":
o.password = value
default:
return fmt.Errorf(`option "%s" was not found`, name)
}
return nil
}
func (o *OpenSubtitles) Initialize() error {
c, err := osdb.NewClient()
if err != nil {
return err
}
o.c = c
return o.c.LogIn(o.username, o.password, "")
}
func (s OpenSubtitlesSubtitle) GetFormatExtension() string {
// We always download srt subtitles
return "srt"
}
func (s OpenSubtitlesSubtitle) GetService() string {
return name
}
func (s OpenSubtitlesSubtitle) GetRanking() float32 {
cnt, err := strconv.Atoi(s.s.SubDownloadsCnt)
if err != nil {
return 0
}
return float32(cnt)
}
func (s OpenSubtitlesSubtitle) GetFileTarget() *sublime.FileTarget {
return s.t
}
func (s OpenSubtitlesSubtitle) GetLang() language.Tag {
for lang, values := range langToISO639 {
for _, iso639 := range values {
if iso639 == s.s.ISO639 {
return lang
}
}
}
return language.Make(s.s.ISO639)
}
func (s OpenSubtitlesSubtitle) GetInfo() guessit.Information {
return guessit.Parse(s.s.SubFileName)
}
func (s OpenSubtitlesSubtitle) Open() (io.ReadCloser, error) {
url := "https://subs5.strem.io/en/download/subencoding-stremio-utf8/src-api/file/" + s.s.IDSubtitleFile
res, err := http.Get(url)
if err != nil {
return nil, err
}
return res.Body, nil
}