-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtweet.py
102 lines (91 loc) · 4.24 KB
/
tweet.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""Script used to post tweets as @NewHorizonsBot on Twitter."""
try:
from urllib.request import urlopen, urlretrieve
except ImportError: # Darn Python 2
from urllib import urlopen, urlretrieve
import re
from twython import Twython
from astropy import log
from astropy.time import Time
def get_latest_images():
"""Returns a dictionary detailing the images most recently released.
The web page distributing raw images from New Horizons uses javascript
arrays to produce a gallery page, so we need to read in the contents of
those arrays using regular expressions.
"""
# First retrieve the page distributing the images
webpage = urlopen('http://pluto.jhuapl.edu/soc/Pluto-Encounter/index.php')
html = webpage.read()
# Names of the javascript arrays are used to construct the web gallery:
array_names = ['jpegArr', 'UTCArr', 'DescArr',
'TargetArr', 'RangeArr', 'ExpArr']
images = {}
for name in array_names:
images[name] = re.findall(name+r".push\(\"([^\"]+)\"\)", str(html))
return images
def generate_tweet(jpeg, utc, desc, target, myrange, exp):
"""Generates a @NewHorizonsBot tweet.
Returns
-------
(status, image_fn): (str, str)
Status message and filename of the image to upload.
"""
# First create the URL to link to
url = ('http://pluto.jhuapl.edu/soc/Pluto-Encounter/view_obs.php?'
'image={}&utc_time={}&description={}&target={}&range={}&exposure={}'
.format(jpeg, utc, desc, target, myrange, exp))
url = url.replace('<', '<').replace('>', '>').replace(' ', ' ')
# Then create a pretty status message
time_object = Time(utc.replace('<br>', ' ')[0:19])
pretty_time = time_object.datetime.strftime('%b %d, %H:%M:%S UTC')
status = ('#NewHorizons released an image of {}!\n'
'⌚ {}.\n'
'📍 {} from #Pluto.\n'
'🔗 {}\n'.format(target, pretty_time.lstrip("0"), myrange, url))
# Finally, make sure the image we are tweeting is on disk
jpeg_prefix = 'http://pluto.jhuapl.edu/soc/Pluto-Encounter/'
image_fn = '/tmp/newhorizonsbot.jpg'
log.info('Downloading {}'.format(jpeg_prefix + jpeg))
urlretrieve(jpeg_prefix + jpeg, image_fn)
return (status, image_fn)
def post_tweet(status, media_fn):
"""Post media and an associated status message to Twitter."""
import secrets
twitter = Twython(secrets.APP_KEY, secrets.APP_SECRET,
secrets.OAUTH_TOKEN, secrets.OAUTH_TOKEN_SECRET)
upload_response = twitter.upload_media(media=open(media_fn, 'rb'))
response = twitter.update_status(status=status,
media_ids=upload_response['media_id'])
log.info(response)
return twitter, response
if __name__ == '__main__':
# Try to find an image that has not already been tweeted
# Which images have already been tweeted?
try:
IMAGES_TWEETED = [line.strip() for line in
open('images-tweeted.txt').readlines()]
except FileNotFoundError:
log.warning('images-tweeted.txt not found')
IMAGES_TWEETED = []
images = get_latest_images()
# Tweet the most recent image first; never tweet images older than the 24th
for idx in range(0, 24):
archive_filename = images['jpegArr'][idx]
# The same image is sometimes re-posted using a different "_sci_x" suffix
unique_id = archive_filename.split('_sci_')[0]
if unique_id not in IMAGES_TWEETED:
status, image_fn = generate_tweet(jpeg=images['jpegArr'][idx],
utc=images['UTCArr'][idx],
desc=images['DescArr'][idx],
target=images['TargetArr'][idx],
myrange=images['RangeArr'][idx],
exp=images['ExpArr'][idx])
log.info(status)
twitter, response = post_tweet(status, image_fn)
# Remember that we tweeted this image
history = open('images-tweeted.txt', 'a')
history.write(unique_id + '\n')
history.close()
# We're done
break