-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
144 lines (125 loc) · 3.84 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from insta import Instagram
import os
import json
import pystache
import io
import webbrowser
import random
numer = sys.argv[1]
print " ~ Connecting to Instagram"
#modify here
insta = Instagram('usernamefoo', 'passfordfoo')
if insta.login() == False:
print "Login failed"
sys.exit(2)
print " ~ Sending request to Instagram , fetching your feeds"
temp = insta.getTotalUserFeed(numer)
likedUsers = {}
for item in temp:
insta.getMediaLikers(item["id"])
test = insta.LastJson["users"]
for user in test:
if user["username"] not in likedUsers:
data = {}
data["count"] = 1
data["name"] = user["full_name"]
data["image"] = user["profile_pic_url"]
data["username"] = user["username"]
likedUsers[user["username"]] = data
else:
likedUsers[user["username"]]["count"] += 1
print " ~ Sending request to Instagram , fetching followers"
followers = insta.getTotalFollowers(numer)
print " ~ Sending request to Instagram , fetching followings"
followings = insta.getTotalFollowings(numer)
followersnum=len(followers)
followingsnum=len(followings)
print " ~ Processing..."
fans = []
for fr in followers:
flag = True
for fg in followings:
if fg["pk"] == fr["pk"]:
flag = False
break
if flag:
fans.append(fr)
notfollowedback = []
for fr in followings:
flag = True
for fg in followers:
if fg["pk"] == fr["pk"]:
flag = False
break
if flag:
notfollowedback.append(fr)
newfollowers = []
if os.path.exists('followers.json'):
with open('followers.json') as data_file:
oldfollowers = json.load(data_file)
for fr in followers:
flag = True
for fg in oldfollowers:
if fg["pk"] == fr["pk"]:
flag = False
break
if flag:
newfollowers.append(fr)
newunfollowers = []
if os.path.exists('followers.json'):
with open('followers.json') as data_file:
oldfollowers = json.load(data_file)
for fr in oldfollowers:
flag = True
for fg in followers:
if fg["pk"] == fr["pk"]:
flag = False
break
if flag:
newunfollowers.append(fr)
newfollowings = []
if os.path.exists('followings.json'):
with open('followings.json') as data_file:
oldfollowings = json.load(data_file)
for fr in followings:
flag = True
for fg in oldfollowings:
if fg["pk"] == fr["pk"]:
flag = False
break
if flag:
newfollowings.append(fr)
with open('followers.json', 'w') as outfile:
json.dump(followers, outfile)
with open('followings.json', 'w') as outfile:
json.dump(followings, outfile)
neverLiked = []
for follower in followers:
if follower["username"] not in likedUsers:
neverLiked.append(follower)
file = open('template.tpl', 'r')
html = file.readlines()
tpl = ''
for line in html:
tpl = tpl + " " + line
likedUsers = sorted(likedUsers.values(),
key=lambda x: x["count"], reverse=True)
data = {
"username": [followersnum, followingsnum],
"notback": notfollowedback,
"newfollowers": newfollowers,
"newunfollowers":newunfollowers,
"newfollowings": newfollowings,
"bestlikers": likedUsers[:20],
# "badlikers": likedUsers[len(likedUsers) - 10:],
#"neverliked": neverLiked[0:10]
"neverliked": random.sample(neverLiked,10)
}
print " ~ Creating output.html"
output = pystache.render(tpl, data)
with io.open('output.html', 'w', encoding='utf8') as f:
f.write(output)
#webbrowser.open_new_tab('output.html')