def getOriginFromWikidata(name: str): toReturn = Optional.empty() placeOfBirth = None page = wptools.page(name) try: page.get_wikidata() except: print("Couldn't get wikidata from name.") try: placeOfBirth = page.data['wikidata']['place of birth (P19)'] except: None try: placeOfBirth = page.data['wikidata']['location of formation (P740)'] except: None try: index = placeOfBirth.index("(") placeOfBirth = placeOfBirth[0:index] except: None if placeOfBirth != None: toReturn = Optional.of(placeOfBirth) return toReturn
def test_get_or_raise_on_an_empty_optional_throws_wrapped_exception(self): optional = Optional.empty() class RandomDomainException(Exception): pass with pytest.raises(RandomDomainException): optional.get_or_raise(RandomDomainException())
def test_will_raise_on_or_else_raise_from_if_present_when_not_present( self): class TestException(Exception): pass optional = Optional.empty() with pytest.raises(TestException): optional.if_present(lambda x: x).or_else_raise( TestException("Something"))
def test_will_not_run_consumer_if_not_present(self): scope = {'seen': False} def some_thing_consumer(thing): scope['seen'] = True optional = Optional.empty() optional.if_present(some_thing_consumer) assert not scope['seen']
def test_will_run_or_else_from_if_present_when_not_present(self): scope = { 'if_seen': False, 'else_seen': False, } def some_thing_consumer(thing): scope['if_seen'] = True def or_else_procedure(): scope['else_seen'] = True optional = Optional.empty() optional.if_present(some_thing_consumer).or_else(or_else_procedure) assert not scope['if_seen'] assert scope['else_seen']
def getArtistOriginFromScraping(name: str): result = Optional.empty() if result.is_empty(): result = getOriginFromMusicbrainz(name) if result.is_empty(): result = getOriginFromWikipedia(name + " Musician") if result.is_empty(): result = getOriginFromWikipedia(name + " Band") if result.is_empty(): result = getOriginFromWikipedia(name) if result.is_empty(): result = getOriginFromWikidata(name) return result
def test_empty_optionals_are_falsy(self): assert not Optional.empty()
def test_get_or_default_on_an_empty_optional_returns_default_value(self): optional = Optional.empty() assert optional.get_or_default("pants") == "pants"
def test_can_instantiate_an_empty_optional_via_the_zero_arity_of(self): assert Optional.of() == Optional.empty()
def test_can_eval_the_representation_of_an_empty_optional(self): optional = Optional.empty() assert eval(repr(optional)) == optional
def test_flat_map_returns_empty_if_value_is_empty(self): def does_stuff(thing): return Optional.of("PANTS") optional = Optional.empty() assert optional.flat_map(does_stuff).is_empty()
def does_nothing(thing): return Optional.empty()
def test_instantiate_empty(self): optional = Optional.empty() assert optional.is_empty()
def test_will_return_optional_of_return_val_when_not_present(self): def or_else_supplier(): return "pants" optional = Optional.empty() assert optional.or_else(or_else_supplier) == Optional.of("pants")
def getOriginFromMusicbrainz(name: str): toReturn = Optional.empty() wiki_location = Optional.empty() location = "" sort_name = "" url = "https://musicbrainz.org/search?query=" + urllib.parse.quote_plus( name) + "&type=artist&method=indexed" page = requests.get(url) print(page) bs = BeautifulSoup(page.content, features="lxml") table = bs.find_all("table") try: titles = [title.text.strip() for title in table[0].find_all("th")] except: return toReturn artist_data = [] for data in table[0].find_all("td"): if len(artist_data) < len(titles): artist_data.append(data.text.strip()) else: break found_category = 0 for key, value in zip(titles, artist_data): if (key == "Area" or key == "Begin Area"): if value != "": found_category += 1 if key == "Area": location += value if key == "Begin Area": if value != "": location = value + ", " + location if key == "Sort Name": sort_name = value # attempt to verify this is the right artist using sort name, if not stop the function name_list = name.lower().split() sort_name = sort_name.lower() dont_match_score = 0 for word in name_list: try: try: index = sort_name.index(word) except: # removes a potential 's from the end of the name index = sort_name.index(word[0:len(word) - 2]) except: dont_match_score += 1 dont_match_index = dont_match_score / len(name_list) print("Dissimilarity rating is", dont_match_index) if dont_match_index > 0.4: return toReturn # if we didnt find precise city, search wikipedia with location name if found_category < 2: try: wiki_location = getOriginFromWikipedia(name + " " + location + " Musician") print(wiki_location) except: None if wiki_location.is_present(): toReturn = wiki_location elif location != "": toReturn = Optional.of(location) return toReturn
def test_cannot_get_from_empty_even_after_checking(self): optional = Optional.empty() assert optional.is_empty() with pytest.raises(OptionalAccessOfEmptyException): optional.get()
def test_empty_optionals_are_equal(self): assert Optional.empty() == Optional.empty()
def test_empty_optional_not_equal_non_empty_optional(self): assert Optional.empty() != Optional.of("thing")
def get_open_class(self, period: int, subject: Subject) -> Optional.of(Class): newest_class = self.matrix[period][subject.id][-1] if newest_class.is_open(): return Optional.of(newest_class) return Optional.empty()
def getOriginFromWikipedia(name: str): toReturn = Optional.empty() try: searches = wiki.search(name) while len(searches) == 0: if len(name) <= 2: return Optional.empty() name = ' '.join(name.split(' ')[:-1]) print(name) searches = wiki.search(name) artistPage = wiki.page(searches[0], auto_suggest=False).html() except: return Optional.empty() bs = BeautifulSoup(artistPage, features="lxml") tables = bs.find_all("table") tab = None for tab in tables: if "infobox" in tab["class"]: break if tab != None: node_list = [node for node in tab.find_all('tr')] zipped_key_value = [] for node in node_list: try: entry = (node.find_all('th')[0].text.strip(), node.find_all('td')[0].text.strip()) zipped_key_value.append(entry) except: None # search for 'origin' to return for key, value in zipped_key_value: if key == "Origin": toReturn = Optional.of(value) # edge case for some solo artists, find birthplace if toReturn == Optional.empty(): for div in tab.find_all("div", {"class": "birthplace"}): if "birthplace" in div["class"]: toReturn = Optional.of(div.text.strip()) # worse case, birthplace not tagged if toReturn == Optional.empty(): for key, value in zipped_key_value: if key == "Born": try: index = value.index("age") value = value[index + 7:] toReturn = Optional.of(value) except: index = 0 if key == "Died": try: index = value.index("age") value = value[index + 7:] toReturn = Optional.of(value) except: index = 0 return toReturn