forked from mamanain/recommendMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
117 lines (85 loc) · 3.85 KB
/
utils.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
import urllib
import json
import vk
import time
import random
import os
import numpy as np
def _get_coded_string(string):
value_utf8 = string.encode("utf-8")
return urllib.parse.quote(value_utf8)
def get_movie_info(movie_name, api_key, desired_list=('id', 'title', 'original_title', 'genre_ids', 'poster_path')):
URL = "https://api.themoviedb.org/3/search/movie?api_key={0}&language=ru&query={1}&page=1&include_adult=false"
URL = URL.format(api_key, _get_coded_string(movie_name))
response = urllib.request.urlopen(URL)
response = response.read().decode("utf-8")
js = json.loads(response)
if len(js['results']) == 0:
return []
else:
return {x: js['results'][0][x] for x in desired_list}
def get_data(_id, n, database, user_collection, api_key, min_num_of_movies=3):
count = 0
while database.get_collection_size(user_collection) < n:
f = vk.get_friends(str(_id))
friends = []
for x in f:
friends.append(str(x))
friends_info = vk.get_users_info(friends)
for friend in friends_info.keys():
if database.get_collection_size(user_collection) >= n:
break
try:
if count == 100:
time.sleep(1)
count = 0
else:
count += 1
if ("movies" in friends_info[friend].keys() and
len(friends_info[friend]["movies"].split(",")) >= min_num_of_movies and
len(friends) >= 5):
movies = []
min_len = 2
for movie in friends_info[friend]["movies"].split(","):
if len(movie) >= min_len:
m_info = get_movie_info(movie, api_key)
if len(m_info):
movies.append(m_info['id'])
m_info['_id'] = m_info['id']
del m_info['id']
# Inserting new movies
try:
database.insert_one(m_info, "Movie_Info")
except Exception as e:
continue
if len(movies) >= min_num_of_movies:
friends_info[friend]['_id'] = friends_info[friend]['uid']
del friends_info[friend]['uid']
friends_info[friend]['movies'] = movies
try:
# Inserting new user, plus his rating
f = friends_info[friend]
f['groups'] = vk.get_groups(f['_id'])['groups']['items']
f = {x: f[x] for x in ["movies", "groups", "_id"]}
rating = {str(x): 10 for x in f['movies']}
rating["_id"] = f["_id"]
database.insert_one(f, "User_Info")
database.insert_one(rating, "ratings")
except Exception as e:
continue
except:
pass
if database.get_collection_size(user_collection) < n:
last_id = _id
while True:
ind = random.randint(0, len(friends) - 1)
if (len(vk.get_friends(friends[ind])) >= 5 and
last_id != friends[ind]):
_id = friends[ind]
break
def download_avatars(database, name="photo_100"):
ids = list(map(lambda x: x['_id'], database.get_all("User_Info")))
info = vk.get_users_info(ids, [name])
for user in info.values():
if name in user.keys():
urllib.request.urlretrieve(user[name], 'Images/{0}.png'.format(user['uid']))