Skip to content

Commit

Permalink
Issue mozilla-mobile#25: Add fn to write ta crosswalk to DB.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcomella committed Jan 26, 2017
1 parent 5bca3e3 commit 7478367
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/prox_crosswalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
Intended to be used from a REPL. Primary functions are:
- yelp_ids_to_tripadvisor_ids
- write_to_db
TODO:
- Handle hitting API limits
- Handle network errors (e.g. TA API)
- TEST ACCURACY
- Make it easy to run (read place ids from file?)
"""
from __future__ import print_function
Expand Down Expand Up @@ -93,3 +96,38 @@ def yelp_ids_to_tripadvisor_ids(yelp_ids):
"""
raw_ta = _yelp_ids_to_raw_tripadvisor(yelp_ids)
return _get_yelp_to_ta_map_from_raw_ta(raw_ta)


def _write_crosswalk_to_db(yelp_id, provider_map):
"""Ensure the given crosswalk object is valid and writes it to the DB.
Data existing at the given keys will be overwritten.
:param yelp_id: for the place
:param provider_map: is {'tripadvisor': <id-str>, ...}
"""
# Assert 1) no typos, 2) we haven't added keys that this code may not know how to handle.
for key in provider_map: assert key in CROSSWALK_KEYS
_get_crosswalk_db().child(yelp_id).update(provider_map)


def write_to_db(yelp_to_ta=None, yelp_to_wiki=None, yelp_to_website=None):
"""Takes yelp_id to other provider ID dicts and writes those values into the crosswalk DB.
Existing data for a given (yelp_id, other_id) pair will be overwritten.
:param yelp_to_ta: {<yelp_id>: <ta_id>, ...}; output of `yelp_ids_to_tripadvisor_ids`
"""
crosswalk = {} # Bound in fn below.

def add_ids_to_dict(provider_key, yelp_to_other_id):
if not yelp_to_other_id: return
for yelp_id, other_id in yelp_to_other_id.iteritems():
val = crosswalk.get(yelp_id, {})
val[provider_key] = other_id
crosswalk[yelp_id] = val

add_ids_to_dict('tripadvisor', yelp_to_ta)
add_ids_to_dict('wikipedia', yelp_to_wiki)
add_ids_to_dict('website', yelp_to_website)

for yelp_id, provider_map in crosswalk.iteritems():
_write_crosswalk_to_db(yelp_id, provider_map)

0 comments on commit 7478367

Please sign in to comment.