-
Notifications
You must be signed in to change notification settings - Fork 0
/
itch-batch-downloader.py
563 lines (465 loc) · 28.5 KB
/
itch-batch-downloader.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"""
Based on https://github.com/shakeyourbunny/itch-downloader
"""
import configparser
import json
import os
import sys
import time
from http.cookiejar import MozillaCookieJar
import dateparser
import requests
import requests.cookies
from bs4 import BeautifulSoup
import dltool
import unicodedata
import re
import pickle
from datetime import datetime
import subprocess
import codecs
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.expected_conditions import staleness_of
import util
import pathlib
from pathlib import Path
import glob
import base64
import yt_dlp
import traceback
from colorama import Fore
from colorama import Style
version = "0.1.0"
# used to count if a certain page received an update in terms of contents. if it did this is used for creating a new version of
# the .png and .pdf files
global newDownloads
# yt-dlp logger class, used to make the yt-dlp output formatted the same as the rest of the logs
class ydLogger:
def debug(self, msg):
# For compatibility with youtube-dl, both debug and info are passed into debug
# You can distinguish them by the prefix '[debug] '
if msg.startswith('[debug] '):
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] External links downloader: " + msg)
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] External links downloader: " + msg)
def info(self, msg):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] External links downloader: " + msg)
def warning(self, msg):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.YELLOW}[WARNING]{Style.RESET_ALL} External links downloader: " + msg)
def error(self, msg):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} External links downloader: " + msg)
# yt-dlp hook
def yd_hook(d):
if d['status'] == 'finished':
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] External links downloader: done downloading, now post-processing ...")
def local_file_sanity_check(localfile, localsize, localdate, remotesize, remotedate):
if not os.path.isfile(localfile):
return False
if localsize != remotesize:
return False
# date is a string, has to be converted to timestamp.
if localdate != remotedate:
return False
# fall through, at this point the conditions above are satisfied
return True
def slugify(value, allow_unicode=False, is_value_a_filename=False):
"""
Taken from https://github.com/django/django/blob/master/django/utils/text.py
and modified to prevent the fuction to interfere with the file extension
and to specify if the input is a filename or a generic string
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace, dashes, and underscores.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
if is_value_a_filename:
fext = pathlib.Path(value).suffix
fname = Path(value).stem
value = re.sub(r'[^\w\s-]', '', fname.lower())
value = re.sub(r'[-\s]+', '-', value).strip('-_')
fext = re.sub(r'[^\w\s-]', '', fext.lower())
fext = re.sub(r'[-\s]+', '-', fext).strip('-_')
if fext is None or fext.strip() == "":
pass
else:
value = value + "." + fext
else:
value = value.strip()
return value
def fetch_upload(uploads_soup, dlurl, session, params, csfrtoken, gamedirectory, fileNr):
# returns true if the file was downloaded
try:
downloadid = uploads_soup.find("a")["data-upload_id"]
except:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.YELLOW}[WARNING]{Style.RESET_ALL} Skipped a file: {dlurl}")
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.YELLOW}[WARNING]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.YELLOW}[WARNING]{Style.RESET_ALL} =====================================")
return False
dlurl_final = dlurl + "/file/" + downloadid
dlj = session.post(dlurl_final, params=params, data=csfrtoken).json()
if dlj["url"].split("/")[2] == "w3g3a5v6.ssl.hwcdn.net":
# remote file check
x = 0
while x < 3:
try:
dlhead = session.head(dlj["url"])
dldate = dlhead.headers["last-modified"]
if "content-disposition" in dlhead.headers:
dlfilename = dlhead.headers["content-disposition"].split('"')[1]
else:
dlfilename = dlj["url"].split("?")[0].split("/")[-1]
dlsize = dlhead.headers["content-length"]
x = 4
except:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Connection error. Retry " + str(x+1) + " of 3")
x = x + 1
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
finally:
if x == 3:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Cannot recover from connection error. Quitting ...")
sys.exit(1)
# make filename unique and unicode compatible
dlfilename = str(fileNr) + "_" + slugify(dlfilename, False, True)
# local preparation
fulldldir = os.path.join(config["DEFAULT"]["download_directory"], gamedirectory)
os.makedirs(fulldldir, exist_ok=True)
fulldname = os.path.join(fulldldir, dlfilename)
# rename files if exist
suf = pathlib.Path(dlfilename).suffix
suf.strip()
if len(suf) > 0:
if (suf[0] == "."):
suf = suf[1:]
if not suf or suf == "":
newdlname = dlfilename + "_{}".format(dateparser.parse(dldate).strftime("%Y%m%d"))
else:
newdlname = dlfilename.replace("." + suf, "_{}.{}".format(dateparser.parse(dldate).strftime("%Y%m%d"), suf))
newfulldname = os.path.join(fulldldir, newdlname)
# old format
if os.path.isfile(fulldname):
if os.path.exists(fulldname):
if os.path.exists(newfulldname):
os.remove(newfulldname)
os.rename(fulldname, newfulldname)
fulldname = newfulldname
# new filename with date stamp
fulldname = newfulldname
# do the download
wasTheFileDownloaded = False
downloadRetries = 0
while downloadRetries < 3:
try:
if config["DEFAULT"]["debug_logs"] == "ON":
debugon = True
wasTheFileDownloaded = dltool.download_a_file(dlj["url"], filename=fulldname, session=session, debugon=debugon)
downloadRetries = 4
else:
debugon = False
wasTheFileDownloaded = dltool.download_a_file(dlj["url"], filename=fulldname, session=session, debugon=debugon)
downloadRetries = 4
except:
wasTheFileDownloaded = False
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL}" + " Error while downloading - url: {}, filename: {}".format(dlj["url"], fulldname))
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL}" + " Retry " + str(downloadRetries + 1) + " of 3")
downloadRetries = downloadRetries + 1
finally:
if downloadRetries == 3:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Cannot recover from connection error. Quitting ...")
sys.exit(1)
return wasTheFileDownloaded
else:
dldomain = dlj["url"].split("/")[2]
tempUrl = dlj["url"]
if dldomain == "drive.google.com":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.YELLOW}[WARNING]{Style.RESET_ALL} Download from Google Drive is UNSUPPORTED: {tempUrl}")
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.YELLOW}[WARNING]{Style.RESET_ALL} Skipped a file: {tempUrl}")
return False
def main(config):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Download directory is '{}'".format(config["DEFAULT"]["download_directory"]))
time.sleep(3)
# basic setup
session = requests.Session()
cookiejar = requests.cookies.RequestsCookieJar()
cookies = MozillaCookieJar(config["DEFAULT"]["cookie_file"])
cookies.load(ignore_expires=True, ignore_discard=True)
cookiejar.update(cookies)
session.cookies = cookiejar
os.makedirs(config["DEFAULT"]["download_directory"], exist_ok=True)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Loading and parsing my claimed purchases: ", end='')
mypurchases_url = "https://itch.io/my-purchases"
pagecounter = 1
gamelist = list()
r = session.get(mypurchases_url)
if r.status_code == 200:
if r.url != mypurchases_url:
print("") # carriage return
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Not properly authenticated, please provide cookies.")
sys.exit(1)
# parse first page
soup_gamepage = BeautifulSoup(r.text, "html.parser").find_all("div", class_="game_cell_data")
for game in soup_gamepage:
gtitle = slugify(game.find("a", class_="title game_link").text)
gurl = game.find("a", class_="button")["href"]
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] Item found - " + "Title: " + gtitle + ". url: " + gurl)
platform_soup = game.find("div", class_="game_platform")
gamelist.append(
{
"title": slugify(gtitle),
"dlurl": gurl
}
)
soup_nextpage = BeautifulSoup(r.text, "html.parser").find("div", attrs={"class": "next_page forward_link"})
while soup_nextpage:
pagecounter = pagecounter + 1
print(".", flush=True, end="")
r = session.get(mypurchases_url + "?page={}".format(pagecounter))
if r.status_code != 200:
print(" [{}]".format(pagecounter), flush=True)
break
soup_gamepage = BeautifulSoup(r.text, "html.parser").find_all("div", class_="game_cell_data")
for game in soup_gamepage:
gtitle = slugify(game.find("a", class_="title game_link").text)
gurl = game.find("a", class_="button")["href"]
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] Item found - " + "Title: " + gtitle + ". url: " + gurl)
platform_soup = game.find("div", class_="game_platform")
gamelist.append(
{
"title": slugify(gtitle),
"dlurl": gurl
}
)
soup_nextpage = BeautifulSoup(r.text, "html.parser").find("div", attrs={"class": "next_page forward_link"})
print("") # carriage return
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] {} items found.".format(len(gamelist)))
trackfile = os.path.join(config["DEFAULT"]["download_directory"], "itch-batch-downloader-track.txt")
trackNum = 0
if os.path.exists(trackfile):
with open(trackfile, "r", encoding="utf-8") as f:
trackNum = json.loads(f.read())
f.close()
os.remove(trackfile)
numGames = len(gamelist)
curGame = 0
for g in gamelist:
curGame = curGame + 1
if curGame >= trackNum:
print("")
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Analysing item {} of {}. Title: ".format(curGame, numGames) + slugify(g["title"]))
# trackfile
with open(trackfile, "w", encoding="utf-8") as f:
json.dump(curGame, f)
f.close()
r = session.get(g["dlurl"])
if r.status_code == 200:
dlpage_soup = BeautifulSoup(r.text, "html.parser")
dlpage_dlbuttons = dlpage_soup.find_all("a", class_="button download_btn")
dlurl = g['dlurl'].rsplit("/", 2)[0]
gamedirectory = g["dlurl"].split("/")[3]
paramPost = {"source": "game_download", "key": g["dlurl"].split("/")[5]}
csfrToken = dlpage_soup.find("meta", attrs={"name": "csrf_token"})["value"]
uploads = dlpage_soup.find("div", class_="upload_list_widget").find_all(class_="upload")
newDownloads = False
fileNr = 1
for u in uploads:
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] fetch_upload - " + "u: " + str(u) + ". durl: " + str(dlurl) + ". session: " + str(session) + ". paramPost: " + str(paramPost) + ". csfrToken: " + str(csfrToken) + ". gamedirectory: " + str(gamedirectory) + ". fileNr: " + str(fileNr))
newDownloadsTemp = fetch_upload(u, dlurl, session, paramPost, csfrToken, gamedirectory, fileNr)
fileNr = fileNr + 1
if not newDownloads:
newDownloads = newDownloadsTemp
# product webpage screenshot is exported to image and PDF
URL = "https://" + g["dlurl"].split("/")[2] + "/" + g["dlurl"].split("/")[3]
now = datetime.now()
dateyearmonthday = now.strftime("%Y%m%d")
# selenium 4
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--disable-3d-apis")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-web-security')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
driver.cookies = cookiejar
try:
driver.get(URL)
except:
driver.get(URL, verify=False)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Cannot verify domain, connection insicure")
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
try:
util.wait_until_images_loaded(driver, 30)
except TimeoutException:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Timeout error on: " + URL)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
try:
height = 1024 + driver.execute_script("return document.documentElement.scrollHeight;")
width = 1024 + driver.execute_script("return document.body.offsetWidth;")
driver.set_window_size(width, height)
driver.maximize_window()
# product webpage screenshot is exported to image
download_dir = os.path.join(os.path.abspath(config["DEFAULT"]["download_directory"]), gamedirectory)
fullscrname = os.path.join(download_dir, gamedirectory + "_webpage_screenshot_" + dateyearmonthday + ".png")
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Taking screenshot and creating PDF with Selenium, width: " + str(width) + ", height: " + str(height))
try:
dir_check = Path(download_dir)
if not dir_check.is_dir():
os.mkdir(download_dir)
screenshotsFound = 0
for name in glob.glob(os.path.join(download_dir, gamedirectory + "_webpage_screenshot_" + "*" + ".png")):
screenshotsFound = screenshotsFound + 1
if newDownloads or screenshotsFound <= 0:
if config["DEFAULT"]["create_png"] == "ON":
debugon = True
util.fullpage_screenshot(driver, fullscrname, debugon)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Screenshot taken: " + fullscrname)
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Screenshot creation disabled as config setting not equal to ON")
else:
if config["DEFAULT"]["create_png"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Recent screenshot exists. Skipped.")
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Screenshot creation disabled as config setting not equal to ON")
except:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Error while writing file: " + fullscrname)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
# product webpage screenshot is exported to pdf
fullpdfname = os.path.join(download_dir, gamedirectory + "_webpage_screenshot_" + dateyearmonthday + ".pdf")
try:
pdfsFound = 0
for name in glob.glob(os.path.join(download_dir, gamedirectory + "_webpage_screenshot_" + "*" + ".pdf")):
pdfsFound = pdfsFound + 1
if newDownloads or pdfsFound <= 0:
if config["DEFAULT"]["create_pdf"] == "ON":
calculated_print_options = {
'landscape': True,
'displayHeaderFooter': True,
'printBackground': True,
'preferCSSPageSize': False,
'shrinkToFit': True,
'paper_width': '46.81', 'paper_height': '33.11',
}
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
urlpdf = driver.command_executor._url + resource
cmd = "Page.printToPDF"
body = json.dumps({'cmd': cmd, 'params': calculated_print_options})
response = driver.command_executor._request('POST', urlpdf, body)
if not response:
try:
raise Exception(response.get('value'))
except Exception as error:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[ERROR] " + "PDF generation get response: ")
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
else:
result = response.get('value')
with open(fullpdfname, 'wb') as file:
file.write(base64.b64decode(result['data']))
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] PDF created: " + fullpdfname)
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] PDF creation disabled as config setting not equal to ON")
else:
if config["DEFAULT"]["create_pdf"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Recent PDF exists. Skipped.")
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] PDF creation disabled as config setting not equal to ON")
except:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Error while writing file: " + fullpdfname)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
traceback.print_exc()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} =====================================")
# product videos are downloaded (youtube, vimeo, etc)
if config["DEFAULT"]["download_videos"] == "ON":
ydl_opts = {
'ignoreerrors': True,
'outtmpl': os.path.join(download_dir, gamedirectory + "_" + '%(id)s.%(ext)s'),
'logger': ydLogger(),
'progress_hooks': [yd_hook],
}
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Downloading page: {}".format(URL))
res = requests.get(URL)
if config["DEFAULT"]["debug_logs"] == "ON":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + f"Got back response: {res.status_code}")
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + f"Page length: {len(res.text)}")
pageHtml = res.text
bsParser = BeautifulSoup(pageHtml, features='html.parser')
hrefs = bsParser.find_all('iframe')
for href in hrefs:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Found URL: {}".format(href.get("src")))
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
error_code = ydl.download("https:" + href.get("src"))
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Video downloads disabled as config setting not equal to ON")
finally:
newDownloads = False
driver.quit()
else:
tempUrl = g["dlurl"]
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Could not access download page {tempUrl}")
# trackfile
with open(trackfile, "w", encoding="utf-8") as f:
json.dump(0, f)
f.close()
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + f"{Fore.RED}[ERROR]{Style.RESET_ALL} Could not access {mypurchases_url} properly [{r.status_code}]")
if __name__ == "__main__":
print("itch-batch-downloader.py {} (c) 2022 mukkino".format(version))
print("")
config = configparser.ConfigParser()
# initialize and load defaults
configfile = "itch-batch-downloader.ini"
config['DEFAULT'] = {
"download_directory": "Downloads",
"cookie_file": "cookies-itch.txt",
"create_pdf": "ON",
"create_png": "ON",
"download_videos": "ON",
"debug_logs": "OFF"
}
if not os.path.isfile(configfile):
with open(configfile, "w", encoding="utf-8") as f:
config.write(f)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[INFO] Created new configuration. please edit {}".format(configfile))
if sys.platform == "win32":
x = input("Press ENTER to exit")
sys.exit(1)
config.read(configfile)
main(config)
newDownloads = 0
print("")
print("Done")
if sys.platform == "win32":
x = input("Press ENTER to exit")