def testGetPlace(self): place_id = "43e166a5-a024-4cbb-9a1f-d4947b4ff489" musicbrainzngs.get_place_by_id(place_id) self.assertEqual("http://musicbrainz.org/ws/2/place/43e166a5-a024-4cbb-9a1f-d4947b4ff489", self.opener.get_url()) musicbrainzngs.get_place_by_id(place_id, ["event-rels"]) self.assertEqual("http://musicbrainz.org/ws/2/place/43e166a5-a024-4cbb-9a1f-d4947b4ff489?inc=event-rels", self.opener.get_url())
def get_place_type(id): l = musicbrainzngs.get_place_by_id(id) try: t = l['place']['type'].lower() except KeyError: t = 'place' return t
def match_venue_by_coordinates(venue_mbid, setlist_api_key, distance_threshold=25, match_threshold=80): mb_venue = musicbrainzngs.get_place_by_id(venue_mbid) mb_venue = mb_venue['place'] matched_venue_dict = dict(mbid=mb_venue['id'], mb_name=mb_venue['name'], \ venue_lat=None, venue_long=None, \ slid=None, sl_name=None, \ city_lat=None, city_long=None, city_name=None) if 'coordinates' in mb_venue: venue_lat = float(mb_venue['coordinates']['latitude']) venue_long = float(mb_venue['coordinates']['longitude']) matched_venue_dict['venue_lat'] = venue_lat matched_venue_dict['venue_long'] = venue_long # Search Setlist.fm venues by the name of the MB venue request = 'https://api.setlist.fm/rest/1.0/search/venues?name={}'.format(mb_venue['name']) headers = {'Accept': 'application/json', 'x-api-key': setlist_api_key} results = requests.get(request, headers=headers) json_results = results.json() if 'code' in json_results.keys(): if json_results['code'] == 404: print('No match found in Setlist for venue {}'.format(mb_venue['name'])) else: sleep_time = 1 while 'venue' not in json_results.keys(): time.sleep(sleep_time) print("Trying again...") results = requests.get(request, headers=headers) json_results = results.json() sleep_time = sleep_time*1.5 potential_matches = json_results['venue'] # Calculate distance between query venue coords and city coords for each SL venue # Filter out SL venues with distance above threshold filter_matches = [] for venue in potential_matches: if 'lat' in venue['city']['coords'].keys(): city_lat = venue['city']['coords']['lat'] city_long = venue['city']['coords']['long'] if distance((venue_lat, venue_long), (city_lat, city_long)) < distance_threshold: filter_matches.append(venue) potential_matches = filter_matches # Calculate match between venue names based on edit distance best_match = match_threshold best_venue = {} for venue in potential_matches: fuzzy_match = fuzz.ratio(mb_venue['name'], venue['name']) if fuzzy_match > best_match: best_venue = venue if bool(best_venue): matched_venue_dict['slid'] = best_venue['id'] matched_venue_dict['sl_name'] = best_venue['name'] matched_venue_dict['city_lat'] = best_venue['city']['coords']['lat'] matched_venue_dict['city_long'] = best_venue['city']['coords']['long'] matched_venue_dict['city_name'] = best_venue['city']['name'] return matched_venue_dict
def get_place_by_id(id): """Get event with the MusicBrainz ID. Returns: Event object with the following includes: artist-rels, place-rels, series-rels, url-rels. """ key = cache.gen_key(id) place = cache.get(key) if not place: try: place = musicbrainzngs.get_place_by_id( id, ['artist-rels', 'place-rels', 'release-group-rels', 'url-rels']).get('place') except ResponseError as e: if e.cause.code == 404: return None else: raise InternalServerError(e.cause.msg) cache.set(key=key, val=place, time=DEFAULT_CACHE_EXPIRATION) return place
def get_place_location_string(place_id): log.debug("%s: get_place_location_string cache size %d", PLUGIN_NAME, len(get_place_location_string.cache)) if place_id in get_place_location_string.cache: return get_place_location_string.cache[place_id] place_full_info = musicbrainzngs.get_place_by_id(place_id, includes=['area-rels' ])['place'] name_components = [ place_full_info['name'], # Union Chapel place_full_info['area']['name'], # Islington ] # Now walk up the name def check_area(area_id): area_full_info = musicbrainzngs.get_area_by_id(area_id, includes=['area-rels' ])['area'] for area_rel in area_full_info['area-relation-list']: # This type-id is "type of" if area_rel['type-id'] == 'de7cc874-8b1b-3a05-8272-f3834c968fb7' \ and 'direction' in area_rel \ and area_rel['direction'] == 'backward': name_components.append(area_rel['area']['name']) check_area(area_rel['area']['id']) check_area(place_full_info['area']['id']) def remove_seq_dupes(seq): newseq = [] for x in seq: if len(newseq) == 0 or newseq[-1] != x: newseq.append(x) return newseq place_location_string = ", ".join(remove_seq_dupes(name_components)) get_place_location_string.cache[place_id] = place_location_string return place_location_string
from json import dumps from pandas import DataFrame, read_csv from codecs import open set_useragent('kp_lister', '0.0.1', contact='*****@*****.**') with open("mbids_contemporary_music.txt", "r") as f: mbids = f.readlines() records = [] for mbid in set(mbids): try: mb_data = get_artist_by_id(mbid.strip().split("/")[-1], includes=['url-rels'])["artist"] except ResponseError: try: mb_data = get_place_by_id(mbid.strip().split("/")[-1], includes=['url-rels'])["place"] except ResponseError: try: mb_data = get_label_by_id(mbid.strip().split("/")[-1], includes=['url-rels'])["label"] except ResponseError: mb_data = get_series_by_id(mbid.strip().split("/")[-1], includes=['url-rels'])["series"] print(mb_data) data = { "name": mb_data["name"], "mbid": mbid, "sort-name": mb_data["sort-name"] if "sort-name" in mb_data else None,
def get_place_name(id): l = musicbrainzngs.get_place_by_id(id) return l['place']['name']