Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- '''
- create webpage of moonphase
- update Facebook "The Moon" page when it's a major phase
- '''
- import sys, os, math, ephem, netrc, urllib, urllib2, logging, httplib
- from datetime import datetime
- logging.basicConfig(level = logging.DEBUG)
- IMAGE_PATTERN = 'moons/m%03d.gif' # relative to images/
- FACEBOOK = 'graph.facebook.com'
- NETRC = netrc.netrc()
- THE_MOON_ID, APP, KEY = NETRC.authenticators(FACEBOOK)
- MOONPAGE = 'http://unixshell.jcomeau.com/luna/index.html'
- DOCUMENT_ROOT = os.getenv('DOCUMENT_ROOT', '/var/www/jcomeau/luna')
- GRAPH = 'https://%s' % FACEBOOK
- TIMESTAMP = datetime.utcnow()
- MESSAGE = {
- 0: 'Beginning a new cycle!',
- 90: 'First quarter happening now!',
- 180: 'Full moon happening now!',
- 270: 'Last quarter happening now!',
- }
- def update_image():
- '''
- update symlink images/phase.gif
- use trig function guessed at by observing how new and full moons seem
- to last several days, whereas first and last quarter zoom by quickly
- '''
- os.chdir(DOCUMENT_ROOT)
- utcnow = ephem.Date(TIMESTAMP)
- last_new_moon = ephem.previous_new_moon(utcnow)
- next_new_moon = ephem.next_new_moon(utcnow)
- lunar_cycle = next_new_moon - last_new_moon
- full_moon_offset = lunar_cycle / 2
- current_phase = utcnow - last_new_moon
- if current_phase >= full_moon_offset:
- degrees, image = image_url((math.cos(
- (current_phase / lunar_cycle) * 2 * math.pi) + 3) * 90)
- else:
- degrees, image = image_url((-math.cos(
- (current_phase / lunar_cycle) * 2 * math.pi) + 1) * 90)
- try:
- old_image = os.readlink('images/phase.gif')
- except OSError:
- old_image = ''
- if old_image != image:
- try:
- os.unlink('images/phase.gif')
- except OSError:
- pass
- os.symlink(image, 'images/phase.gif')
- if degrees in [0, 90, 180, 270, 360]:
- update_facebook_page(degrees, text = MESSAGE(degrees % 360))
- def init_https():
- '''
- enable debugging of HTTPS connection if run from command-line
- '''
- handler = urllib2.HTTPSHandler(debuglevel = sys.stdin.isatty())
- opener = urllib2.build_opener(handler)
- urllib2.install_opener(opener)
- def update_facebook_page(degrees, link = MOONPAGE, text = 'My current phase'):
- '''
- make a POST to The Moon page
- notifying followers of major phase
- '''
- init_https()
- arguments = {
- 'link': '%s?cachebreaker=%s' % (link, TIMESTAMP),
- 'message': text,
- 'access_token': KEY,
- }
- if not text:
- arguments.pop('message')
- logging.debug('notifying followers of %d phase' % degrees)
- response = urllib2.urlopen('%s/%s/feed' % (GRAPH, THE_MOON_ID),
- urllib.urlencode(arguments))
- page = response.read()
- logging.debug(page)
- def image_url(number):
- '''
- images are only in multiples of 2 degrees, such as moons/m002.gif,
- moons/m356.gif, so round accordingly and construct relative URL
- '''
- rounded = int((number / 2) + .5) * 2
- return rounded, IMAGE_PATTERN % rounded
- if __name__ == '__main__':
- update_image()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement