1. Sumin Byeon
  2. finance

Commits

Sumin Byeon  committed b1996df

Added process.py

  • Participants
  • Parent commits ce61802
  • Branches default

Comments (0)

Files changed (1)

File process.py

View file
  • Ignore whitespace
+import sys
+import csv
+import urllib2, json
+from urllib import urlencode
+
+def get_geolocation(address):
+    u = urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?' + urlencode({'sensor':'false', 'address':address}))
+    s = json.loads(u.read())
+    
+    if s['status'] == 'OK':
+        return s['results'][0]['geometry']['location']
+    else:
+        return None
+
+def process_row(row):
+    geo = get_geolocation(row[5])
+    return {'category':row[2], 'amount':row[3], 'lat':geo['lat'], 'lng':geo['lng']}
+
+rows = csv.reader(sys.stdin)
+#filtered = filter(lambda r: len(r) > 5 and r[5] != None, rows)
+#address_pairs = map(lambda r: (r, get_geolocation(r[5])), filtered)
+#filtered_pairs = filter(lambda p: p[1] != None, address_pairs)
+#data = map(lambda p: dict(p[0], **p[1]), filtered_pairs)
+
+data = []
+for row in rows:
+    if len(row) > 5 and row[5] != None:
+        print 'Looking up %s...' % row[5]
+        location = get_geolocation(row[5])
+        if location != None:
+            d = {'category':row[2], 'amount':float(row[3].replace(',','')), 'lat':location['lat'], 'lng':location['lng']}
+            data.append(d)
+
+print 'var data = ['
+for d in data:
+    print '    %s,' % d
+print '];'