From 170a818259d1f60fd4893075b4296d93f8ecebf7 Mon Sep 17 00:00:00 2001 From: Jaydeep Das Date: Sat, 23 Oct 2021 16:32:01 +0530 Subject: [PATCH 1/4] Added a feature to download images. --- web_programming/nasa_data.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/web_programming/nasa_data.py b/web_programming/nasa_data.py index 9b15c38a7f05..1de16e98a03d 100644 --- a/web_programming/nasa_data.py +++ b/web_programming/nasa_data.py @@ -1,13 +1,23 @@ +import shutil import requests -def get_apod_data(api_key: str) -> dict: +def get_apod_data(api_key: str, download: bool = False, path: str = ".") -> dict: """ Get the APOD(Astronomical Picture of the day) data Get the API Key from : https://api.nasa.gov/ """ url = "https://api.nasa.gov/planetary/apod/" - return requests.get(url, params={"api_key": api_key}).json() + data = requests.get(url, params={"api_key": api_key}).json() + if download: + img_url = data["url"] + img_name = img_url.split("/")[-1] + response = requests.get(img_url, stream=True) + + with open(f"{path}/{img_name}", "wb+") as img_file: + shutil.copyfileobj(response.raw, img_file) + del response + return data def get_archive_data(query: str) -> dict: From 9401cf87c5b5ff8fd78f5d655b00d30ea6b0b9b4 Mon Sep 17 00:00:00 2001 From: Jaydeep Das Date: Sat, 23 Oct 2021 17:02:02 +0530 Subject: [PATCH 2/4] Minor changes --- web_programming/nasa_data.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web_programming/nasa_data.py b/web_programming/nasa_data.py index 1de16e98a03d..caf732432d75 100644 --- a/web_programming/nasa_data.py +++ b/web_programming/nasa_data.py @@ -1,4 +1,5 @@ import shutil + import requests @@ -29,7 +30,7 @@ def get_archive_data(query: str) -> dict: if __name__ == "__main__": - print(get_apod_data("YOUR API KEY")) + print(get_apod_data("YOUR API KEY", download=True, path="/home/Pictures")) print( get_archive_data("apollo 2011")["collection"]["items"][0]["data"][0][ "description" From 653ce2c584dfe06aafd07f422ade913ecd2c410f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 14:28:11 +0200 Subject: [PATCH 3/4] Update nasa_data.py --- web_programming/nasa_data.py | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/web_programming/nasa_data.py b/web_programming/nasa_data.py index caf732432d75..622e789892e0 100644 --- a/web_programming/nasa_data.py +++ b/web_programming/nasa_data.py @@ -6,33 +6,33 @@ def get_apod_data(api_key: str, download: bool = False, path: str = ".") -> dict: """ Get the APOD(Astronomical Picture of the day) data - Get the API Key from : https://api.nasa.gov/ + Get your API Key from: https://api.nasa.gov/ """ - url = "https://api.nasa.gov/planetary/apod/" - data = requests.get(url, params={"api_key": api_key}).json() - if download: - img_url = data["url"] - img_name = img_url.split("/")[-1] - response = requests.get(img_url, stream=True) + url = "https://api.nasa.gov/planetary/apod" + return requests.get(url, params={"api_key": api_key}).json() - with open(f"{path}/{img_name}", "wb+") as img_file: - shutil.copyfileobj(response.raw, img_file) - del response - return data + +def save_apod(api_key: str, path: str = ".") -> dict: + apod_data = get_apod_data(api_key): + img_url = apod_data["url"] + img_name = img_url.split("/")[-1] + response = requests.get(img_url, stream=True) + + with open(f"{path}/{img_name}", "wb+") as img_file: + shutil.copyfileobj(response.raw, img_file) + del response + return apod_data def get_archive_data(query: str) -> dict: """ Get the data of a particular query from NASA archives """ - endpoint = "https://images-api.nasa.gov/search" - return requests.get(endpoint, params={"q": query}).json() + url = "https://images-api.nasa.gov/search" + return requests.get(url, params={"q": query}).json() if __name__ == "__main__": - print(get_apod_data("YOUR API KEY", download=True, path="/home/Pictures")) - print( - get_archive_data("apollo 2011")["collection"]["items"][0]["data"][0][ - "description" - ] - ) + print(save_apod("YOUR API KEY")) + apollo_2011_items = get_archive_data("apollo 2011")["collection"]["items"] + print(apollo_2011_items[0]["data"][0]["description"]) From 96e0223e35298401e012bbaf08528ca0ab28957b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 14:31:35 +0200 Subject: [PATCH 4/4] : --- web_programming/nasa_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/nasa_data.py b/web_programming/nasa_data.py index 622e789892e0..c0a2c4fdd1a7 100644 --- a/web_programming/nasa_data.py +++ b/web_programming/nasa_data.py @@ -13,7 +13,7 @@ def get_apod_data(api_key: str, download: bool = False, path: str = ".") -> dict def save_apod(api_key: str, path: str = ".") -> dict: - apod_data = get_apod_data(api_key): + apod_data = get_apod_data(api_key) img_url = apod_data["url"] img_name = img_url.split("/")[-1] response = requests.get(img_url, stream=True)