-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_image_function.py
52 lines (38 loc) · 1.39 KB
/
get_image_function.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
import os
from pathlib import Path
import requests
from fake_useragent import UserAgent
from tqdm import tqdm
ua = UserAgent(verify_ssl=False)
if not os.path.exists("Thumbnails"):
os.makedirs("Thumbnails")
def func_download_thumbnail(json_filename):
print(f"Download Thumbnails: {str(json_filename)}")
json_filename = Path(json_filename)
big_finish_id = json_filename.stem.split("-")[-1]
print(big_finish_id)
image_url = f"https://www.bigfinish.com/image/release/{big_finish_id}/"
image_filename = os.path.join("Thumbnails", f"{json_filename.stem}.jpg")
resp = requests.get(
image_url, stream=True, headers={"User-Agent": ua.chrome}, timeout=30
)
total = int(resp.headers.get("content-length", 0))
if resp.status_code > 200:
print(f"Error downloading image, status code: {resp.status_code}")
return
try:
with open(image_filename, "wb") as file, tqdm(
desc=image_filename,
total=total,
unit="iB",
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in resp.iter_content(chunk_size=1024):
size = file.write(data)
bar.update(size)
except Exception as ex:
print(f"Error downloading file: {image_filename}")
print("Error:", ex)
print("Img:", image_url)
os.remove(image_filename)