-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapod-dl.py
More file actions
executable file
·139 lines (122 loc) · 4.16 KB
/
apod-dl.py
File metadata and controls
executable file
·139 lines (122 loc) · 4.16 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
# Copyright © 2023, Huy Dao
import requests
import re
import os, time, random
import argparse
import json
from tqdm.auto import tqdm, trange
from bs4 import BeautifulSoup
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
def get_apod(url, adir):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:84.0) Gecko/20100101 Firefox/84.0"
}
sess = requests.Session()
if not os.path.exists(adir):
os.makedirs(adir, exist_ok=False)
parenturl = os.path.split(url)[0]
spaceregex = re.compile(r"\s{2,}")
print(f"getting image from {url}")
apod = sess.get(url, timeout=30, headers=headers, verify=True)
apod.raise_for_status()
apodsoup = BeautifulSoup(apod.text, features="lxml")
apod.close()
imgelem = apodsoup.find_all("a", href=re.compile("^image"))
if imgelem == []:
print("No image link found\n")
else:
imgurl = parenturl + "/" + imgelem[0].get("href")
imgfilename = os.path.basename(imgurl)
imgdate = imgelem[0].find_previous("p").getText(strip=True)
imgtitle = imgelem[0].find_next("b").getText(strip=True)
imgtext = imgelem[0].find_next("p").getText()
imgtext = re.sub("\n", " ", imgtext).strip()
imgtext = re.sub(spaceregex, " ", imgtext)
with open(os.path.join(adir, "album_list.txt"), "at") as albumfd:
print(f'save album list to {os.path.join(adir, "album_list.txt")}')
albumfd.write(
imgdate
+ " - "
+ imgfilename
+ " - "
+ imgtitle
+ " - "
+ imgtext
+ "\n\n"
)
print(f"{imgdate} - {imgurl} --> {os.path.join(adir, imgfilename)}")
if not os.path.exists(os.path.join(adir, imgfilename)):
imageresp = sess.get(
imgurl,
headers=headers,
timeout=30,
cookies=apod.cookies,
stream=True,
verify=True,
)
total = int(imageresp.headers.get("content-length", 0))
print(total)
imageresp.raise_for_status()
with open(os.path.join(adir, imgfilename), "wb") as fd:
for chunk in tqdm(
imageresp.iter_content(chunk_size=1024),
total=total / 1024,
unit="kiB",
unit_scale=True,
unit_divisor=1024,
):
# print(".", end="", flush=True)
fd.write(chunk)
print("\n")
fd.flush()
imageresp.close()
else:
print(f"file {imgfilename} already downloaded\n")
albumfd.flush()
try:
prevlink = parenturl + "/" + apodsoup.find_all("a", string="<")[0].get("href")
except Exception as exc:
print(f"Can not find previous link")
sess.close()
return prevlink
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument(
"-u",
"--url",
required=False,
type=str,
help="start url. Start from this page. If omitted, will start with today's page",
)
ap.add_argument(
"-d",
"--dir",
required=False,
type=str,
help="name of directory to save files to. If omitted, will create and save files to ./apod-images/",
)
ap.add_argument(
"-o",
"--oneday",
required=False,
action="store_true",
help="just get the one day's apod and nothing else",
)
args = ap.parse_args()
if args.url:
url = args.url
else:
url = "https://apod.nasa.gov/apod/astropix.html"
if args.dir:
SAVEDIR = args.dir
else:
SAVEDIR = "apod-images"
if args.oneday:
_ = get_apod(url, SAVEDIR)
else:
while url:
url = get_apod(url, SAVEDIR)
# sleep randomly between 0 to 3 seconds
time.sleep(random.randint(0, 3))