Update desktop file so that it runs correctly and fix date display bug
[gigfinder] / src / opt / gigfinder / resultsparser.py
1 from xml.dom.minidom import parseString
2 from datetime import datetime, date
3 import time
4
5 import location
6
7 def parse_json(json, lat, lng):
8     """ Parse json into usable format """
9     pass
10
11 def parse_xml(xml, lat, lng):
12     """ Parse xml into a dict """
13     events_list = []
14     today = date.today()
15     dom = parseString(xml)
16
17     events = dom.getElementsByTagName('event')
18     for event in events:
19         start_date = parse_date(event.getElementsByTagName('startDate')[0]\
20                 .childNodes[0].data)
21         if start_date.date() == today:
22             title = event.getElementsByTagName('title')[0].childNodes[0].data
23             
24             artists_element = event.getElementsByTagName('artists')[0]
25             artist_list = []
26             for artist in artists_element.getElementsByTagName('artist'):
27                 artist_list.append(artist.childNodes[0].data)
28             artists = ', '.join(artist_list)
29
30             v_details = event.getElementsByTagName('venue')[0]
31             venue_name = v_details.getElementsByTagName('name')[0]\
32                     .childNodes[0].data
33             address = get_address(v_details.getElementsByTagName('location')[0])
34             geo_data = v_details.getElementsByTagName('geo:point')[0]
35             venue_lat = geo_data.getElementsByTagName('geo:lat')[0]\
36                     .childNodes[0].data
37             venue_long = geo_data.getElementsByTagName('geo:long')[0]\
38                     .childNodes[0].data
39             distance = location.distance_between(float(lat), 
40                                                  float(lng), 
41                                                  float(venue_lat), 
42                                                  float(venue_long))
43             
44             events_list.append({'title': title,
45                                 'venue': venue_name,
46                                 'address': address,
47                                 'distance': distance,
48                                 'artists': artists,
49                                 'date': start_date})
50     return events_list
51
52 def get_address(location_element):
53     """ Return the venues address details from the xml element """
54     street = ''
55     city = ''
56     country = ''
57     postalcode = ''
58     if location_element.getElementsByTagName('street')[0].childNodes:
59         street = location_element.getElementsByTagName('street')[0]\
60                 .childNodes[0].data
61     if location_element.getElementsByTagName('city')[0].childNodes:
62         city = location_element.getElementsByTagName('city')[0]\
63                 .childNodes[0].data
64     if location_element.getElementsByTagName('country')[0].childNodes:
65         country = location_element.getElementsByTagName('country')[0]\
66                 .childNodes[0].data
67     if location_element.getElementsByTagName('postalcode')[0].childNodes:
68         postalcode = location_element.getElementsByTagName('postalcode')[0]\
69                 .childNodes[0].data
70     return '\n'.join([street, city, country, postalcode])
71
72 def parse_date(date_string):
73     """ Parse date string into datetime object """
74     fmt =  "%a, %d %b %Y %H:%M:%S"
75     result = time.strptime(date_string, fmt)
76     return datetime(result.tm_year, 
77                     result.tm_mon, 
78                     result.tm_mday, 
79                     result.tm_hour, 
80                     result.tm_min, 
81                     result.tm_sec)