-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapi.py
254 lines (225 loc) · 9.94 KB
/
api.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/env python3
import time
import json
import random
import os
import sys
import requests
import collections
import urllib.request
from bs4 import BeautifulSoup
collections.Callable = collections.abc.Callable
nu = "\033[0m"
re = "\033[1;31m"
gr = "\033[1;32m"
cy = "\033[1;36m"
raw_tags = []
tag_lis = []
useragent = [
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14",
"Mozilla/5.0 (Linux; U; Android-4.0.3; en-us; Galaxy Nexus Build/IML74K) AppleWebKit/535.7 (KHTML, like Gecko) CrMo/16.0.912.75 Mobile Safari/535.7",
]
class extra:
def tiny_url(url):
apiurl = "http://tinyurl.com/api-create.php?url="
tinyurl = urllib.request.urlopen(apiurl + url).read()
return tinyurl.decode("utf-8")
def write(in_text):
for char in in_text:
time.sleep(0.1)
sys.stdout.write(char)
sys.stdout.flush()
def extract_hash_tags(stri):
return [part[1:] for part in stri.split() if part.startswith("#")]
def banner():
print(
f"""{cy}
╔═╗ ╔═╗ ╦ ╔═╗
╚═╗ ║ ║ ║ ║ ╦
╚═╝ ╚═╝ {gr}o{cy} ╩ ╚═╝
{gr}Coded By :
{gr}Yezz123{nu}
"""
)
class main:
def __init__(self, user):
self.user = user
self.get_profile()
def get_profile(self):
extra.write(f"\n{gr}[+]{nu} getting profile ...")
profile = requests.get(
f"https://www.instagram.com/{self.user}",
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
},
)
soup = BeautifulSoup(profile.text, "html.parser")
more_data = soup.find_all("script", attrs={"type": "text/javascript"})
self.data = json.loads(more_data[3].get_text()[21:].strip(";"))
self.p_data = self.data["entry_data"]["ProfilePage"][0]["graphql"]["user"]
self.output = {
"username ": str(self.p_data["username"]),
"name ": str(self.p_data["full_name"]),
"url ": str(f"instagram.com/{self.p_data['username']}"),
"followers ": str(self.p_data["edge_followed_by"]["count"]),
"following ": str(self.p_data["edge_follow"]["count"]),
"posts ": str(
self.p_data["edge_owner_to_timeline_media"]["count"]
),
"bio ": str(self.p_data["biography"].replace("\n", ", ")),
"external url ": str(self.p_data["external_url"]),
"private ": str(self.p_data["is_private"]),
"verified ": str(self.p_data["is_verified"]),
"profile pic url ": extra.tiny_url(str(self.p_data["profile_pic_url_hd"])),
"business account ": str(self.p_data["is_business_account"]),
"connected to fb ": str(self.p_data["connected_fb_page"]),
"joined recently ": str(self.p_data["is_joined_recently"]),
"business category": str(self.p_data["business_category_name"]),
}
if str(self.p_data["is_private"]).lower() == "true":
print(f"{re}[!]{gr} private profile can't scrap data !\n")
return 1
else:
for index, post in enumerate(
self.p_data["edge_owner_to_timeline_media"]["edges"]
):
try:
raw_tags.append(
extra.extract_hash_tags(
post["node"]["edge_media_to_caption"]["edges"][0]["node"][
"text"
]
)
)
except IndexError:
pass
x = len(raw_tags)
for i in range(x):
tag_lis.extend(raw_tags[i])
self.tags = dict(collections.Counter(tag_lis))
return self.tags
def print_data_(self):
self._extracted_from_print_data_2()
print(f"{gr}[+]{nu} most used user tags : \n")
o = 0
for key, value in collections.Counter(self.tags).most_common():
print(f"{gr}{key} : {nu}{value}")
o += 1
if o == 5:
break
print("")
def print_data(self):
self._extracted_from_print_data_2()
def _extracted_from_print_data_2(self):
os.system("clear")
extra.banner()
for key, value in self.output.items():
print(f"{gr}{key} : {nu}{value}")
print("")
def make_dir(self):
try:
os.mkdir(self.user)
os.chdir(self.user)
except FileExistsError:
os.chdir(self.user)
def scrap_uploads(self):
if self.output["private "].lower() == "true":
print(f"{re}[!]{gr} private profile can't scrap data !\n")
return 1
else:
posts = {}
print(f"{gr}[+]{nu} user uploads data : \n")
for index, post in enumerate(
self.p_data["edge_owner_to_timeline_media"]["edges"]
):
# GET PICTURE URL AND SHORTEN IT
print(
f"{gr}picture : {nu}{extra.tiny_url(str(post['node']['thumbnail_resources'][0]['src']))}"
)
# IF PIC HAS NO CAPTIONS > SKIP / PRINT
try:
print(
f"{gr}Caption : {nu}{post['node']['edge_media_to_caption']['edges'][0]['node']['text']}"
)
except IndexError:
pass
posts[index] = {
"comments": str(post["node"]["edge_media_to_comment"]["count"]),
"comments disabled": str(post["node"]["comments_disabled"]),
"timestamp": str(post["node"]["taken_at_timestamp"]),
"likes": str(post["node"]["edge_liked_by"]["count"]),
"location": str(post["node"]["location"]),
"accessability caption": str(post["node"]["accessibility_caption"]),
}
for key, value in posts[index].items():
print(f"{gr}{key} : {nu}{value}")
print("")
def most_common_tags(self):
print(f"{gr}[+]{nu} user uploads tags : \n")
for key, value in collections.Counter(self.tags).most_common():
print(f"{gr}{key} : {nu}{value}")
def save_data(self):
self.make_dir()
# DOWNLOAD PROFILE PICTURE
with open(f"profile_pic.jpg", "wb") as f:
time.sleep(1)
r = requests.get(
self.output["profile pic url "],
headers={"User-Agent": random.choice(useragent)},
)
f.write(r.content)
print(f"{gr}[+]{nu} saved pic to {os.getcwd()}/profile_pic.jpg")
# SAVES PROFILE DATA TO TEXT FILE
self.output_data = {
"username": str(self.p_data["username"]),
"name": str(self.p_data["full_name"]),
"url": str(f"instagram.com/{self.p_data['username']}"),
"followers": str(self.p_data["edge_followed_by"]["count"]),
"following": str(self.p_data["edge_follow"]["count"]),
"posts": str(self.p_data["edge_owner_to_timeline_media"]["count"]),
"bio": str(self.p_data["biography"]),
"external url": str(self.p_data["external_url"]),
"private": str(self.p_data["is_private"]),
"verified": str(self.p_data["is_verified"]),
"profile pic url": extra.tiny_url(str(self.p_data["profile_pic_url_hd"])),
"business account": str(self.p_data["is_business_account"]),
"connected to fb": str(self.p_data["connected_fb_page"]),
"joined recently": str(self.p_data["is_joined_recently"]),
"business category": str(self.p_data["business_category_name"]),
}
with open(f"profile_data.txt", "w") as f:
f.write(json.dumps(self.output_data))
print(f"{gr}[+]{nu} saved data to {os.getcwd()}/profile_data.txt")
# SAVES INFORMATION
posts = {}
for index, post in enumerate(
self.p_data["edge_owner_to_timeline_media"]["edges"]
):
posts[index] = {
"comments": str(post["node"]["edge_media_to_comment"]["count"]),
"comments disabled": str(post["node"]["comments_disabled"]),
"timestamp": str(post["node"]["taken_at_timestamp"]),
"likes": str(post["node"]["edge_liked_by"]["count"]),
"location": str(post["node"]["location"]),
"accessability caption": str(post["node"]["accessibility_caption"]),
"picture": extra.tiny_url(
str(post["node"]["thumbnail_resources"][0]["src"])
),
}
try:
post[index]["caption"] = str(
post["node"]["edge_media_to_caption"]["edges"][0]["node"]["text"]
)
except KeyError:
pass
with open(f"posts_data.txt", "w") as f:
f.write(json.dumps(posts))
print(f"{gr}[+]{nu} saved post info to {os.getcwd()}/posts_data.txt")
# SAVES TAGS
with open(f"tags.txt", "w") as f:
f.write(json.dumps(tag_lis))
print(f"{gr}[+]{nu} saved tags to {os.getcwd()}/posts_data.txt\n")