-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
96 lines (67 loc) · 2.1 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
import json
from pprint import pprint
from string import Template
from presentation import generate_template
def calculateLikesFrom(likesDict, username):
for user, likeCount in likesDict.iteritems():
if user == username:
return likeCount
return 0
def extractFirstName(s):
res = s.split('*')[0].split('/')[-1]
return str(res)
def extractSecondName(s):
res = s.split('*')[2].split('.')[0]
return str(res)
###############################################################
def convertJsonToLikesTable(jsonPath, likesTablePath):
with open(jsonPath) as data_file:
data = json.load(data_file)
username = data['username']
print "Convert json data for " + username + "..."
userLikes = {}
photos = data['photos']
for photo in photos:
likes = photo['likes']
for like in likes:
like_by = str(like['username'])
if like_by in userLikes:
userLikes[like_by] += 1
else:
userLikes[like_by] = 1
oo = open(likesTablePath, "w")
for user, likeCount in userLikes.iteritems():
oo.write(str(user) + ',' + str(likeCount) + '\n')
oo.close()
def calculateMutualLikes(whoLikes, whomLikes, output):
whoLikesFile = open(whoLikes)
whomLikesFile = open(whomLikes)
outputFile = open(output, "w")
likes_dict = {}
for line in whomLikesFile:
(user, like_count) = line.split(",")
likes_dict[user] = int(like_count)
username = whoLikesFile.name.split('.')[0]
likes = calculateLikesFrom(likes_dict, username)
outputFile.write(str(likes))
whoLikesFile.close()
whomLikesFile.close()
outputFile.close()
def calcStatistics(likesCountFiles, output):
usersSet = set()
for f in likesCountFiles:
usersSet.add(extractFirstName(f))
usersList = list(usersSet)
peoples_count = len(usersList)
userMapping = {}
for i in xrange(peoples_count):
userMapping[usersList[i]] = i
data = [[0 for i in xrange(peoples_count)] for j in xrange(peoples_count)]
for f in likesCountFiles:
ii = open(f)
likes = int(ii.read())
ii.close()
whoLikes = extractFirstName(f)
whomLikes = extractSecondName(f)
data[userMapping[whoLikes]][userMapping[whomLikes]] = likes
generate_template(data, usersList, output)