forked from mx-psi/fa-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fa-scrapper.py
executable file
·214 lines (167 loc) · 6.29 KB
/
fa-scrapper.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
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
#! /usr/bin/python3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import argparse
import csv
import sys
from datetime import datetime
import platform
import locale
import requests
import bs4
# FilmAffinity root URL
FA_ROOT_URL = "https://www.filmaffinity.com/{lang}/"
def set_locale(lang):
"""Attempts to set locale."""
if platform.system() in {"Linux", "Darwin"}:
loc = "es_ES.utf8" if lang == "es" else "en_US.utf8"
elif platform.system() == "Windows":
loc = "es-ES" if lang == "es" else "en-US"
else:
raise locale.Error()
locale.setlocale(locale.LC_ALL, loc)
def get_date(tag, lang):
"""Gets date from tag (format YYYY-MM-DD)"""
if lang == "es":
date_str = tag.string[len("Votada el día: ") :].strip()
fecha = datetime.strptime(date_str, "%d de %B de %Y").date()
else:
date_str = tag.string[len("Rated on ") :].strip()
fecha = datetime.strptime(date_str, "%B %d, %Y").date()
return fecha.strftime("%Y-%m-%d")
def get_directors(tag):
"""Gets directors from a film"""
def sanitize_director_tag(d):
"""Sanitizes director tag into director name."""
director = d.a["title"]
return director[:-10] if director.endswith("(Creator)") else director
return ", ".join(
sanitize_director_tag(d)
for d in tag.find_all(class_="mc-director")[0].find_all(class_="nb")
)
def is_film(tag, lang):
"""Checks if given tag is a film"""
title = tag.find_all(class_="mc-title")[0].a.string.strip()
if lang == "es":
skip = ["(Serie de TV)", "(Miniserie de TV)", "(TV)", "(C)"]
else:
skip = ["(TV Series)", "(TV Miniseries)", "(TV)", "(S)"]
return not any(title.endswith(suffix) for suffix in skip)
def pages_from(template):
"""Yields pages from a given section until one of them fails."""
eof = False
n = 1
while not eof:
request = requests.get(template.format(n))
request.encoding = "utf-8"
yield bs4.BeautifulSoup(request.text, "lxml")
eof = request.status_code != 200
if not eof:
print("Página {n}".format(n=n), end="\r")
else:
print("Página {n}. Download complete!".format(n=n - 1))
n += 1
def get_profile_data(user_id, lang):
"""Yields films rated by user given user id"""
FA = (FA_ROOT_URL + "userratings.php?user_id={id}&p={{}}&orderby=4").format(
lang=lang, id=user_id
)
for page in pages_from(FA):
tags = page.find_all(class_=["user-ratings-header", "user-ratings-movie"])
cur_date = None
for tag in tags:
if tag["class"] == ["user-ratings-header"]:
cur_date = get_date(tag, lang)
elif is_film(tag, lang):
title = tag.find_all(class_="mc-title")[0].a
yield {
"Title": title.string.strip(),
"Year": title.next_sibling.strip()[1:-1],
"Directors": get_directors(tag),
"WatchedDate": cur_date,
"Rating": int(tag.find_all(class_="ur-mr-rat")[0].string) / 2,
"Rating10": tag.find_all(class_="ur-mr-rat")[0].string,
}
def get_list_data(user_id, list_id, lang):
"""Yields films from list given list id"""
FA = (
FA_ROOT_URL + "userlist.php?user_id={user_id}&list_id={list_id}&page={{}}"
).format(lang=lang, user_id=user_id, list_id=list_id)
for page in pages_from(FA):
tags = page.find_all(class_=["movie-wrapper"])
for tag in tags:
title = tag.find_all(class_="mc-title")[0].a
yield {
"Title": title.string.strip(),
"Year": title.next_sibling.strip()[1:-1],
"Directors": get_directors(tag),
}
def save_to_csv(films, filename):
"""Saves films in a csv file"""
with open(filename, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(list(next(films)))
for film in films:
writer.writerow(list(film.values()))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generates Letterboxd-compatible csv from Filmaffinity user data."
)
parser.add_argument("id", help="User id")
parser.add_argument("--list", help="List id", metavar="LIST")
parser.add_argument("--csv", nargs=1, help="Name of export FILE", metavar="FILE")
parser.add_argument(
"--lang",
nargs=1,
help="Language for exporting",
metavar="LANG",
default=["en"],
choices={"es", "en"},
)
args = parser.parse_args()
if args.csv:
export_file = args.csv[0]
elif args.list:
export_file = "filmAffinity_{lang}_{id}_list_{list_id}.csv".format(
id=args.id, lang=args.lang[0], list_id=args.list
)
else:
export_file = "filmAffinity_{lang}_{id}.csv".format(
id=args.id, lang=args.lang[0]
)
try:
set_locale(args.lang[0])
except locale.Error:
print(
"Could not set locale for '{lang}' and UTF-8 encoding.".format(
lang=args.lang[0]
)
)
manual_locale = input("locale (empty for default): ").strip()
if manual_locale:
try:
locale.setlocale(locale.LC_ALL, manual_locale)
except locale.Error as e:
print(e)
sys.exit()
try:
if args.list:
data = get_list_data(args.id, args.list, args.lang[0])
else:
data = get_profile_data(args.id, args.lang[0])
except ValueError as v:
print("Error:", v)
sys.exit()
save_to_csv(data, export_file)