Ma présentation sur ElasticSearch à l'Apéro Web Nancy du 25 septembre 2014 : slides, jeu de données et démos
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fetch_bars.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import os
  5. import os.path
  6. import sys
  7. import time
  8. from googleplaces import GooglePlaces, types
  9. DIR = os.path.join("dataset", "bars")
  10. API_KEY = ""
  11. PLACES = GooglePlaces(API_KEY)
  12. def page_results(token=None):
  13. # Pour la création du dataset initial : pas de name, puis name="bar",
  14. # name="pub", name="club", etc.
  15. query_result = PLACES.nearby_search(
  16. location="Nancy, France", language="fr", radius=15000, #name="name",
  17. types=[types.TYPE_BAR, types.TYPE_NIGHT_CLUB], next_page_token=token)
  18. for place in query_result.places:
  19. filename = os.path.join(DIR, place.id + ".json")
  20. if os.path.exists(filename):
  21. continue
  22. print place.name
  23. place.get_details()
  24. doc = {
  25. "name": place.name,
  26. "location": {
  27. "lat": place.geo_location["lat"],
  28. "lon": place.geo_location["lng"],
  29. },
  30. "details": place.details,
  31. }
  32. with open(filename, "w") as out:
  33. json.dump(doc, out)
  34. return query_result.next_page_token
  35. def main():
  36. if API_KEY == "":
  37. print "Vous devez vous créer une clef d'API Google Places et l'ajouter à ce fichier !"
  38. sys.exit(1)
  39. if not os.path.isdir(DIR):
  40. os.makedirs(DIR)
  41. token = page_results()
  42. while token is not None:
  43. time.sleep(2)
  44. token = page_results(token)
  45. if __name__ == "__main__":
  46. main()