-
Notifications
You must be signed in to change notification settings - Fork 2
/
apod.py
46 lines (40 loc) · 1.5 KB
/
apod.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
import re
import urllib
from PIL import Image
import cStringIO
import requests
from requests_oauthlib import OAuth1Session
# This is the APOD index page
apodbaseurl = 'http://apod.nasa.gov/apod/{}'
# This is how we look for the image on the page
regex = r'a href="(image.*)"'
# You can adjust this but twitter only allows 800k uploads
imgsize = 900, 900
# This our twitter API endpoint for changing the background
twitter_endpoint = 'http://api.twitter.com/1/account/update_profile_background_image.json'
def get_apod_image():
# grab the mainpage
apodpage = urllib.urlopen(apodbaseurl.format('astropix.html')).read()
# find image url
apodurl = re.search(regex, apodpage).group(1)
# open the image file
imgfile = urllib.urlopen(apodbaseurl.format(apodurl))
# parse it into memory (cStringIO is faster than StringIO)
imgstr = cStringIO.StringIO(imgfile.read())
img = Image.open(imgstr)
img.convert("RGB")
# resize preserving aspect ratio
img.thumbnail(imgsize, Image.ANTIALIAS)
# save it in the smallest size possible
img.save("apod.png", "PNG", optimize=True)
def update_twitter():
image = open('apod.png', 'rb')
# create an oauth thingy called "client"
response = client.post(twitter_endpoint, '', params={'tile': True},
files={'image': ('apod.png', image)})
# lets print and return some info for troubleshooting
print response.text
return response
if __name__ == '__main__':
get_apod_image()
update_twitter()