-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate.py
248 lines (202 loc) · 8.34 KB
/
update.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
import asyncio
import requests
from pprint import pprint
import time
import sys
from aiohttp import ClientSession
from bs4 import BeautifulSoup
import pymongo
from pymongo import UpdateOne
from pymongo.errors import BulkWriteError
from config import conn_url
def get_conn_url(db_name):
return conn_url + db_name + '?retryWrites=true&w=majority'
async def fetch(url, session, input_data={}):
async with session.get(url) as response:
return await response.read(), input_data
def get_page_count(username):
url = "https://letterboxd.com/{}/films/by/date"
r = requests.get(url.format(username))
soup = BeautifulSoup(r.text, "lxml")
body = soup.find("body")
if "error" in body["class"]:
return -1
try:
page_link = soup.findAll("li", attrs={"class", "paginate-page"})[-1]
num_pages = int(page_link.find("a").text.replace(',', ''))
except IndexError:
num_pages = 1
return num_pages
async def get_page_counts(lb_ids, users_cursor):
url = "https://letterboxd.com/{}/films/"
tasks = []
async with ClientSession() as session:
for lb_id in lb_ids:
task = asyncio.ensure_future(fetch(url.format(lb_id), session, {"lb_id": lb_id}))
tasks.append(task)
responses = await asyncio.gather(*tasks)
for response in responses:
soup = BeautifulSoup(response[0], "lxml")
try:
page_link = soup.findAll("li", attrs={"class", "paginate-page"})[-1]
num_pages = int(page_link.find("a").text.replace(',', ''))
except IndexError:
num_pages = 1
users_cursor.update_one({"lb_id": response[1]['lb_id']}, {"$set": {"num_ratings_pages": num_pages}})
async def generate_ratings_operations(response, send_to_db=True, return_unrated=False):
# Parse ratings page response for each rating/review, use lxml parser for speed
soup = BeautifulSoup(response[0], "lxml")
reviews = soup.findAll("li", attrs={"class": "poster-container"})
# Create empty array to store list of bulk operations or rating objects
operations = []
# For each review, parse data from scraped page and append an UpdateOne operation for bulk execution or a rating object
for review in reviews:
movie_id = review.find('div', attrs={"class", "film-poster"})['data-target-link'].split('/')[-2]
rating = review.find("span", attrs={"class": "rating"})
if not rating:
if return_unrated == False:
continue
else:
rating_id = -1
else:
rating_class = rating['class'][-1]
rating_id = int(rating_class.split('-')[-1])
if response[1]["lb_id"] in ['hizv', 'ketchupin']:
rating_id *= 1.25
rating_object = {
"movie_id": movie_id,
"rating_id": rating_id,
"lb_id": response[1]["lb_id"]
}
# If returning objects, just append the object to return list
if not send_to_db:
operations.append(rating_object)
# Otherwise return an UpdateOne operation to bulk execute
else:
operations.append(UpdateOne({
"lb_id": response[1]["lb_id"],
"movie_id": movie_id
},
{
"$set": rating_object
}, upsert=True))
return operations
async def get_user_ratings(lb_id, db_cursor=None, mongo_db=None, store_in_db=True, num_pages=None, return_unrated=False):
url = "https://letterboxd.com/{}/films/by/date/page/{}/"
if not num_pages:
# Find them in the MongoDB database and grab the number of ratings pages
user = db_cursor.find_one({"lb_id": lb_id})
num_pages = user["num_ratings_pages"]
# Fetch all responses within one Client session,
# keep connection alive for all requests.
async with ClientSession() as session:
tasks = []
# Make a request for each ratings page and add to task queue
for i in range(num_pages):
task = asyncio.ensure_future(fetch(url.format(lb_id, i+1), session, {"lb_id": lb_id}))
tasks.append(task)
# Gather all ratings page responses
scrape_responses = await asyncio.gather(*tasks)
# Process each ratings page response, converting it into bulk upsert operations or output dicts
tasks = []
for response in scrape_responses:
task = asyncio.ensure_future(generate_ratings_operations(response, send_to_db=store_in_db, return_unrated=return_unrated))
tasks.append(task)
parse_responses = await asyncio.gather(*tasks)
# Concatenate each response's upsert operations/output dicts
upsert_operations = []
for response in parse_responses:
upsert_operations += response
if not store_in_db:
return upsert_operations
# Execute bulk upsert operations
try:
if len(upsert_operations) > 0:
# Create/reference "ratings" collection in db
ratings = mongo_db.ratings
ratings.bulk_write(upsert_operations, ordered=False)
except BulkWriteError as bwe:
pprint(bwe.details)
async def get_ratings(lb_ids, db_cursor=None, mongo_db=None, store_in_db=True):
start = time.time()
# Loop through each user
for i, lb_id in enumerate(lb_ids):
print(i, lb_id, round((time.time() - start), 2))
await get_user_ratings(lb_id, db_cursor=db_cursor, mongo_db=mongo_db, store_in_db=store_in_db, return_unrated=True)
def main():
# Connect to MongoDB Client
db_name = sys.argv[1]
client = pymongo.MongoClient(get_conn_url(db_name))
# Find letterboxd database and user collection
db = client[db_name]
users = db.users
films = db.films
ratings = db.ratings
if len(sys.argv) < 3:
all_users = users.find({})
all_lb_ids = [x['lb_id'] for x in all_users]
loop = asyncio.get_event_loop()
# Find number of ratings pages for each user and add to their Mongo document (note: max of 128 scrapable pages)
future = asyncio.ensure_future(get_page_counts(all_lb_ids, users))
loop.run_until_complete(future)
# Find and store ratings for each user
future = asyncio.ensure_future(get_ratings(all_lb_ids, users, db))
loop.run_until_complete(future)
# Update rating avg
for movie_id in ratings.distinct('movie_id'):
total, r_count, ur_count = 0, 0, 0
for rating in ratings.find({'movie_id': movie_id}):
rating_id = rating['rating_id']
if rating_id != -1:
total += rating_id
r_count += 1
else:
ur_count += 1
avg = total/r_count if r_count > 0 else 0
film = {
'movie_id': movie_id,
'guild_avg': avg,
'rating_count': r_count,
'watch_count': (r_count + ur_count)
}
pprint(film)
films.update_one({
'movie_id': movie_id
},
{
"$set": film
}, upsert=True)
else:
lb_id = users.find({'uid': int(sys.argv[2])})[0]['lb_id']
num_pages = get_page_count(lb_id)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
future = asyncio.ensure_future(get_user_ratings(lb_id, users, db, num_pages=num_pages))
print(future)
loop.run_until_complete(future)
# Update rating avg
for movie_id in ratings.find({'uid': int(sys.argv[2])}):
total, r_count, ur_count = 0, 0, 0
for rating in ratings.find({'movie_id': movie_id}):
rating_id = rating['rating_id']
if rating_id != -1:
total += rating_id
r_count += 1
else:
ur_count += 1
avg = total/r_count if r_count > 0 else 0
film = {
'movie_id': movie_id,
'guild_avg': avg,
'rating_count': r_count,
'watch_count': (r_count + ur_count)
}
pprint(film)
films.update_one({
'movie_id': movie_id
},
{
"$set": film
}, upsert=True)
if __name__ == "__main__":
main()