-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
175 lines (148 loc) · 3.88 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import json
import requests
from flask import Flask
from flask import request
from flask import jsonify
from flask import render_template
from flask_cors import CORS, cross_origin
from getFollowUser import getFollowUser
from getUserMovie import getUserMovie
from getMovieUser import getMovieUser
dataList = []
linksList = []
def mdataParser(data):
obj = {
"name": "",
"symbolSize": 9,
"category": "movie",
"draggable": "true"
}
obj["name"] = data[1]
return obj
def fdataParser(data):
obj = {
"name": "",
"symbolSize": 9,
"category": "user",
"draggable": "true",
"img": "",
"attr": ""
}
obj["img"] = data[2]
obj["name"] = data[1]
obj["attr"] = {}
d = json.loads(data[3])
if (len(d['faces'])>0):
obj["attr"] = d['faces'][0]['attributes']
return obj
def dataParser(data):
obj = {
"name": "",
"symbolSize": 9,
"category": "user",
"draggable": "true",
"img": ""
}
obj["img"] = data[2]
obj["name"] = data[1]
return obj
def linkParser(source, target):
obj = {
"source": "",
"target": ""
}
obj["source"] = source
obj["target"] = target
return obj
def face_detect(imgurl):
url = 'https://v1-api.visioncloudapi.com/face/detection'
pl = {'api_id': 'bdd94d86390c46b99879406d61b70b0f', 'api_secret': 'b59326aeaec84c9eb0d9bda89a77cf63', 'url': imgurl,'attributes':1}
r = requests.post(url, params=pl)
return r.content
# user's following's following
def processUUU(uid):
# uid = 'nulland'
mid = '26210985'
fu = getFollowUser(uid, 0)
foo = fu.getUsers()
tmp = foo.pop(0)
l = 5 if len(foo) > 5 else len(foo)
foo = random.sample(foo, l)
global dataList
global linksList
dataList = []
linksList = []
dataList.append(dataParser(tmp))
source = tmp[1]
for each in foo:
each = [each[0],each[1],each[2],face_detect(each[2])]
dataList.append(fdataParser(each))
linksList.append(linkParser(source, each[1]))
fu = getFollowUser(each[0], 0)
foo = fu.getUsers()
l = 5 if len(foo) > 5 else len(foo)
foo = random.sample(foo, l)
foo.pop(0)
for child in foo:
dataList.append(dataParser(child))
linksList.append(linkParser(each[1], child[1]))
# user's movies' user
def processUMU(uid):
# uid = 'nulland'
mid = '26210985'
um = getUserMovie(uid)
foo = um.getMovies()
tmp = foo.pop(0)
l = 7 if len(foo) > 7 else len(foo)
foo = random.sample(foo, l)
global dataList
global linksList
dataList = []
linksList = []
dataList.append(dataParser(tmp))
source = tmp[1]
for each in foo:
dataList.append(mdataParser(each))
linksList.append(linkParser(source, each[1]))
mu = getMovieUser(each[0])
foo = mu.getUsers()
l = 5 if len(foo) > 5 else len(foo)
foo = random.sample(foo, l)
foo.pop(0)
for child in foo:
dataList.append(dataParser(child))
linksList.append(linkParser(each[1], child[1]))
# um = getUserMovie(uid)
# print(um.getMovies())
# mu = getMovieUser(mid)
# print(mu.getUsers())
app = Flask(__name__)
CORS(app)
@app.route('/data')
def getdata():
global dataList, linksList
uid = request.args.get('uid')
processUUU(uid)
obj = {
"data": dataList,
"links": linksList
}
return jsonify(obj)
@app.route('/mdata')
def getmdata():
global dataList, linksList
uid = request.args.get('uid')
processUMU(uid)
obj = {
"data": dataList,
"links": linksList
}
return jsonify(obj)
@app.route('/')
def hi():
return render_template('index.html')
if __name__ == '__main__':
app.run()