From 6d19d28f6e78fe0af404317e0632f917db2517ce Mon Sep 17 00:00:00 2001 From: 1kamma Date: Mon, 1 May 2023 22:56:39 +0300 Subject: [PATCH] prettified the code --- download books in bulks.py | 71 +++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/download books in bulks.py b/download books in bulks.py index 5b3e516..d23e145 100644 --- a/download books in bulks.py +++ b/download books in bulks.py @@ -21,6 +21,8 @@ Books = [] THREADS = [] PATHS = [] OLD_REMOVE = [] +BROWSER_PREFENCES = {"browser.download.folderList": 2, "browser.download.manager.showWhenStarting": False, "browser.download.dir": "ignore", "browser.helperApps.neverAsk.saveToDisk": "attachment/csv, text/plain, application/octet-stream, application/binary, text/csv, application/csv, application/excel, text/comma-separated-values, text/xml, application/xml, application/xls, excel/xls, application/excel 97-2003,application/Microsoft Excel 97-2003 Worksheet, application/vnd.ms-excel", "browser.helperApps.neverAsk.openFile": + "application/PDF, application/FDF, application/XFDF, application/LSL, application/LSO, application/LSS, application/IQY, application/RQY, application/XLK, application/XLS, application/XLT, application/POT application/PPS, application/PPT, application/DOS, application/DOT, application/WKS, application/BAT, application/PS, application/EPS, application/WCH, application/WCM, application/WB1, application/WB3, application/RTF, application/DOC, application/MDB, application/MDE, application/WBK, application/WB1, application/WCH, application/WCM, application/AD, application/ADP, application/vnd.ms-excel", "browser.download.panel.shown": False} def remove_text(text: str): @@ -77,7 +79,7 @@ def do_action_now(index: int): for s in SOURCES[index]: if SOURCES[index][s]: if not os.path.exists(f"ignore/{name}/{files.index(s):04}.jpg"): - with open(f"ignore/{name}/{files.index(s):04}.jpg","wb") as F: + with open(f"ignore/{name}/{files.index(s):04}.jpg", "wb") as F: F.write(urllib3.PoolManager().request("GET", SOURCES[index][s]).data) # files[files.index(s)] = urllib3.PoolManager().request("GET", SOURCES[index][s]).data return files @@ -153,46 +155,37 @@ def open_firefox(url: str): def give_me_web(): options = webdriver.FirefoxOptions() fp = webdriver.FirefoxProfile() - fp.set_preference("browser.download.folderList", 2) - fp.set_preference("browser.download.manager.showWhenStarting", False) - fp.set_preference("browser.download.dir", "ignore") - fp.set_preference( - "browser.helperApps.neverAsk.saveToDisk", - "attachment/csv, text/plain, application/octet-stream, application/binary, text/csv, application/csv, application/excel, text/comma-separated-values, text/xml, application/xml, application/xls, excel/xls, application/excel 97-2003,application/Microsoft Excel 97-2003 Worksheet, application/vnd.ms-excel") - fp.set_preference( - "browser.helperApps.neverAsk.openFile", - "application/PDF, application/FDF, application/XFDF, application/LSL, application/LSO, application/LSS, application/IQY, application/RQY, application/XLK, application/XLS, application/XLT, application/POT application/PPS, application/PPT, application/DOS, application/DOT, application/WKS, application/BAT, application/PS, application/EPS, application/WCH, application/WCM, application/WB1, application/WB3, application/RTF, application/DOC, application/MDB, application/MDE, application/WBK, application/WB1, application/WCH, application/WCM, application/AD, application/ADP, application/vnd.ms-excel") - fp.set_preference("browser.download.panel.shown", False) + for key,val in BROWSER_PREFENCES: + fp.set_preference(key, val) options.add_argument('--lang=EN') options.headless = True fire = "geckodriver" return (fp, fire, options) - -with open("BooksToDownload", "r", encoding="utf_8") as file: - books = file.read().split("\n") - -for b in books: - if b.find('````') > -1 and not b.startswith("#"): - OLD_REMOVE.append(b) - PATHS.append(b[b.rfind('`')+1:]) - b = b[:b.find('`')] - elif not b.startswith("#"): - OLD_REMOVE.append(b) - PATHS.append(None) - t1 = threading.Thread(None, open_firefox, args=(b,)) - t1.start() - THREADS.append(t1) -for t in THREADS: - t.join() -lasts = [] -for i in range(len(ACTS)): - SOURCES[i].update({key.attrs["id"]: "" - for key in bs4.BeautifulSoup(ACTS[i]._driver.page_source, "html.parser").find_all( - "div", attrs={"class": "BV_oImage"})}) - lasts.append(list(SOURCES[i].keys())[-1]) -couters = 0 -treads = len(ACTS)-1 -for i in range(len(ACTS)): - T = threading.Thread(None, act_now, args=(i, PATHS[i])) - T.start() +if __name__ == "__main__": + with open("BooksToDownload", "r", encoding="utf_8") as file: + books = file.read().split("\n") + for b in books: + if b.find('````') > -1 and not b.startswith("#"): + OLD_REMOVE.append(b) + PATHS.append(b[b.rfind('`')+1:]) + b = b[:b.find('`')] + elif not b.startswith("#"): + OLD_REMOVE.append(b) + PATHS.append(None) + t1 = threading.Thread(None, open_firefox, args=(b,)) + t1.start() + THREADS.append(t1) + for t in THREADS: + t.join() + lasts = [] + for i in range(len(ACTS)): + SOURCES[i].update({key.attrs["id"]: "" + for key in bs4.BeautifulSoup(ACTS[i]._driver.page_source, "html.parser").find_all( + "div", attrs={"class": "BV_oImage"})}) + lasts.append(list(SOURCES[i].keys())[-1]) + couters = 0 + treads = len(ACTS)-1 + for i in range(len(ACTS)): + T = threading.Thread(None, act_now, args=(i, PATHS[i])) + T.start() \ No newline at end of file