Merge recent changes
[gigfinder] / src / opt / gigfinder / gigfinder.py
index 42d4d6c..7e159ef 100755 (executable)
@@ -10,13 +10,8 @@ __copyright__ = "Copyright 2010 Jon Staley"
 __license__ = "MIT"
 __version__ = "0.0.1"
 
 __license__ = "MIT"
 __version__ = "0.0.1"
 
-from xml.dom.minidom import parseString
-from datetime import datetime, date
-import urllib
-import time
 import gtk
 import hildon
 import gtk
 import hildon
-import location
 import time
 import gobject
 import os.path
 import time
 import gobject
 import os.path
@@ -24,119 +19,14 @@ from threading import Thread
 import thread
 
 from locator import LocationUpdater
 import thread
 
 from locator import LocationUpdater
+from events import Events
 
 gtk.gdk.threads_init()
 
 
 gtk.gdk.threads_init()
 
-class GigParser:
-
-    def parse_xml(self, xml, lat, long):
-        """ Parse xml into a dict """
-        events_list = []
-        today = date.today()
-        dom = parseString(xml)
-
-        events = dom.getElementsByTagName('event')
-        for event in events:
-            start_date = self.parse_date(event.getElementsByTagName('startDate')[0].childNodes[0].data)
-            if start_date.date() == today:
-                title = event.getElementsByTagName('title')[0].childNodes[0].data
-                
-                artists_element = event.getElementsByTagName('artists')[0]
-                artist_list = []
-                for artist in artists_element.getElementsByTagName('artist'):
-                    artist_list.append(artist.childNodes[0].data)
-                artists = ', '.join(artist_list)
-
-                venue_details = event.getElementsByTagName('venue')[0]
-                venue_name = venue_details.getElementsByTagName('name')[0].childNodes[0].data
-                address = self.get_address(venue_details.getElementsByTagName('location')[0])
-                geo_data = venue_details.getElementsByTagName('geo:point')[0]
-                venue_lat = geo_data.getElementsByTagName('geo:lat')[0].childNodes[0].data
-                venue_long = geo_data.getElementsByTagName('geo:long')[0].childNodes[0].data
-                distance = location.distance_between(float(lat), 
-                                                     float(long), 
-                                                     float(venue_lat), 
-                                                     float(venue_long))
-                
-                events_list.append({'title': title,
-                                    'venue': venue_name,
-                                    'address': address,
-                                    'distance': distance,
-                                    'artists': artists,
-                                    'date': start_date})
-        return events_list
-    
-    def get_address(self, location):
-        """ Return the venues address details from the xml element """
-        street = ''
-        city = ''
-        country = ''
-        postalcode = ''
-        if location.getElementsByTagName('street')[0].childNodes:
-            street = location.getElementsByTagName('street')[0].childNodes[0].data
-        if location.getElementsByTagName('city')[0].childNodes:
-            city = location.getElementsByTagName('city')[0].childNodes[0].data
-        if location.getElementsByTagName('country')[0].childNodes:
-            country = location.getElementsByTagName('country')[0].childNodes[0].data
-        if location.getElementsByTagName('postalcode')[0].childNodes:
-            postalcode = location.getElementsByTagName('postalcode')[0].childNodes[0].data
-        return '\n'.join([street, city, country, postalcode])
-
-    def parse_date(self, date_string):
-        """ Parse date string into datetime object """
-        fmt =  "%a, %d %b %Y %H:%M:%S"
-        result = time.strptime(date_string, fmt)
-        return datetime(result.tm_year, 
-                        result.tm_mon, 
-                        result.tm_mday, 
-                        result.tm_hour, 
-                        result.tm_min, 
-                        result.tm_sec)
-
-
-class Events:
-
-    def __init__(self):
-        self.api_key = "1928a14bdf51369505530949d8b7e1ee"
-        self.url_base = "http://ws.audioscrobbler.com/2.0/"
-        self.parser = GigParser()
-
-    def get_events(self, lat, long, distance):
-        """ Retrieve xml and parse into events list """
-        events = []
-        for page in ['1', '2', '3']:
-            xml = self.get_xml(lat, long, distance, page=page)
-            events.extend(self.parser.parse_xml(xml, 
-                                                lat,
-                                                long))
-        return self.sort_events(events)
-
-    def sort_events(self, events):
-        """ Sort gig by distance """
-        events.sort(cmp=self.distance_cmp, key=lambda x: x['distance'])
-        return events
-        
-    def get_xml(self, lat, long, distance, page="1"):
-        """ Return xml from lastfm """
-        method = "geo.getevents"
-        params = urllib.urlencode({'method': method,
-                                   'api_key': self.api_key,
-                                   'distance': distance,
-                                   'long': long,
-                                   'lat': lat,
-                                   'page': page})
-        response = urllib.urlopen(self.url_base, params)
-        return response.read()
-    
-    def distance_cmp(self, x, y):
-        """ Compare distances for list sort """
-        if x > y:
-            return 1
-        elif x == y:
-            return 0
-        else:
-            return -1
-
+# TODO: 
+# Add user settings for distance, date
+# maybe switch to json
+# maybe do km to mile conversions
 
 class GigFinder:
 
 
 class GigFinder:
 
@@ -149,10 +39,6 @@ class GigFinder:
         self.events = Events()
         self.win = hildon.StackableWindow()
         self.app_title = "Gig Finder"
         self.events = Events()
         self.win = hildon.StackableWindow()
         self.app_title = "Gig Finder"
-        # TODO: 
-        # Add user settings for distance, date
-        # refactor gui code, 
-        # maybe do km to mile conversions
 
     def main(self):
         """ Build the gui and start the update thread """
 
     def main(self):
         """ Build the gui and start the update thread """
@@ -216,7 +102,7 @@ class GigFinder:
                                                             message)
 
     def hide_message(self):
                                                             message)
 
     def hide_message(self):
-        """ Hide banner and sete progress indicator """
+        """ Hide banner and set progress indicator """
         self.banner.hide()
         hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
 
         self.banner.hide()
         hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)