-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstaAnalyzer.py
152 lines (132 loc) · 5.8 KB
/
InstaAnalyzer.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
import sqlite3
import csv
from instagram_private_api import Client, ClientCompatPatch
user_name = ''
password = ''
print("connecting to server")
api = Client(user_name, password)
userID = api.authenticated_user_id
conn = sqlite3.connect(":memory:")
# conn = sqlite3.connect('test.db')
conn.text_factory = str
conn.execute('''CREATE TABLE InstaUser
(pk INT PRIMARY KEY NOT NULL,
is_follower INTEGER,
is_following INTEGER,
is_private INTEGER,
is_verified INTEGER,
liked_images TEXT,
username TEXT,
full_name TEXT,
UNIQUE(pk)
);''')
print("Working on followers")
rank_token = api.generate_uuid(return_hex=False, seed=None)
result = api.user_followers(userID, rank_token)
users = result.get('users', [])
for x in users:
user_name = x.get('username', []).encode('utf-8')
full_name = x.get('full_name', []).encode('utf-8')
pk = x.get('pk', [])
is_verified = '1' if x.get('is_verified', []) else '0'
is_private = '1' if x.get('is_private', []) else '0'
params = (pk, 1, 0, is_private, is_verified, '', user_name, full_name)
conn.execute("insert into InstaUser values (?, ?, ?, ?, ?, ?, ?, ?)", params)
big_list=result.get('big_list', [])
while big_list:
result = api.user_followers(userID, rank_token, max_id = result.get('next_max_id', []))
big_list=result.get('big_list', [])
users = result.get('users', [])
for x in users:
user_name = x.get('username', []).encode('utf-8')
full_name = x.get('full_name', []).encode('utf-8')
pk = x.get('pk', [])
is_verified = '1' if x.get('is_verified', []) else '0'
is_private = '1' if x.get('is_private', []) else '0'
params = (pk, 1, 0, is_private, is_verified, '', user_name, full_name)
conn.execute("insert into InstaUser values (?, ?, ?, ?, ?, ?, ?, ?)", params)
print("Working on following")
result = api.user_following(userID, rank_token)
users = result.get('users', [])
for x in users:
pk = x.get('pk', [])
conn.execute("UPDATE InstaUser set is_following = 1 where pk = "+str(pk))
conn.commit
user_name = x.get('username', []).encode('utf-8')
full_name = x.get('full_name', []).encode('utf-8')
is_verified = '1' if x.get('is_verified', []) else '0'
is_private = '1' if x.get('is_private', []) else '0'
params = (pk, 0, 1, is_private, is_verified, '', user_name, full_name)
conn.execute("INSERT OR IGNORE INTO InstaUser VALUES (?, ?, ?, ?, ?, ?, ?, ?)", params)
big_list=result.get('big_list', [])
while big_list:
result = api.user_following(userID, rank_token, max_id = result.get('next_max_id', []))
big_list=result.get('big_list', [])
users = result.get('users', [])
for x in users:
pk = x.get('pk', [])
conn.execute("UPDATE InstaUser set is_following = 1 where pk = "+str(pk))
conn.commit
user_name = x.get('username', []).encode('utf-8')
full_name = x.get('full_name', []).encode('utf-8')
is_verified = '1' if x.get('is_verified', []) else '0'
is_private = '1' if x.get('is_private', []) else '0'
params = (pk, 0, 1, is_private, is_verified, '', user_name, full_name)
conn.execute("INSERT OR IGNORE INTO InstaUser VALUES (?, ?, ?, ?, ?, ?, ?, ?)", params)
print("Working on your medias")
result = api.user_detail_info(userID).get('feed', [])
items = result.get('items', [])
counter = 1
for x in items:
media_id = x.get('pk', [])
like_count = str(x.get('like_count', []))
comment_count = str(x.get('comment_count', []))
print("Working on media NO "+str(counter)+" ==> "+str(media_id))
counter = counter + 1
for x in api.media_likers(media_id).get('users', []):
pk = x.get('pk', [])
liked_images = ''
cursor = conn.execute("SELECT liked_images from InstaUser where pk = "+str(pk))
for row in cursor:
liked_images=str(row[0])+"|("+str(media_id)+","+like_count+","+comment_count+")"
conn.execute("UPDATE InstaUser set liked_images = ? where pk = ?", (liked_images, pk))
conn.commit
more_available=result.get('more_available', [])
while more_available:
result = api.user_detail_info(userID, max_id=result.get('next_max_id', [])).get('feed', [])
more_available=result.get('more_available', [])
items = result.get('items', [])
for x in items:
media_id = x.get('pk', [])
like_count = str(x.get('like_count', []))
comment_count = str(x.get('comment_count', []))
print("Working on media NO "+str(counter)+" ==> "+str(media_id))
counter = counter + 1
for x in api.media_likers(media_id).get('users', []):
pk = x.get('pk', [])
liked_images = ''
cursor = conn.execute("SELECT liked_images from InstaUser where pk = "+str(pk))
for row in cursor:
liked_images=str(row[0])+"|("+str(media_id)+","+like_count+","+comment_count+")"
conn.execute("UPDATE InstaUser set liked_images = ? where pk = ?", (liked_images, pk))
conn.commit
print("Wrting your data to CSV files")
with open('InstaInfo.csv', 'w') as csvfile:
fieldnames = ['pk', 'is_follower', 'is_following', 'is_private', 'is_verified', 'liked_images', 'username', 'full_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
cursor = conn.execute("SELECT * from InstaUser")
for row in cursor:
writer.writerow(
{
'pk': row[0],
'is_follower': row[1],
'is_following': row[2],
'is_private': row[3],
'is_verified': row[4],
'liked_images': row[5],
'username': row[6],
'full_name': row[7]
}
)
conn.close()