Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
location module - # requests per time limited?
-
I was experimenting with the location module, specifically location.geocode() to convert addresses in the contacts list to lat/lon coordinates.
I wrote a small script which went through all the contacts and for each address in each contact, called location.geocode() and then cached the results in a file. The idea is that you just do this once and then only update the cache when the contact modification_date changes so you're not geocode()'ing them all every time.
Anyway, I noticed it went through the list for awhile and then eventually just failed on all the remaining entries. I tried again multiple times and it kept failing. I then waited a minute or so and tried it and the remaining entries all worked.
So... is pythonista or iOS throttling the # geocode requests you can send in some unit of time? Does anyone know what the actual limit is? There appears to be some limiting going on, I'm just curious what the actual limit is.
-
Could you post your code? That might help me, and other people troubleshoot your problem.
-
There's not much to it. Just, as I said, looping through contacts and attempting to geocode the addresses :)
Here's a bit of sample code which reproduces what I'm seeing. When I run this it seems to work, going through the list but it eventually gets to a point where all remaining geocode requests fail. When this happens if you immediately run it again you'll see they all fail, even the ones which worked before. If you then wait a bit and try again it'll start working.
My conclusion was that there's some sort of throttling being applied. Maybe by Pythonista, maybe iOS, not sure. I was posting here just to see if someone knew of such a limit imposed somewhere in the stack for geocode operations and what the actual limit was.
import location import contacts x = 0 ppl = contacts.get_all_people() for p in ppl: for addrt in p.address: x = x + 1 addr_tag = contacts.localized_label(addrt[0]) addr = addrt[1] print(str(x) + ', ' + p.full_name + ', ' + addr_tag) locs = location.geocode(addr) if locs and len(locs) > 0: loc = locs[0] lat = loc['latitude'] lon = loc['longitude'] print(' ' + str(lat) + ',' + str(lon)) else: print(' ERROR: unable to locate')
-
I get 50 before failure starts, if that helps. Reversing the list still gives me the first 50. Also there's an extra ) in the first print line.
-
Pythonista itself doesn't throttle geocoding, but I assume that the underlying iOS APIs are rate-limited (it's basically just a wrapper for the native CoreLocation functionality).
-
henryiii, I corrected the code - I edited it after the fact so the print provides a little more useful info besides just the index :)
omz, thanks for the reply, must just be an iOS limit then.
-
You can throttle the API calls on your end by sleeping after each successful API call:
import contacts, location, time for i, p in enumerate(contacts.get_all_people()): for addr_tag, addr in p.address: addr_tag = contacts.localized_label(addr_tag) print('{}, {}, {}'.format(i+1, p.full_name, addr_tag)) locs = location.geocode(addr) if locs: print(' {latitude},{longitude}'.format(**locs[0])) time.sleep(2) # comment out to go full speed else: print(' ERROR: unable to locate: {}'.format(addr))