-
Notifications
You must be signed in to change notification settings - Fork 1
/
populate_ratings.py
53 lines (34 loc) · 1.17 KB
/
populate_ratings.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
import os
import urllib.request
import django
import datetime
import decimal
from tqdm import tqdm
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'prs_project.settings')
django.setup()
from analytics.models import Rating
def create_rating(user_id, content_id, rating, timestamp):
rating = Rating(user_id=user_id, movie_id=content_id, rating=decimal.Decimal(rating),
rating_timestamp=datetime.datetime.fromtimestamp(float(timestamp)))
rating.save()
return rating
def download_ratings():
URL = 'https://raw.githubusercontent.com/sidooms/MovieTweetings/master/latest/ratings.dat'
response = urllib.request.urlopen(URL)
data = response.read()
print('download finished')
return data.decode('utf-8')
def delete_db():
print('truncate db')
Rating.objects.all().delete()
print('finished truncate db')
def populate():
delete_db()
ratings = download_ratings()
for rating in tqdm(ratings.split(sep="\n")):
r = rating.split(sep="::")
if len(r) == 4:
create_rating(r[0], r[1], r[2], r[3])
if __name__ == '__main__':
print("Starting MovieGeeks Population script...")
populate()