-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnasaAPI.py
57 lines (51 loc) · 2.24 KB
/
nasaAPI.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
53
54
55
56
57
import requests, shutil, os, tweepy
from funcs import isOriginal
from variables import NASA_API_KEY
def Link_format(link): #Se formatea los links de youtube para que muestren una preview en el tweet
if("embed/" in link):
return link.replace("embed/", "watch?v=")
else:
return link
def Apod_fetch():
APOD_ENDPOINT = "https://api.nasa.gov/planetary/apod"
apod = requests.get(APOD_ENDPOINT, params= {"hd": True, "api_key": NASA_API_KEY})
if(apod.status_code == 200):
try:
apod = apod.json()
return {"title": apod["title"], "url": apod["url"], "type": apod["media_type"]}
except:
print("Hubo un error en el pedido de APOD.")
else:
print("Hubo un error en el pedido de APOD. Codigo de error: " + str(apod.status_code))
return None
def Post(api):
print("Se empieza la función de nasaAPI.py: Post(api)")
TIMELINE_ACTUAL= tweepy.Cursor(api.user_timeline, screen_name=api.me().screen_name, tweet_mode="extended").items()
Tweets = []
for x in TIMELINE_ACTUAL:
Tweets.append(x._json["full_text"])
#Pedido a APOD API
APOD = Apod_fetch()
if(isOriginal(APOD["title"], Tweets)):
if(APOD["type"] == "image"):
#Descargar imagen localmente para despues adjuntarla al tweet
res = requests.get(APOD["url"], stream=True)
img_path = "img.jpeg"
with open(img_path, "wb") as out_file:
shutil.copyfileobj(res.raw, out_file)
del res
#Subir tweet con foto
Estado = '"' + APOD["title"] + '" #APOD #Astronomy #Nasa'
api.update_with_media(status= Estado, filename = img_path)
print("Se publico el APOD: " + Estado)
os.remove(img_path)
else:
URL= Link_format(APOD["url"])
Estado = APOD["title"] + " " + URL + " #APOD #Astronomy #Nasa"
api.update_status(status= Estado)
print("Se publico el AVOD: " + Estado + " con el video " + APOD['url'])
else:
print("El APOD de la NASA " + APOD["title"] + " ya fue publicado en el timeline. No se publicó nada ahora mismo.")
del APOD
print("Se termina la función de nasaAPI.py: Post(api)\n")
return None