-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_top250_to_imdb_list.py
executable file
·174 lines (154 loc) · 5.45 KB
/
parse_top250_to_imdb_list.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
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
import re
import sys
import requests
from datetime import datetime
from html.parser import HTMLParser
from typing import List, Optional, Tuple
class WrongPageDateException(Exception):
pass
def get_href_value(attrs: List[Tuple[str, str]]) -> str:
href_value = ""
for attr in attrs:
if attr[0] == "href":
href_value = attr[1]
break
return href_value
class ParseInfo250(HTMLParser):
def __init__(self, year: int, month: int, day: Optional[int] = None):
super().__init__()
self.titles = []
self.year = year
self.month = month
self.day = day
self.searching_td = False
self.searching_movie_link = False
self.searching_date_link = False
self.searching_span = False
self.movie_id = ""
self.next_date_candidate = ""
self.next_date = None
def handle_starttag(self, tag, attrs):
if tag == "td":
self.searching_td = True
date_href_pattern = re.compile(r"/charts/\?(\d{4}/\d{2}/\d{2})")
movie_href_pattern = re.compile(r"/movie/\?(\w+)")
href_value = get_href_value(attrs)
movie_href_match = movie_href_pattern.match(href_value)
date_href_match = date_href_pattern.match(href_value)
if tag == "a":
if movie_href_match:
self.movie_id = movie_href_match.group(1)
self.searching_movie_link = True
if date_href_match:
self.next_date_candidate = date_href_match.group(1)
self.searching_date_link = True
if tag == "span":
self.searching_span = True
def handle_endtag(self, tag):
if tag == "td":
self.searching_td = False
if tag == "a":
self.searching_movie_link = False
self.searching_date_link = False
if tag == "span":
self.searching_span = False
def handle_data(self, data):
content = str(data).strip()
if self.searching_td:
if self.searching_date_link:
next_page_pattern = re.compile(r"→")
if next_page_pattern.match(content):
date_parts = [
int(part) for part in self.next_date_candidate.split("/")
]
self.next_date = datetime(
date_parts[0], date_parts[1], date_parts[2]
)
if self.searching_movie_link and self.searching_span:
title_pattern = re.compile(r"(.*)(\([\d]{4}\))")
title_match = title_pattern.match(content)
if title_match:
title = title_match.groups()[0].strip()
if title:
date = f"{str(self.year)}-{str(self.month)}-{str(self.day)}"
self.titles.append(
[
f"tt{self.movie_id}",
date,
date,
"",
title,
f"https://www.imdb.com/title/{self.movie_id}",
"movie",
"",
"",
"",
"",
"",
"",
"",
]
)
def get_list_page_html(year: int, month: int, day: Optional[int] = None):
url = "http://top250.info/charts/?"
url = f"{url}{year}"
month_string = f"0{month}" if month < 10 else month
url = f"{url}/{month_string}"
if day:
day_string = f"0{day}" if day < 10 else day
url = f"{url}/{day_string}"
resp = requests.get(url)
response = resp.content.decode("utf8")
clean_html = re.sub(r"[\n\r\t]", "", response)
return clean_html
try:
date_pieces = sys.argv[1].split("-")
year = int(date_pieces[0])
month = int(date_pieces[1])
day = int(date_pieces[2])
except:
print(
"WARNING: no date or wrong date format provided, should be in the form: 'YYYY-MM-DD'. Will parse from 1996-04-26."
)
year = 1996
month = 4
day = 26
start_date = datetime(year=year, month=month, day=day)
today = datetime.now()
end_year = today.year
end_month = today.month
end_day = today.day
max_days = (today - start_date).days
date = start_date
date_counter = 0
start_row = 0
while True:
list_page_html = get_list_page_html(year=date.year, month=date.month, day=date.day)
parser = ParseInfo250(year=date.year, month=date.month, day=date.day)
parser.feed(list_page_html)
titles = parser.titles
title_count = len(titles)
if title_count > 0:
if start_row == 0:
print(
"Position,Const,Created,Modified,Description,Title,URL,"
+ "Title Type,IMDb Rating,Runtime (mins),Year,Genres,Num Votes,"
+ "Release Date,Directors"
)
start_row += 1
print(
*[
f"{start_row+index}," + ",".join(titles[index])
for index in range(title_count)
],
sep="\n",
)
start_row += title_count
if not parser.next_date:
break
else:
date = parser.next_date
date_counter += 1
if date_counter > max_days:
break