This repository has been archived by the owner on Oct 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahsd.py
162 lines (128 loc) · 4.54 KB
/
yahsd.py
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
import os
import sys
import time
import argparse
import itertools
import collections
import html.parser
import urllib.parse
import urllib.request
class HorribleSubsShow:
BASE_URL = "https://horriblesubs.info/api.php"
HEADERS = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0",
}
def __init__(self, showid: int):
self.showid = showid
def get(self, page: int = 0):
timestamp = int(time.time() * 1000)
params = [
("method", "getshows"),
("type", "show"),
("showid", self.showid),
("_", timestamp),
]
if page != 0:
params.append(("nextid", page),)
query_string = urllib.parse.urlencode(params)
url = self.BASE_URL + "?" + query_string
req = urllib.request.Request(url, headers=self.HEADERS)
with urllib.request.urlopen(req) as response:
html = response.read().decode()
return html
def get_first(self):
yield self.get(page=0)
def get_all(self):
for page in itertools.count():
html = self.get(page=page)
if html == "DONE":
break
yield html
class EpisodeListParser(html.parser.HTMLParser):
def __init__(self):
self.episodes = {}
self.show_name = None
self.current_episode = None
self.resolution = None
self.data_count = 0
super().__init__()
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
classes = attrs.get("class", "").split()
if tag == "div" and "rls-info-container" in classes:
self.current_episode = attrs["id"]
self.episodes[self.current_episode] = {}
return
if tag == "div" and "rls-link" in classes:
assert attrs["id"].startswith(self.current_episode)
self.resolution = attrs["id"].split("-")[1]
self.episodes[self.current_episode][self.resolution] = {}
return
if tag == "a" and attrs.get("title") == "Magnet Link":
self.episodes[self.current_episode][self.resolution]["magnet"] = attrs[
"href"
]
return
if tag == "a" and attrs.get("title") == "Torrent Link":
self.episodes[self.current_episode][self.resolution]["torrent"] = attrs[
"href"
]
return
def handle_endtag(self, tag):
...
def handle_data(self, data):
if self.data_count == 1:
self.show_name = data.strip()
self.data_count += 1
class ArgParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, description="Process some integers.", **kwargs)
self.add_argument(
"show_ids",
metavar="ShowID(s)",
type=int,
nargs="+",
help="HorribleSubs show id",
)
self.add_argument(
"--all",
dest="get",
action="store_const",
const=lambda show: show.get_all(),
default=lambda show: show.get_first(),
help="sum the integers (default: find the max)",
)
class YahsDownloader:
@classmethod
def run(cls):
args = ArgParser().parse_args()
shows = collections.defaultdict(dict)
for showid in args.show_ids:
show = HorribleSubsShow(showid=showid)
for body in args.get(show):
parser = EpisodeListParser()
parser.feed(body)
shows[parser.show_name].update(parser.episodes)
cls.output(shows)
@classmethod
def output(cls, shows: dict):
for show in shows:
for episode in shows[show]:
for resolution in shows[show][episode]:
for medium, url in shows[show][episode][resolution].items():
sys.stdout.write(
cls.fmt(show, episode, resolution, medium, url)
)
@staticmethod
def fmt(show_name, episode, resolution, medium, url):
def bold(string):
start_bold = "\033[1m"
end = "\033[0m"
return f"{start_bold}{string}{end}"
if sys.stdout.isatty() and os.getenv("NO_COLOR") is None:
show_name = bold(show_name)
episode = bold(episode)
resolution = bold(resolution)
return "\t".join([show_name, episode, medium, resolution, url]) + "\n"
if __name__ == "__main__":
YahsDownloader.run()