-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddicted.go
274 lines (248 loc) · 7.3 KB
/
addicted.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
270
271
272
273
274
package addicted
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/jpillora/scraper/scraper"
"golang.org/x/net/publicsuffix"
"gopkg.in/xmlpath.v2"
)
var (
baseURL = "http://www.addic7ed.com/"
reDownloadCount = regexp.MustCompile(`(\d+) Downloads`)
xpathRelease = xmlpath.MustCompile("//div[@id=\"container95m\"]//td[@class=\"NewsTitle\"]")
xpathLanguageFromRelease = xmlpath.MustCompile("../..//td[@class=\"language\"]")
xpathDownloadFromLanguage = xmlpath.MustCompile("..//a[@class=\"buttonDownload\"]/@href")
xpathDownloadCountFromLanguage = xmlpath.MustCompile("../following-sibling::tr[1]/td[1]")
xpathCheckSubtilePage = xmlpath.MustCompile("//div[@id=\"container\"]")
xpathCheckLogged = xmlpath.MustCompile("//a[@href=\"/logout.php\"]")
releaseRe = regexp.MustCompile(`Version ([-\(\)\.\w]+),`)
//ErrNoCreditial returned when attempt to login without creditial set
ErrNoCreditial = errors.New("no creditial provided")
//ErrInvalidCredential returned when login failed
ErrInvalidCredential = errors.New("invalid creditial")
//ErrEpisodeNotFound returned when try to find subtitles for an unknow episode or season or show
ErrEpisodeNotFound = errors.New("episode not found")
//ErrUnexpectedContent returned when addic7ed's website seem to have change
ErrUnexpectedContent = errors.New("unexpected content")
// ErrDownloadLimit retuned when download limit by day exceeded
ErrDownloadLimit = errors.New("download count exceeded")
)
// Subtitle represents a subtitle
type Subtitle struct {
Language string
Release string
Download int
Link string
conn io.ReadCloser
client *Client
}
// Read subtitle's content
func (sub *Subtitle) Read(p []byte) (int, error) {
if sub.conn == nil {
resp, err := sub.client.Get(fmt.Sprintf("%s%s", baseURL, sub.Link[1:]), true)
if err != nil {
return 0, err
}
if resp.Request.URL.Path == "/downloadexceeded.php" {
return 0, ErrDownloadLimit
}
sub.conn = resp.Body
}
return sub.conn.Read(p)
}
// Close close subtitle's connection
func (sub *Subtitle) Close() error {
if sub.conn != nil {
return sub.conn.Close()
}
return nil
}
// ByDownloads helper for sorting
type ByDownloads []Subtitle
func (a ByDownloads) Len() int { return len(a) }
func (a ByDownloads) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDownloads) Less(i, j int) bool { return a[i].Download < a[j].Download }
// Subtitles helper for filter subtitle
type Subtitles []Subtitle
// FilterByLang filter by language
func (a Subtitles) FilterByLang(lang string) Subtitles {
subs := []Subtitle{}
for _, sub := range a {
if sub.Language == lang {
subs = append(subs, sub)
}
}
return Subtitles(subs)
}
// Client store information for interaction with addic7ed as logged user
type Client struct {
user string
passwd string
shows map[string]string
httpClient *http.Client
isConnected bool
showScraper *scraper.Endpoint
}
// NewWithAuth returns new addicted's client with autentification
func NewWithAuth(user, passwd string) (*Client, error) {
c, err := New()
if err != nil {
return nil, err
}
c.user = user
c.passwd = passwd
return c, nil
}
// New returns new addicted's client
func New() (*Client, error) {
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&options)
if err != nil {
return nil, err
}
httpClient := http.Client{Jar: jar}
e := &scraper.Endpoint{
Name: "showScraper",
Method: "GET",
List: "select > option",
URL: baseURL,
Headers: map[string]string{
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2988.133 Safari/537.36",
},
Result: map[string]scraper.Extractors{
"id": {scraper.MustExtractor("@value")},
"name": {scraper.MustExtractor("/(.*)/")},
},
Debug: false,
}
return &Client{
httpClient: &httpClient,
showScraper: e,
}, nil
}
// SetCredential set user password for addicted client
func (c *Client) SetCredential(user, password string) {
c.user = user
c.passwd = password
}
// Get wrapper function for http.Get which takes care of session's cookies
func (c *Client) Get(url string, auth bool) (resp *http.Response, err error) {
if auth && !c.isConnected {
if c.user != "" && c.passwd != "" {
if err := c.connect(); err != nil {
return nil, err
}
c.isConnected = true
}
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Referer", baseURL)
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6")
return c.httpClient.Do(req)
}
func (c *Client) connect() error {
values := url.Values{}
values.Add("username", c.user)
values.Add("password", c.passwd)
values.Add("url", "")
values.Add("Submit", "Log in")
resp, err := c.httpClient.PostForm(baseURL+"dologin.php", values)
if err != nil {
return err
}
root, err := xmlpath.ParseHTML(resp.Body)
if err != nil {
return ErrUnexpectedContent
}
if !xpathCheckLogged.Exists(root) {
return ErrInvalidCredential
}
return nil
}
// GetTvShows returns a map of show's title as keys and ids as values
func (c *Client) GetTvShows() (map[string]string, error) {
if len(c.shows) > 0 {
return c.shows, nil
}
var err error
// Parse the page
res, err := c.showScraper.Execute(nil)
if err != nil {
return nil, err
}
c.shows = map[string]string{}
for _, r := range res {
c.shows[r["name"]] = r["id"]
}
return c.shows, nil
}
// GetSubtitles returns available subtitles
func (c *Client) GetSubtitles(showID string, season, episode int) (Subtitles, error) {
s := strconv.Itoa(season)
e := strconv.Itoa(episode)
return c.parseSubtitle(showID, s, e)
}
func (c *Client) parseSubtitle(showID, s, e string) (Subtitles, error) {
resp, err := http.Get(fmt.Sprintf("%vre_episode.php?ep=%s-%sx%s", baseURL, showID, s, e))
if err != nil {
return nil, err
}
defer resp.Body.Close()
root, err := xmlpath.ParseHTML(resp.Body)
if err != nil {
return nil, ErrUnexpectedContent
}
if !xpathCheckSubtilePage.Exists(root) {
return nil, ErrEpisodeNotFound
}
sub := []Subtitle{}
iter := xpathRelease.Iter(root)
for iter.Next() {
releaseStr := iter.Node().String()
releaseM := releaseRe.FindStringSubmatch(releaseStr)
release := ""
if len(releaseM) > 1 {
release = releaseM[1]
}
iterlang := xpathLanguageFromRelease.Iter(iter.Node())
for iterlang.Next() {
download, ok := xpathDownloadFromLanguage.String(iterlang.Node())
if !ok {
return nil, ErrUnexpectedContent
}
downloadText, ok := xpathDownloadCountFromLanguage.String(iterlang.Node())
if !ok {
return nil, ErrUnexpectedContent
}
refound := reDownloadCount.FindAllStringSubmatch(downloadText, 1)
if len(refound) == 0 || len(refound[0]) == 0 {
return nil, ErrUnexpectedContent
}
downloadcount, err := strconv.Atoi(refound[0][1])
if err != nil {
return nil, ErrUnexpectedContent
}
subtitle := Subtitle{
Language: strings.ToLower(strings.TrimSpace(iterlang.Node().String())),
Download: downloadcount,
Link: download,
Release: release,
client: c,
}
sub = append(sub, subtitle)
}
}
return Subtitles(sub), nil
}