Skip to content

Commit

Permalink
🦄 Refactor Code and Fix Some Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yezz123 committed Sep 15, 2021
1 parent 36cac5e commit a4cdcd1
Showing 1 changed file with 112 additions and 88 deletions.
200 changes: 112 additions & 88 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
import time
import json
import random
import string
import os
import sys
import requests
import collections
import urllib.request
from bs4 import BeautifulSoup

nu = '\033[0m'
re = '\033[1;31m'
gr = '\033[1;32m'
cy = '\033[1;36m'
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']
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():

class extra:
def tiny_url(url):
apiurl = "http://tinyurl.com/api-create.php?url="
tinyurl = urllib.request.urlopen(apiurl + url).read()
Expand All @@ -41,59 +41,73 @@ def write(in_text):
sys.stdout.flush()

def extract_hash_tags(stri):
return list(part[1:] for part in stri.split() if part.startswith('#'))
return [part[1:] for part in stri.split() if part.startswith("#")]

def banner():
print(f"""{cy}
print(
f"""{cy}
╔═╗ ╔═╗ ╦ ╔═╗
╚═╗ ║ ║ ║ ║ ╦
╚═╝ ╚═╝ {gr}o{cy} ╩ ╚═╝
{gr}Coded By :
{gr}Yezz123{nu}
""")

"""
)

class main():

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']
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']),
"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'])
"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':
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']):
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']))
raw_tags.append(
extra.extract_hash_tags(
post["node"]["edge_media_to_caption"]["edges"][0]["node"][
"text"
]
)
)
except IndexError:
pass
x = len(raw_tags)
Expand All @@ -102,14 +116,9 @@ def get_profile(self):
self.tags = dict(collections.Counter(tag_lis))

return self.tags
return self.output

def print_data_(self):
os.system("clear")
extra.banner()
for key, value in self.output.items():
print(f"{gr}{key} : {nu}{value}")
print("")
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():
Expand All @@ -120,6 +129,9 @@ def print_data_(self):
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():
Expand All @@ -134,29 +146,34 @@ def make_dir(self):
os.chdir(self.user)

def scrap_uploads(self):
if self.output["private "].lower() == 'true':
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']):
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']))}")
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']}")
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'])}
"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}")
Expand All @@ -172,57 +189,64 @@ def save_data(self):
# 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)})
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']),
"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'])
"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']):
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'])}

posts[index]["picture"] = extra.tiny_url(
str(post['node']['thumbnail_resources'][0]['src']))
"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'])
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:
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:
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")

0 comments on commit a4cdcd1

Please sign in to comment.