Source

scatter-share / demos / whenwheretools.py

Full commit
# -- %< --
# From geo.py

import sys, urllib
import simplejson

GEOLOCATION_SERVICE = 'http://labs.zepheira.com:8880/geolookup.json'
GEOLOCATION_SERVICE = 'http://localhost:8880/geolookup.json'

GEOCACHE = {}

def geolookup(place):
    if isinstance(place, unicode):
        place = place.encode('utf-8')
    if place in GEOCACHE:
        return GEOCACHE[place]
    query = urllib.urlencode({'place' : place})
    url = GEOLOCATION_SERVICE + '?' + query
    response = urllib.urlopen(url)
    result = response.read()
    try:
        latlong = simplejson.loads(result).itervalues().next()
        GEOCACHE[place] = latlong
        return latlong
    except ValueError:
        print >> sys.stderr, "Not found:", place
        GEOCACHE[place] = None
        return None

# -- %< --
# From temporal.py
import datetime
import feedparser

# To be moved to amara lib
# Reuses some code from: http://seehuhn.de/blog/52
class timezone(datetime.tzinfo):
    def __init__(self, name="+0000"):
        self.name = name
        seconds = int(name[:-2])*3600+int(name[-2:])*60
        self.offset = datetime.timedelta(seconds=seconds)

    def utcoffset(self, dt):
        return self.offset

    def dst(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return self.name


UTC = timezone()

def mods_convention_date(d):
    #Feedparser extension to parse a date format idiosyncratic to MODS
    if len(d) == 14:
        try:
            #FIXME: converting via tuple to datetime, then back to tuple.  Wasteful.
            return datetime.datetime(int(d[0:4]), int(d[4:6]), int(d[6:8]), int(d[8:10]), int(d[10:12]), int(d[12:]), UTC).timetuple() #.utctimetuple()
        except ValueError:
            return None
    return None

#
feedparser.registerDateHandler(mods_convention_date)

def smart_parse_date(date):
    parts = date.split('/')
    try:
        if len(parts) == 3:
            return datetime.date(int(parts[2]), int(parts[0]), int(parts[1])).timetuple()
    except ValueError:
        pass
    return datetime.datetime(*feedparser._parse_date(date)[:7])