Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added bulk downloading and changed download folder #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
I got redirected when clicking on the main download button. That's it.
75 changes: 50 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
@@ -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}')