-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (47 loc) · 1.71 KB
/
app.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
from pathlib import Path
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests
import os
import random
import concurrent.futures
print("Example url format: https://papers.xtremepape.rs/CAIE/IGCSE/Mathematics%20-%20Additional%20(0606)/")
print("Enter url: ")
url = input()
print("""Example path format:"/Users/akshat/Documents/past-paper-downloader/extreme-add""")
print("Enter path: ")
pathnam = input()
# If there is no such folder, the script will create one automatically
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
files = []
for link in soup.select("a[href$='.pdf']"):
# Name the pdf files using the last portion of each link which are unique in this case
link_str = str(link)
if (link_str.find('qp') == -1):
continue
filename = os.path.join(
pathnam, link['href'].split('/')[-1])
path = Path(filename)
if (path.is_file()):
ff = open(filename, "rb")
if not (ff.read() == b''):
continue
files.append(
{"filename": filename,
"link": f"{url}/{link['href']}"})
# print('doing', files[-1])
def download_files(fss):
print(f"Downloading {len(fss)} files")
for filename in fss:
with open(filename["filename"], 'wb') as f:
f.write(requests.get(filename["link"]).content)
def download_file(file):
with requests.get(file["link"], stream=True) as r:
r.raise_for_status()
with open(file["filename"], 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(download_file, files)