-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
62 lines (48 loc) · 1.87 KB
/
bot.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
58
59
60
61
62
import tweepy
import datetime
import urllib2
import numpy as np
import time
import lensAPOD
consumer_key = 'TAOrZdVmFkuefvKWs3WM6QQML'
consumer_secret = 'sXujMnXakPJD3v2464VyfNVeHVj8Kxwc1TJ3coAvsb0WqP8dZT'
access_token = '835329970801037312-lmfkZv0y8gW6qu5CKZuumFFdn3E8Ncc'
access_secret = 'QpyPSmFQSAV3z1cNlazb0tWwG6p72wjMedCiduIWBHpAJ'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
file = './pictures/AstroPicOfTheDay_Lensed.jpg'
def run_bot():
# pulling down image and lensing it
EinsteinRadius = lensAPOD.run_lensAPOD()
# finding object title
url = "https://apod.nasa.gov/apod/astropix.html"
response = urllib2.urlopen(url)
the_page = response.read()
page = the_page.split('\n')
centerlines = []
for i,line in enumerate(page):
if ('<center>' in line):
centerlines.append(i)
titlerow_id = centerlines[1]+1
titlerow = page[titlerow_id]
print('titlerow',titlerow)
title = titlerow.split('/')[0][3:-2].strip()
print('title',title)
# day month year for link
now = datetime.datetime.now()
year = '{:02d}'.format(now.year-2000)
month = '{:02d}'.format(now.month)
day = '{:02d}'.format(now.day)
day_month_year = '{}{}{}'.format(year, month, day)
status = "A gravitationally lensed "+title+", with an Einstein radius of "+str(int(EinsteinRadius))+" pixels. #Astronomy #SciCom https://apod.nasa.gov/apod/ap"+day_month_year+'.html'
# status = "A gravitationally lensed #astronomy picture of the day! https://apod.nasa.gov/apod/ap"+day_month_year+'.html'
print(status)
return status
INTERVAL = 60 * 60 * 24 # every 24 hours
# INTERVAL = 60 # every 15 seconds, for testing
while True:
print("About to run...")
status = run_bot()
api.update_with_media(file, status=status)
time.sleep(INTERVAL)