-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
266 lines (242 loc) · 11.7 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
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
255
256
257
258
259
260
261
262
263
264
265
from utils import generate_session, media_sorter, generate_user_info_file, user_summary, media_downloader, media_downloader_tagged
from instagrapi import Client
import os
import pickle
import requests
def user_followers(user_id):
followers_data = cl.user_followers(user_id)
file_path = os.path.join(target, 'followers.txt')
with open(file_path, 'w',encoding="utf-8") as fh:
for i in followers_data:
#print(f'''Username: "{followers_data[i].username}" Full Name: "{followers_data[i].full_name}" Profile Picture: "{followers_data[i].profile_pic_url}"''')
fh.write(f'''Username: "{followers_data[i].username}" Full Name: "{followers_data[i].full_name}" Profile Picture: "{followers_data[i].profile_pic_url}"''')
fh.write('\n')
def user_following(user_id):
following_data = cl.user_following(user_id)
file_path = os.path.join(target, 'following.txt')
with open(file_path, 'w',encoding="utf-8") as fh:
for i in following_data:
#print(f'''Username: "{followers_data[i].username}" Full Name: "{followers_data[i].full_name}" Profile Picture: "{followers_data[i].profile_pic_url}"''')
fh.write(f'''Username: "{following_data[i].username}" Full Name: "{following_data[i].full_name}" Profile Picture: "{following_data[i].profile_pic_url}"''')
fh.write('\n')
def fetch_images_albums(user_id):
## Grabbing all the media that exist on this profile
unsorted_media = cl.user_medias_v1(user_id)
sorted_media = media_sorter(unsorted_media)
return sorted_media
def fetch_videos(user_id):
unsorted_media = cl.user_clips_v1(user_id)
sorted_media = media_sorter(unsorted_media)
return sorted_media
def fetch_tagged(user_id):
## Grabbing all the media that exist on this profile
unsorted_media = cl.usertag_medias_v1(user_id)
sorted_media = media_sorter(unsorted_media)
return sorted_media
def combine_media(sorted_photos, sorted_videos):
final_sort = sorted_photos
for key in final_sort:
for values in sorted_videos[key]:
unique = True
for j in final_sort[key]:
if values['id'] == j['id']:
unique = False
if(unique):
final_sort[key].append(values)
return final_sort
def clear_screen():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
if "session.json" in os.listdir():
session_file_exists = True
else:
session_file_exists = False
user_id = False
print('''
@@@ @@@ @@@ @@@ @@@@@@ @@@@@@@@ @@@@@@@@ @@@ @@@
@@@ @@@ @@@@ @@@ @@@@@@@ @@@@@@@@ @@@@@@@@ @@@@ @@@
@@! @@@ @@!@!@@@ !@@ @@! @@! @@!@!@@@
!@! @!@ !@!!@!@! !@! !@! !@! !@!!@!@!
@!@ !@! @!@ !!@! !!@@!! @!!!:! @!!!:! @!@ !!@!
!@! !!! !@! !!! !!@!!! !!!!!: !!!!!: !@! !!!
!!: !!! !!: !!! !:! !!: !!: !!: !!!
:!: !:! :!: !:! !:! :!: :!: :!: !:!
::::: :: :: :: :::: :: :: :::: :: :::: :: ::
: : : :: : :: : : : :: :: : :: :: :: :
''')
print('\n\n')
target = input('Please enter your target: ')
clear_screen()
while(True):
clear_screen()
print('''
@@@ @@@ @@@ @@@ @@@@@@ @@@@@@@@ @@@@@@@@ @@@ @@@
@@@ @@@ @@@@ @@@ @@@@@@@ @@@@@@@@ @@@@@@@@ @@@@ @@@
@@! @@@ @@!@!@@@ !@@ @@! @@! @@!@!@@@
!@! @!@ !@!!@!@! !@! !@! !@! !@!!@!@!
@!@ !@! @!@ !!@! !!@@!! @!!!:! @!!!:! @!@ !!@!
!@! !!! !@! !!! !!@!!! !!!!!: !!!!!: !@! !!!
!!: !!! !!: !!! !:! !!: !!: !!: !!!
:!: !:! :!: !:! !:! :!: :!: :!: !:!
::::: :: :: :: :::: :: :: :::: :: :::: :: ::
: : : :: : :: : : : :: :: : :: :: :: :
''')
print('\n\n')
print(f'Current Target is "{target}"\n\n')
print('Choose one of the following options...')
print('1. Generate Session File and userid')
print('2. Summary of target')
print('3. Find Followers')
print('4. Find Following')
print('5. Download All Media')
print('6. Download All Tagged Media')
print('7. Download All Highlights')
print('8. Exit')
choice = input('Enter Choice: ')
clear_screen()
if target not in os.listdir():
os.mkdir(f"./{target}")
if "posts" not in os.listdir(target):
os.mkdir(f"{target}/posts")
if "tagged_posts" not in os.listdir(target):
os.mkdir(f"{target}/tagged_posts")
if "highlights" not in os.listdir(target):
os.mkdir(f"{target}/highlights")
media_list = ["images","videos","igtv","reels","albums"]
for key in media_list:
if key not in os.listdir(f"{target}/posts"):
os.mkdir(f"{target}/posts/{key}")
for key in media_list:
if key not in os.listdir(f"{target}/tagged_posts"):
os.mkdir(f"{target}/tagged_posts/{key}")
if not choice.isnumeric():
print('Invalid choice')
input('Press Enter to continue...')
continue
if not (1<=int(choice)<=8 ):
print('Invalid choice')
input('Press Enter to continue...')
continue
if(choice == '8'):
break
if(choice == '1'):
if "session.json" not in os.listdir():
if(generate_session()):
print('Session file successfully generated')
session_file_exists = True
cl = Client()
cl.delay_range = [3, 10]
cl.load_settings("session.json")
input('Press Enter to continue...')
print('Generating user id... Might take a few seconds. Try your command again after this process is complete')
user_id = cl.user_id_from_username(target)
print('User ID Generated')
input('Press Enter to continue...')
continue
else:
print('Something went wrong, restart the script')
break
else:
print("Session File Already Found, you can continue. If you do not want to use it, please delete the session.json file. And try again.")
session_file_exists = True
cl = Client()
cl.delay_range = [3, 10]
cl.load_settings("session.json")
input('Press Enter to continue...')
print('Generating user id... Might take a few seconds. Try your command again.')
user_id = cl.user_id_from_username(target)
print('User ID Generated')
input('Press Enter to continue...')
continue
elif(choice == '2' and session_file_exists):
user_info = cl.user_info(user_id)
generate_user_info_file(target, user_info)
user_summary(user_info)
input('Press Enter to continue...')
continue
elif(choice == '3' and session_file_exists and user_id):
print('Might take a few minutes depending upon the target, please be patient.')
user_followers(user_id)
print('Done!')
input('Press Enter to continue...')
continue
elif(choice == '4' and session_file_exists and user_id):
print('Might take a few minutes depending upon the target, please be patient.')
user_following(user_id)
print('Done!')
input('Press Enter to continue...')
continue
elif(choice == '5' and session_file_exists and user_id):
print("Fetching all the media, might take a while.")
final_sort = combine_media(fetch_images_albums(user_id), fetch_videos(user_id))
with open(f"{target}/posts/media.pickle", "ab") as fh:
pickle.dump(final_sort, fh)
with open(f"{target}/posts/media.txt", "w", encoding="utf-8") as outfile:
outfile.write(str(final_sort))
media_downloader(final_sort, target)
print('Done!')
input('Press Enter to continue...')
continue
elif(choice == '6' and session_file_exists and user_id):
print("Fetching all the tagged media, might take a while.")
final_sort = fetch_tagged(user_id)
with open(f"{target}/tagged_posts/media.pickle", "ab") as fh:
pickle.dump(final_sort, fh)
with open(f"{target}/tagged_posts/media.txt", "w", encoding="utf-8") as outfile:
outfile.write(str(final_sort))
media_downloader_tagged(final_sort, target)
print('Done!')
input('Press Enter to continue...')
continue
elif(choice == '7' and session_file_exists and user_id):
print("Fetching all the Highlights, might take a while.")
highlights = cl.user_highlights(user_id)
for count, highlight in enumerate(highlights):
if f"{count+1} {highlight.pk}" not in os.listdir(f"{target}/highlights"):
os.mkdir(f"{target}/highlights/{count+1} {highlight.pk}")
file_path_for_file = os.path.join(f'{target}/highlights/{count+1} {highlight.pk}', 'cover.jpg')
data = requests.get(highlight.cover_media['cropped_image_version']['url']).content
f = open(file_path_for_file,'wb',)
f.write(data)
f.close()
file_path_for_file = os.path.join(f'{target}/highlights/{count+1} {highlight.pk}', f'{count+1} {highlight.pk}.txt')
with open(file_path_for_file, "w", encoding="utf-8") as fh:
fh.write(f"Title: {highlight.title}\n")
fh.write(f"Highlight id is {highlight.pk}\n")
fh.write(f"Created at {highlight.created_at}\n")
fh.write(f"Media Count is {highlight.media_count}\n")
fh.write(f"Cover Image is {highlight.cover_media['cropped_image_version']['url']}\n")
highlight_info = cl.highlight_info(highlight.pk)
#print(highlight_info)
highlight_media = []
for count1,media in enumerate(highlight_info.items):
if media.media_type == 1:
highlight_media.append([count1+1, media.thumbnail_url])
file_path_for_file = os.path.join(f'{target}/highlights/{count+1} {highlight.pk}', f'{count1+1}.jpg')
if not os.path.isfile(file_path_for_file):
data = requests.get(media.thumbnail_url).content
f = open(file_path_for_file,'wb',)
f.write(data)
f.close()
if media.media_type == 2:
highlight_media.append([count1+1, media.video_url])
file_path_for_file = os.path.join(f'{target}/highlights/{count+1} {highlight.pk}', f'{count1+1}.mp4')
if not os.path.isfile(file_path_for_file):
data = requests.get(media.video_url).content
f = open(file_path_for_file,'wb',)
f.write(data)
f.close()
file_path_for_file = os.path.join(f'{target}/highlights/{count+1} {highlight.pk}', f'highlight_media_list.txt')
with open(file_path_for_file, "w", encoding="utf-8") as fh:
for i in highlight_media:
fh.write(str(i))
fh.write('\n')
with open(f"{target}/highlights/media.pickle", "ab") as fh:
pickle.dump(highlight_media, fh)
print('Done!')
input('Press Enter to continue...')
continue
else:
print('Congratulations, you broke the script somehow, contact the main author with screenshots and explanation as to what you did.')