|
| 1 | +import shutil |
| 2 | + |
1 | 3 | import requests
|
2 | 4 |
|
3 | 5 |
|
4 |
| -def get_apod_data(api_key: str) -> dict: |
| 6 | +def get_apod_data(api_key: str, download: bool = False, path: str = ".") -> dict: |
5 | 7 | """
|
6 | 8 | Get the APOD(Astronomical Picture of the day) data
|
7 |
| - Get the API Key from : https://api.nasa.gov/ |
| 9 | + Get your API Key from: https://api.nasa.gov/ |
8 | 10 | """
|
9 |
| - url = "https://api.nasa.gov/planetary/apod/" |
| 11 | + url = "https://api.nasa.gov/planetary/apod" |
10 | 12 | return requests.get(url, params={"api_key": api_key}).json()
|
11 | 13 |
|
12 | 14 |
|
| 15 | +def save_apod(api_key: str, path: str = ".") -> dict: |
| 16 | + apod_data = get_apod_data(api_key) |
| 17 | + img_url = apod_data["url"] |
| 18 | + img_name = img_url.split("/")[-1] |
| 19 | + response = requests.get(img_url, stream=True) |
| 20 | + |
| 21 | + with open(f"{path}/{img_name}", "wb+") as img_file: |
| 22 | + shutil.copyfileobj(response.raw, img_file) |
| 23 | + del response |
| 24 | + return apod_data |
| 25 | + |
| 26 | + |
13 | 27 | def get_archive_data(query: str) -> dict:
|
14 | 28 | """
|
15 | 29 | Get the data of a particular query from NASA archives
|
16 | 30 | """
|
17 |
| - endpoint = "https://images-api.nasa.gov/search" |
18 |
| - return requests.get(endpoint, params={"q": query}).json() |
| 31 | + url = "https://images-api.nasa.gov/search" |
| 32 | + return requests.get(url, params={"q": query}).json() |
19 | 33 |
|
20 | 34 |
|
21 | 35 | if __name__ == "__main__":
|
22 |
| - print(get_apod_data("YOUR API KEY")) |
23 |
| - print( |
24 |
| - get_archive_data("apollo 2011")["collection"]["items"][0]["data"][0][ |
25 |
| - "description" |
26 |
| - ] |
27 |
| - ) |
| 36 | + print(save_apod("YOUR API KEY")) |
| 37 | + apollo_2011_items = get_archive_data("apollo 2011")["collection"]["items"] |
| 38 | + print(apollo_2011_items[0]["data"][0]["description"]) |
0 commit comments