-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnasa.py
41 lines (30 loc) · 1.14 KB
/
nasa.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
import os
import requests
from datetime import datetime, timedelta
async def fetch_pic(date):
api_key = os.environ['NASA_API']
if date:
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}&date={date}"
else:
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
# fetching data
response = requests.get(url)
data = response.json()
date = data["date"]
title = data["title"]
image_url = data["url"]
hd_url = data["hdurl"] if "hdurl" in data.keys() else data["url"]
description = data["explanation"]
if hd_url == image_url:
result = f"Date: {date}\n\n{title}\nURL to image: {image_url}\n\n{description}"
else:
result = f"Date: {date}\n\n{title}\nURL to image: <{image_url}>\nHigh Resolution Image: {hd_url}\n\n{description}"
return (result,date)
def date_changer(date: str, command: str):
current = datetime.strptime(date, '%Y-%m-%d')
if command == '<':
current -= timedelta(days=1)
return current.strftime('%Y-%m-%d')
else:
current += timedelta(days=1)
return current.strftime('%Y-%m-%d')