-
Notifications
You must be signed in to change notification settings - Fork 5
/
instagramer.py
executable file
·221 lines (191 loc) · 9.04 KB
/
instagramer.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
#!/usr/bin/env python3
# Instagramer by Codelatte
"""
_ _
(_) | |
_ _ __ ___| |_ __ _ __ _ _ __ __ _ _ __ ___ ___ _ __
| | '_ \/ __| __/ _` |/ _` | '__/ _` | '_ ` _ \ / _ \ '__|
| | | | \__ \ || (_| | (_| | | | (_| | | | | | | __/ |
|_|_| |_|___/\__\__,_|\__, |_| \__,_|_| |_| |_|\___|_|
__/ |
|___/ https://dev.codelatte.net
"""
import json
import os
import sys
import time
import urllib.request
from optparse import OptionParser
from typing import (
AnyStr
)
import requests
def getUserDetails(username: AnyStr) -> None:
with requests.get("https://www.instagram.com/" + username + "/?__a=1") as response:
try:
assert response.status_code != 404
except AssertionError:
raise BaseException("Error: User Not Found!")
except requests.ConnectionError:
raise ConnectionError("Error: Connnection Error")
finally:
data = response
output = json.loads(data.content)
if not os.path.exists(username):
os.makedirs(username)
info = {
'user_id': output["graphql"]["user"]["id"],
'username': output["graphql"]["user"]["username"],
'full_name': output["graphql"]["user"]["full_name"],
'biography': str(output["graphql"]["user"]["biography"].replace("\n", " | ")),
'profile_pic_url_hd': output["graphql"]["user"]["profile_pic_url_hd"],
'highlight_story': output["graphql"]["user"]["highlight_reel_count"],
'external_url': output["graphql"]["user"]["external_url"],
'followers': output["graphql"]["user"]["edge_followed_by"]["count"],
'followings': output["graphql"]["user"]["edge_follow"]["count"],
'media_count': output["graphql"]["user"]["edge_owner_to_timeline_media"]["count"],
'is_joined_recently': output["graphql"]["user"]["is_joined_recently"],
'is_business_account': output["graphql"]["user"]["is_business_account"],
'business_category_name': output["graphql"]["user"]["business_category_name"],
'is_private': output["graphql"]["user"]["is_private"],
'connected_fb_page': output["graphql"]["user"]["connected_fb_page"],
'is_verified': output["graphql"]["user"]["is_verified"]
}
with open(username + "/info.json", "w") as file:
try:
file.write(json.dumps(info, indent=4) + "\n")
finally:
file.close()
if options.getmedia is None:
print("USER INFORMATION")
print("----------------------")
print("User ID \t\t: " + str(output["graphql"]["user"]["id"]))
print("Username \t\t: " + str(output["graphql"]["user"]["username"]))
print("Full Name \t\t: " + str(output["graphql"]["user"]["full_name"]))
print("Biography \t\t: " + str(output["graphql"]["user"]["biography"].replace("\n", " | ")))
text = "Profile Picture URL (HD)"
target = output["graphql"]["user"]["profile_pic_url_hd"]
print("Profile Picture \t: " + str(f"\u001b]8;;{target}\u001b\\{text}\u001b]8;;\u001b\\"))
print("Highlight Story \t: " + str(output["graphql"]["user"]["highlight_reel_count"]))
print("External URL \t\t: " + str(output["graphql"]["user"]["external_url"]))
print("Followers / Followings \t: " + str(
output["graphql"]["user"]["edge_followed_by"]["count"]) + " Followers / " + str(
output["graphql"]["user"]["edge_follow"]["count"]) + " Followings")
print("Total Media(s) \t\t: " + str(output["graphql"]["user"]["edge_owner_to_timeline_media"]["count"]))
print("Joined Recently \t: " + str(output["graphql"]["user"]["is_joined_recently"]))
print("Bussines Account \t: " + str(output["graphql"]["user"]["is_business_account"]))
print("Bussines Category \t: " + str(output["graphql"]["user"]["business_category_name"]))
print("Private Account \t: " + str(output["graphql"]["user"]["is_private"]))
print("Connect to FB \t\t: " + str(output["graphql"]["user"]["connected_fb_page"]))
print("Verified \t\t: " + str(output["graphql"]["user"]["is_verified"]))
print("\n\nOutput File: " + username + "/info.json")
print("Note: Use \"--get-media\" to download all " + str(output["graphql"]["user"]["username"]) + "'s media.")
else:
print("\n")
if output["graphql"]["user"]["edge_owner_to_timeline_media"]["count"] > 50:
next_page = 1
else:
next_page = 0
getUserMedia(output["graphql"]["user"]["id"], str(output["graphql"]["user"]["username"]),
str(output["graphql"]["user"]["edge_owner_to_timeline_media"]["count"]),
output["graphql"]["user"]["is_private"], next_page)
def getUserMedia(user_id: AnyStr, username: AnyStr, media_count: AnyStr, is_private: AnyStr,
next_page: int = 0, end_cursor: AnyStr = "", itung: int = 0):
if is_private:
raise "Error: Private User detected! Can't get " + username + "'s media!"
if itung == 0:
print("GET USER's MEDIA " + "(" + media_count + ")")
print("Note: Downloading video file will take longer than image.")
print("----------------------")
time.sleep(3)
if next_page == 1:
with requests.get(
"https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=" + user_id + "&first=50&after=" + str(
end_cursor)) as response:
try:
data = response
except requests.ConnectionError:
raise ConnectionError("Error: Connection Error")
output = json.loads(data.content)
else:
with requests.get(
"https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=" + user_id + "&first=50&after=") as response:
try:
data = response
except requests.ConnectionError:
raise ConnectionError("Error: Connection Error")
output = json.loads(data.content)
for x in output["data"]["user"]["edge_owner_to_timeline_media"]["edges"]:
media_type = "Image" if x["node"]["is_video"] == False else "Video"
if x["node"]["__typename"] == "GraphSidecar":
with requests.get(
"https://www.instagram.com/p/" + str(x["node"]["shortcode"]) + "/?__a=1") as responsepost:
try:
result = responsepost
except requests.ConnectionError:
raise ConnectionError("Error: Connection Error")
result = json.loads(result.content)
for i in result["graphql"]["shortcode_media"]["edge_sidecar_to_children"]["edges"]:
filename = str(i["node"]["display_url"]).split('?')[0]
filename = filename.split('/')[-1]
urllib.request.urlretrieve(str(i["node"]["display_url"]), username + "/" + filename)
if media_type == "Video":
data_video = urllib.request.urlopen(
"https://www.instagram.com/p/" + str(x["node"]["shortcode"]) + "/?__a=1").read()
output_video = json.loads(data_video)
filename = output_video["graphql"]["shortcode_media"]["video_url"].split('?')[0]
filename = filename.split('/')[-1]
urllib.request.urlretrieve(output_video["graphql"]["shortcode_media"]["video_url"],
username + "/" + filename)
else:
if x["node"]["__typename"] == "GraphSidecar":
pass
filename = str(x["node"]["thumbnail_src"]).split('?')[0]
filename = filename.split('/')[-1]
urllib.request.urlretrieve(str(x["node"]["thumbnail_src"]), username + "/" + filename)
itung = itung + 1
print("Media Count \t: " + str(itung) + " / " + str(media_count))
print("Timestamp \t: " + time.ctime(int(x["node"]["taken_at_timestamp"])))
text = "Post URL"
target = "https://www.instagram.com/p/" + str(x["node"]["shortcode"])
print("Shortcode \t: " + str(x["node"]["shortcode"]) + " | " + str(
f"\u001b]8;;{target}\u001b\\{text}\u001b]8;;\u001b\\"))
if media_type == "Video":
print("Media Type \t: Video")
print("Video View \t: " + str(x["node"]["video_view_count"]))
else:
print("Media Type \t: Image")
print("Like(s) \t: " + str(x["node"]["edge_media_preview_like"]["count"]))
print("Comment(s) \t: " + str(x["node"]["edge_media_to_comment"]["count"]))
print("Saved Media \t: " + username + "/" + filename)
print("------------------------------------------")
if output["data"]["user"]["edge_owner_to_timeline_media"]["page_info"]["has_next_page"] == 1:
getUserMedia(user_id, username, media_count, 1, output["data"]["user"]["edge_owner_to_timeline_media"]["page_info"]["end_cursor"], itung)
else:
print("\nGet " + username + "'s " + media_count + " Media(s) Success")
banner = """ _ _
(_) | |
_ _ __ ___| |_ __ _ __ _ _ __ __ _ _ __ ___ ___ _ __
| | '_ \/ __| __/ _` |/ _` | '__/ _` | '_ ` _ \ / _ \ '__|
| | | | \__ \ || (_| | (_| | | | (_| | | | | | | __/ |
|_|_| |_|___/\__\__,_|\__, |_| \__,_|_| |_| |_|\___|_|
__/ |
|___/ https://dev.codelatte.net """
parser = OptionParser(usage="Usage: python3 %prog -u [username]")
parser.add_option("-u", dest='username', help="Get Account Details")
parser.add_option("--get-media", action='store_true', dest='getmedia', help="Get All User's Media")
(options, args) = parser.parse_args()
if __name__ == "__main__":
if len(sys.argv) < 2:
print(banner)
parser.print_help()
sys.exit()
else:
print(banner)
try:
if options.username:
getUserDetails(options.username)
elif options.getmedia:
getUserDetails(options.username)
except KeyboardInterrupt:
print("\nExiting...")