forked from serhattsnmz/turtle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle_console.py
291 lines (247 loc) · 9.09 KB
/
turtle_console.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- coding: utf-8 -*-
from getpass import getpass
from colorama import init, deinit, Fore
from termcolor import colored, cprint
from turtle import Turtle, Driver, Download_Choice
import os
import json
import platform
import argparse
print_red = lambda x, y="\n" : cprint(x, "red", end=y)
print_green = lambda x, y="\n" : cprint(x, "green", end=y)
print_magenta = lambda x, y="\n" : cprint(x, "magenta", end=y)
print_yellow = lambda x, y="\n" : cprint(x, "yellow", end=y)
print_cyan = lambda x, y="\n" : cprint(x, "cyan", end=y)
def parse_args():
parser = argparse.ArgumentParser(description="Fetch all the lectures for a Instagram")
parser.add_argument("-u", "--username", metavar="", help="User username")
parser.add_argument("-p", "--password", metavar="", help="User password")
parser.add_argument("-d", "--driver", metavar="", type=int, choices=[1,2,3], help="Choosen Driver. [1]PantomJS [2]Chrome [3]Firefox")
parser.add_argument("-P", "--path", metavar="", help="The path for saving photos.")
parser.add_argument("-l", "--list", metavar="", help="List of Usernames")
parser.add_argument("-D", "--download", metavar="", type=int, choices=[1,2], help="Download choice. [1]Update(Default for list) [2]Full")
parser.add_argument("-v", "--video", metavar="", choices=["True", "False"], default="False", help="Download videos or not. [True]Download [False] Do Not Download(Default)")
global args
args = parser.parse_args()
def clear_screen():
plt = platform.system()
if plt == "Windows":
os.system("cls")
elif plt == "Linux":
os.system("clear")
def create_config_if_not_exist():
if not os.path.isfile("config.json"):
try:
data = {"username" : "", "password" : "", "path" : "pictures"}
text = json.dumps(data)
with open("config.json", "w") as file:
file.write(text)
except:
print_red("Config file could not be created!")
print_yellow("Try run this script as root.")
return
def header():
print()
print_magenta("----------------------------------", "\n\n")
print_magenta("### Instagram Photo Downloader ###", "\n\n")
print_magenta("----------------------------------")
print()
def line():
print_magenta("-----------------")
def read_json():
if not os.path.isfile("config.json"):
return None
else:
try:
with open("config.json") as data_file:
data = json.load(data_file)
return data
except:
return None
def get_username():
if args.username:
return args.username
else:
config = read_json()
if config and config["username"] != "":
return config["username"]
else:
return input("Username : ")
def get_password():
if args.password:
return args.password
else:
config = read_json()
if config and config["password"] != "":
return config["password"]
else:
return getpass("Password : ")
def get_path():
if args.path:
return args.path
else:
config = read_json()
if config and config["path"] != "":
return config["path"]
else:
return "pictures"
def choose_driver():
if args.driver == 1:
driver = Driver.PHANTOM
print_cyan("Driver : PhantomJS")
elif args.driver == 2:
driver = Driver.CHROME
print_cyan("Driver : Chrome")
elif args.driver == 3:
driver = Driver.FIREFOX
print_cyan("Driver : Firefox")
else:
config = read_json()
if config and "driver" in config and config["driver"] in [1,2,3]:
_dri = config["driver"]
if _dri == 1:
print_cyan("Driver : PhantomJS")
return Driver.PHANTOM
elif _dri == 2:
print_cyan("Driver : Chrome")
return Driver.CHROME
elif _dri == 3:
print_cyan("Driver : Firefox")
return Driver.FIREFOX
while True:
print_cyan("CHOOSE DRIVER : [1]PhantomJS [2]Chrome [3]Firefox")
d_choice = input("Driver : ")
if d_choice == "1":
driver = Driver.PHANTOM
break
elif d_choice == "2":
driver = Driver.CHROME
break
elif d_choice == "3":
driver = Driver.FIREFOX
break
return driver
def get_download_choice():
if args.list:
if args.download and args.download == 2:
return Download_Choice.DOWNLOAD_ALL, 0
else:
return Download_Choice.UPDATE, 0
elif args.download:
if args.download == 2:
return Download_Choice.DOWNLOAD_ALL, 0
elif args.download == 1:
return Download_Choice.UPDATE, 0
else:
while True:
print("How many stories do you want to download?")
print_magenta("Give number ", "")
print_cyan("-> For downloading the number of stories")
print_magenta("Give '0' ", "")
print_cyan("-> For downloading all stroies")
print_magenta("Live empty ", "")
print_cyan("-> For downloading last stories you do not have")
choice = input("Give input : ")
if choice == "":
return Download_Choice.UPDATE, 0
else:
if RepresentsInt(choice):
int_choice = int(choice)
if int_choice == 0:
return Download_Choice.DOWNLOAD_ALL, 0
elif int_choice > 0:
return Download_Choice.SOME, int_choice
else:
print_red("It must be bigger than zero!")
else:
print_red("It must be number or empty!")
def get_video_choice():
if args.video and args.video == "False":
return False
else:
return True
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
def core():
# Config
parse_args()
create_config_if_not_exist()
init()
clear_screen()
# Header
header()
# Get info
driver = choose_driver()
username = get_username()
password = get_password()
path = get_path()
vid_choice = get_video_choice()
line()
# Start Normal Download
if not args.list:
t = Turtle()
t.set_path(path)
t.open(driver)
sign_result = t.sign_in(username, password)
line()
if sign_result:
# Get user links
pic_user = input("Username for Photos : ")
folder_name = input("Folder name (Leave empty for Instagram username) : ")
link_result = t.get_img_links(pic_user)
line()
if link_result:
# Download pictures
down_choice, count = get_download_choice()
t.download_photos(folder_name, down_choice, count, vid_choice)
t.close()
# List Download
else:
# Get Json list
json_path = ""
if os.path.exists(args.list):
json_path = args.list
else:
print_red("Json path does not exist!")
return False
# Open Json list
with open(json_path, encoding="utf-8") as file:
data = json.load(file)
# Instance Turtle and start driver
t = Turtle()
t.set_path(path)
t.open(driver)
sign_result = t.sign_in(username, password)
line()
if sign_result:
report = []
# pic_user loop start
for pic_user_item in data:
# Get user links
link_result = t.get_img_links(pic_user_item[1])
line()
# Download pictures
down_choice, count = get_download_choice()
total_download = t.download_photos(pic_user_item[0], down_choice, count, vid_choice)
line()
# Add download result to Log file
if t.result:
t.log.append("### RESULT ### TRUE ### " + pic_user_item[0])
report.append([pic_user_item[0], True, total_download])
else:
t.log.append("### RESULT ### FALSE ### " + pic_user_item[0])
report.append([pic_user_item[0], False, total_download])
t.log.append("-------------------------------")
# Add all user report to Log file
t.log.append("$$ ALL DOWNLOAD RESULT $$")
t.log.append("-------------------------")
for item in report:
t.log.append("$$ " + str(item[1]) + " $$ Total Download : " + str(item[2]) + " $$ " + str(item[0]))
t.log.append("-------------------------")
t.close()
if __name__ == "__main__":
core()