-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (32 loc) · 1.19 KB
/
main.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
import ssl
from time import sleep
from html.parser import HTMLParser
from http.client import HTTPSConnection
HOST = "www.southparkstudios.com"
conn = HTTPSConnection(HOST, context=ssl._create_unverified_context())
class LinkExtractor(HTMLParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__hrefs = set()
def handle_starttag(self, tag, attrs):
if tag == "a":
self.__hrefs.add(dict(attrs)["href"])
@property
def hrefs(self):
return self.__hrefs
def get_season_urls(cartmn: HTTPSConnection):
cartmn.request("GET", "/seasons/south-park")
parser = LinkExtractor()
resp = cartmn.getresponse()
parser.feed(str(resp.read()))
return (href for href in parser.hrefs if href.startswith("/seasons/south-park/"))
def get_episode_urls(cartmn: HTTPSConnection, season_url: str):
cartmn.request("GET", season_url)
parser = LinkExtractor()
resp = cartmn.getresponse()
parser.feed(str(resp.read()))
return (href for href in parser.hrefs if href.startswith("/episodes"))
for url in get_season_urls(conn):
sleep(1)
for episode_url in get_episode_urls(conn, url):
print(episode_url)