From 2ef545ca2b77ce40d854d45eb5924588ae7075e7 Mon Sep 17 00:00:00 2001 From: Aurn1ox Date: Sun, 23 Apr 2023 22:23:25 +0530 Subject: [PATCH] feat: added bulk downloading and changed download folder --- README.md | 8 +++--- main.py | 75 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 087c646..0b31f64 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ A command-line tool to download PVPRP packs without being redirected to various 1. Download the source. 2. Install the dependencies via `py -m pip install -r requirements.txt`. 3. Run the script via `py main.py`. -4. Enter the URL of the pack you want to download. -5. Enter `y` or to download the pack, or `n` to just get the URL. -6. You're done! The pack will be in your downloads folder if on Windows, or in the same directory as the script if not. +4. Enter the URLs(or drag them from the website to terminal) of the packs you want to download. +5. Enter `y` (default) to download the packs, or `n` to just get the URLs. +6. You're done! The packs will be in your downloads folder if on Windows, or in a folder called `output` in the script directory. ## Why? -I got redirected when clicking on the main download button. That's it. \ No newline at end of file +I got redirected when clicking on the main download button. That's it. diff --git a/main.py b/main.py index eaa1fd6..94df684 100644 --- a/main.py +++ b/main.py @@ -1,36 +1,61 @@ import bs4, requests, os +url_list = [] -url = input('Enter URL: ') +while True: + #split with & to remove search part of url when dragging to terminal + url = input('Enter URL(leave blank to download): ').split('&')[0] -res = requests.get(url) -res.raise_for_status() + if url == "": + break -soup = bs4.BeautifulSoup(res.text, 'html.parser') -script = soup.find_all('script')[-1] + #create list of urls to download in bulk + url_list.append(url) -#get string between ".attr("href","assets/packs and ");" -partial_link = script.string.split('.attr("href","assets/packs')[1].split('")')[0] -partial_link = partial_link.split('?')[0] +if len(url_list) == 0: + print("No packs to download") + exit() -link = f'https://pvprp.com/assets/packs{partial_link}' -filename = link.split('/')[-1] -download = input('Download? (y/n): ').strip().lower().startswith('y') +#get windows username +if os.name == 'nt': + username = os.getlogin() + os.chdir(f'C:\\Users\\{username}\\Downloads') +else: + os.mkdir("./output") + os.chdir("./output") -if download: - res = requests.get(link) - res.raise_for_status() +# make the script download the pack by default +download = input(f'Download {len(url_list)} packs? (Y/n): ').strip().lower().startswith('n') - #get windows username - - if os.name == 'nt': - username = os.getlogin() - os.chdir(f'C:\\Users\\{username}\\Downloads') +for i in range(0, len(url_list)): + # prevent crash from a wrong url + try: + res = requests.get(url_list[i]) + res.raise_for_status() - with open(filename, 'wb') as f: - f.write(res.content) - f.close() + soup = bs4.BeautifulSoup(res.text, 'html.parser') + script = soup.find_all('script')[-1] + + #get string between ".attr("href","assets/packs and ");" + partial_link = script.string.split('.attr("href","assets/packs')[1].split('")')[0] + partial_link = partial_link.split('?')[0] + + link = f'https://pvprp.com/assets/packs{partial_link}' + filename = link.split('/')[-1] + + # defaults to downloading + if download: + print(f'Direct link: {link}') + else: + res = requests.get(link) + res.raise_for_status() + + + with open(filename, 'wb') as f: + f.write(res.content) + f.close() + + print(f'Downloaded {filename} to {os.getcwd()}') + except: + print(f"Invalid URL {url_list[i]}") - print(f'Downloaded {filename} to {os.getcwd()}') -else: - print(f'Direct link: {link}') \ No newline at end of file