Created
September 18, 2012 16:18
-
-
Save williamhrs/3744050 to your computer and use it in GitHub Desktop.
Geodata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| import json, urllib3,urllib | |
| from lxml import etree | |
| import threading, Queue | |
| YAHOO_BASE_URL = 'http://where.yahooapis.com/geocode' | |
| def geocode(address, **geo_args): | |
| geo_args.update({ | |
| 'q': address, | |
| 'appid': 'D5wg1536', | |
| 'flags': 'J' | |
| }) | |
| try: | |
| url = YAHOO_BASE_URL + '?' + urllib.urlencode(geo_args) | |
| http = urllib3.PoolManager() | |
| r = http.request('GET', url) | |
| dados = r.data | |
| result = {} | |
| #print dados | |
| if dados: | |
| result = json.loads(dados) | |
| return result | |
| except: | |
| pass | |
| def alteraXML(): | |
| while True: | |
| empresa = fila.get() | |
| i=0 | |
| geo = geocode(address=empresa[1].text.encode('utf-8'),sensor="false") | |
| print str(i) + ": " +empresa[1].text | |
| i= i+1 | |
| if geo: | |
| if geo.has_key('ResultSet'): | |
| if geo['ResultSet']['Error'] == 0: | |
| if geo['ResultSet'].has_key('Results'): | |
| child = etree.SubElement(empresa,'latitude') | |
| child.text = unicode(geo['ResultSet']['Results'][0]['latitude']) | |
| child2 = etree.SubElement(empresa,'longitude') | |
| child2.text = unicode(geo['ResultSet']['Results'][0]['longitude']) | |
| fila.task_done(); | |
| if __name__ == '__main__': | |
| doc = etree.parse('empresas.xml') | |
| fila = Queue.Queue() | |
| for empresa in doc.getroot(): | |
| fila.put(empresa) | |
| for i in range(20): | |
| t = threading.Thread(target=alteraXML) | |
| t.daemon = True | |
| t.start() | |
| fila.join() | |
| out = open('empresas_geodata.xml', 'w') | |
| doc.write(out,encoding='utf-8') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment