-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
328 lines (271 loc) · 13.3 KB
/
user.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from bs4 import BeautifulSoup
from requests import get as requests_get, exceptions as requests_exceptions
from re import findall, compile, DOTALL
from concurrent.futures import ThreadPoolExecutor, as_completed
from itertools import chain
class PyBoxd():
class user():
def __init__(self) -> None:
self.username = None
self.films = 0
self.thisYear = 0
self.following = 0
self.followers = 0
self.favoriteFilms = []
self.mainResponse = None
self.mainSoup = None
self.filmsResponse = None
self.filmsSoup = None
self.watchedFilms = []
self.watchlist = []
self.userNetwork = {}
self.isPatron = False
self.isPro = False
self.userLists = 0
self.userImage = None
def __str__(self):
userInfo = f'Username: {self.username}\nFilms: {self.films}\nThis Year:{self.thisYear}\nLists: {self.userLists}\nFollowing: {self.following}\nFollowers: {self.followers}\nFavorite Films: {self.favoriteFilms}\nPatron: {self.isPatron}\nPro: {self.isPro}'
return userInfo
def set_username(self, username) -> None:
try:
self.mainResponse = requests_get(f'https://letterboxd.com/{username}/').text
self.mainSoup = BeautifulSoup(self.mainResponse, 'html.parser')
self.username = username
except requests_exceptions.RequestException as e:
print(f"Failed to retrieve data: {e}")
return
def get_profile_stats(self) -> None:
self.profileStats = PyBoxd.scrape_profile_stats(soup = self.mainSoup)
self.films = self.profileStats[0]['Films']
self.thisYear = self.profileStats[0]['This year']
self.following = self.profileStats[0]['Following']
self.followers = self.profileStats[0]['Followers']
self.userLists = self.profileStats[0]['Lists']
self.favoriteFilms = self.profileStats[1]
self.isPatron = self.profileStats[2][0]
self.isPro = self.profileStats[2][1]
def get_user_watched_films(self) -> None:
self.filmsResponse = requests_get(f'https://letterboxd.com/{self.username}/films/').text
self.filmsSoup = BeautifulSoup(self.filmsResponse, 'html.parser')
self.watchedFilms = PyBoxd.scrape_watched_films(user = self.username, soup = self.filmsSoup)
def get_user_watchlist(self) -> None:
self.filmsResponse = requests_get(f'https://letterboxd.com/{self.username}/watchlist/').text
self.filmsSoup = BeautifulSoup(self.filmsResponse, 'html.parser')
self.watchlist = PyBoxd.scrape_watchlist(user = self.username, soup = self.filmsSoup)
def get_user_network(self) -> None:
self.networkFollowingResponse = requests_get(f'https://letterboxd.com/{self.username}/following/').text
self.networkFollowerResponse = requests_get(f'https://letterboxd.com/{self.username}/followers/').text
self.networkFollowingSoup = BeautifulSoup(self.networkFollowingResponse, 'html.parser')
self.networkFollowerSoup = BeautifulSoup(self.networkFollowerResponse, 'html.parser')
self.userNetwork = PyBoxd.scrape_user_network(user = self.username, soup = self.networkFollowingSoup, soup2 = self.networkFollowerSoup)
def get_user_bio(self) -> None:
self.userBio = PyBoxd.scrapeBio(soup = self.mainSoup)
def get_user_image(self) -> None:
self.userImage = findall(r'<img\s+[^>]*src="([^"]+)"', str(self.mainSoup.find('span', class_='avatar -a110 -large')))
class user_diary():
def __init__(self, user:object) -> None:
self.user = user.username
self.userDiary = None
def __str__(self) -> str:
userDriaryInfo = f'Username: {self.user}'
return userDriaryInfo
def get_user_diary(self) -> None:
self.userDiaryResponse = requests_get(f'https://letterboxd.com/{self.user}/films/diary/').text
self.userDiarySoup = BeautifulSoup(self.userDiaryResponse, 'html.parser')
self.userDiary = PyBoxd.scrape_user_diary(user = self.user, soup = self.userDiarySoup)
@staticmethod
def scrape_profile_stats(soup:BeautifulSoup) -> list:
base_dict = {'Films': 0, 'This year': 0, 'Following': 0, 'Followers': 0, 'Lists': 0}
stats = findall(r'<span class="value">([\d,]+)</span>', str(soup))
defintion = findall(r'<span class="definition">([\w\s]+)</span>', str(soup))
stats_dict = dict(zip(defintion, stats))
base_dict.update(stats_dict)
favorite_films = findall(r'data-film-slug="([^"]+)"', str(soup.find('section', {'id': 'favourites'})))
badges = []
badges.append([True if soup.find('span', class_='badge -patron') else False])
badges.append([True if soup.find('span', class_='badge -pro') else False])
return [base_dict, favorite_films, badges]
@staticmethod
def process_page(user:str, i:int, page_type:str='films') -> list:
try:
response = requests_get(f'https://letterboxd.com/{user}/{page_type}/page/{i}/')
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
return findall(r'data-film-slug="([^"]+)"', str(soup))
except requests_exceptions as e:
print(f"Error fetching page {i}: {e}")
return []
@staticmethod
def scrape_watched_films(user: str, soup: BeautifulSoup) -> list:
pages = findall(r'films/page/(\d+)/', str(soup))
if len(pages) == 0:
pages = ['1']
last_page = max([int(page) for page in pages])
with ThreadPoolExecutor() as executor:
futures = [executor.submit(PyBoxd.process_page, user, i, page_type="films") for i in range(1, last_page + 1)]
film_slugs = []
for future in as_completed(futures):
film_slugs.append(future.result())
return list(chain.from_iterable(film_slugs))
@staticmethod
def scrape_watchlist(user:str, soup:BeautifulSoup) -> list:
pages = findall(r'watchlist/page/(\d+)/', str(soup))
if len(pages) == 0:
pages = ['1']
last_page = max([int(page) for page in pages])
with ThreadPoolExecutor() as executor:
futures = [executor.submit(PyBoxd.process_page, user, i, page_type="watchlist") for i in range(1, last_page + 1)]
film_slugs = []
for future in as_completed(futures):
film_slugs.append(future.result())
return list(chain.from_iterable(film_slugs))
@staticmethod
def scrape_user_network(user:str,soup:BeautifulSoup, soup2:BeautifulSoup) -> dict:
dataFollowing = []
dataFollowers = []
pages = findall(r'following/page/(\d+)/', str(soup))
if len(pages) == 0:
pages = ['1']
last_page = max([int(page) for page in pages])
for i in range(1, last_page + 1):
response = requests_get(f'https://letterboxd.com/{user}/following/page/{i}/').text
soup = BeautifulSoup(response, 'html.parser')
pattern = compile(r'href="/([A-Za-z0-9_-]+)/"')
tags = soup.find_all('a', class_='name')
hrefs = [href for tag in tags for href in findall(pattern, str(tag))]
dataFollowing.extend(hrefs)
pages = findall(r'followers/page/(\d+)/', str(soup2))
if len(pages) == 0:
pages = ['1']
last_page = max([int(page) for page in pages])
for i in range(1, last_page + 1):
response = requests_get(f'https://letterboxd.com/{user}/followers/page/{i}/').text
soup = BeautifulSoup(response, 'html.parser')
pattern = compile(r'href="/([A-Za-z0-9_-]+)/"')
tags = soup.find_all('a', class_='name')
hrefs = [href for tag in tags for href in findall(pattern, str(tag))]
dataFollowers.extend(hrefs)
return {"following": dataFollowing, "followers": dataFollowers}
@staticmethod
def scrapeBio(soup:BeautifulSoup) -> str:
bio = soup.find('div', class_ = "collapsible-text body-text -small js-bio-content")
bio = bio.find_all('p')
bio = [x.text for x in bio]
return bio
@staticmethod
def process_diary_page(user, i):
try:
response = requests_get(f'https://letterboxd.com/{user}/films/diary/page/{i}/')
response.raise_for_status() # Raise an error for bad status codes
soup = BeautifulSoup(response.text, 'html.parser')
return {
"dates": PyBoxd.find_dates(soup),
"film_slugs": PyBoxd.find_film_slugs(soup),
"ratings": PyBoxd.find_ratings(soup),
"likes": PyBoxd.find_likes(soup),
"rewatches": PyBoxd.find_rewatches(soup),
"reviews": PyBoxd.find_reviews(soup)
}
except requests_exceptions as e:
print(f"Error fetching page {i}: {e}")
return {
"dates": [], "film_slugs": [], "ratings": [],
"likes": [], "rewatches": [], "reviews": []
}
@staticmethod
def scrape_user_diary(user:str, soup:BeautifulSoup) -> list:
pages = findall(r'films/diary/page/(\d+)/', str(soup))
if len(pages) == 0:
pages = ['1']
last_page = max([int(page) for page in pages])
diary_data = {
"dates": PyBoxd.find_dates(soup),
"film_slugs": PyBoxd.find_film_slugs(soup),
"ratings": PyBoxd.find_ratings(soup),
"likes": PyBoxd.find_likes(soup),
"rewatches": PyBoxd.find_rewatches(soup),
"reviews": PyBoxd.find_reviews(soup)
}
with ThreadPoolExecutor() as executor:
futures = [executor.submit(PyBoxd.process_diary_page, user, i) for i in range(2, last_page + 1)]
for future in as_completed(futures):
page_data = future.result()
diary_data["dates"].extend(page_data["dates"])
diary_data["film_slugs"].extend(page_data["film_slugs"])
diary_data["ratings"].extend(page_data["ratings"])
diary_data["likes"].extend(page_data["likes"])
diary_data["rewatches"].extend(page_data["rewatches"])
diary_data["reviews"].extend(page_data["reviews"])
return [
{
"date": diary_data["dates"][j],
"film_slug": diary_data["film_slugs"][j],
"rating": diary_data["ratings"][j],
"like": diary_data["likes"][j],
"rewatch": diary_data["rewatches"][j],
"review": diary_data["reviews"][j]
}
for j in range(len(diary_data["film_slugs"]))
]
@staticmethod
def find_dates(soup:object) -> list:
return findall(r'films/diary/for/(\d{4}/\d{2}/\d{2})/', str(soup))
@staticmethod
def find_film_slugs(soup:object) -> list:
return findall(r'data-film-slug="([^"]+)"', str(soup))[::2]
@staticmethod
def find_ratings(soup:object) -> list:
rating_list = []
for rating_tag in soup.find_all('span', class_= 'rating'):
rating_class = rating_tag.get('class', [])
rated_value = next((cls.split('-')[-1] for cls in rating_class if cls.startswith('rated-')), None)
if rated_value:
rating_list.append(int(rated_value))
else:
rating_list.append('NA')
return rating_list
@staticmethod
def find_likes(soup:object) -> list:
return [True if 'icon-liked' in like else False for like in findall(r'<td class="td-like center diary-like">(.*?)</td>', str(soup))]
@staticmethod
def find_rewatches(soup:object) -> list:
return [False if 'icon-status' in rewatch else True for rewatch in findall(r'<td class="td-rewatch center( icon-status-off)?">', str(soup))]
@staticmethod
def find_reviews(soup:object) -> list:
return [
f'https://letterboxd.com{findall(r"href=\"([^\"]+)\"", td)[0]}reviews/'
if findall(r'href="([^"]+)"', td)
else 'NA'
for td in findall(r'<td class="td-review center(?: [^"]*)?">(.*?)</td>', str(soup), DOTALL)
]
def main():
user = PyBoxd.user()
user.set_username('fer_nwn') #kurstboy
#user.get_profile_stats()
#user.get_user_watched_films()
#user.get_user_watchlist()
#user.get_user_network()
#user.get_user_bio()
print(user)
user.get_user_image()
print(user.userImage)
#diary = PyBoxd.user_diary(user)
#print(diary)
#import time
#start_time = time.time()
#diary.get_user_diary()
#end_time = time.time()
#print(f'Time taken to get user diary: {end_time - start_time} seconds')
#print(len(diary.userDiary))
#[print(diary.userDiary[_]) for _ in range(len(diary.userDiary))]
#print(user)
#print(len(user.watchedFilms))
#print(len(user.watchlist))
#print(user.userNetwork)
#print(user.userBio)
main()
"""
Username: kurstboy
Time taken to get user diary: 37.64339208602905 seconds
1727 movies scraped
"""