forked from pylint-dev/pylint
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a regression test for issue pylint-dev#4631
- Loading branch information
1 parent
52027cb
commit 2bbc41a
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
""" | ||
Regression tests for StopIteration raised when using limit-inference-results=0 | ||
""" | ||
|
||
import typing | ||
|
||
from ytmusicapi import YTMusic | ||
|
||
|
||
def get_youtube_link( | ||
song_name: str, song_artists: typing.List[str], song_album: str, song_duration: int | ||
) -> typing.Optional[str]: | ||
ytm_results = __query_ytmusic(song_name=song_name, song_artists=song_artists) | ||
top_match_score = 0 | ||
top_result = None | ||
for result in ytm_results: | ||
name_match = __common_elm_fraction(src=song_name, res=result["name"]) | ||
if name_match < 0.1: | ||
continue | ||
if result["album"] is None: | ||
album_match = 0 | ||
else: | ||
if result["album"].lower() == song_album.lower(): | ||
album_match = 1 | ||
else: | ||
continue | ||
delta = abs(result["duration"] - song_duration) | ||
duration_match = 1 - (delta / 15) | ||
if duration_match < 0: | ||
continue | ||
artist_match = __common_elm_fraction(src=song_artists, res=result["artists"]) | ||
avg_match = (name_match + album_match + duration_match + artist_match) / 4 | ||
if avg_match > top_match_score: | ||
top_match_score = avg_match | ||
top_result = result | ||
if top_match_score <= 0.75: | ||
if top_result is not None: | ||
with open("possible errors.txt", "ab") as file: | ||
file.write( | ||
f"{', '.join(song_artists)} - {song_name}\n {top_match_score:0.2f}pt " | ||
f"{top_result['link']}: {top_result['name']}\n".encode() | ||
) | ||
else: | ||
with open("skipped.txt", "ab") as file: | ||
file.write(f"{', '.join(song_artists)} - {song_name}\n".encode()) | ||
if top_result is None: | ||
return None | ||
|
||
return top_result["link"] | ||
|
||
|
||
def __query_ytmusic( | ||
song_name: str, song_artists: typing.List[str] | ||
) -> typing.List[dict]: | ||
query = f"{', '.join(song_artists)} - {song_name}" | ||
ytm_client = YTMusic() | ||
search_results = ytm_client.search(query=query, filter="videos") | ||
search_results += ytm_client.search(query=query, filter="songs") | ||
simplified_results = [] | ||
for result in search_results: | ||
if result["resultType"] == "song": | ||
if result["album"] is None: | ||
continue | ||
res_album = result["album"]["name"] | ||
else: | ||
res_album = None | ||
try: | ||
time_bits = list(reversed(result["duration"].split(":"))) | ||
if not isinstance(result["duration"], str) or len(time_bits) > 3: | ||
continue | ||
res_duration = sum(a * int(b) for a, b in zip([1, 60, 3600], time_bits)) | ||
except ValueError: | ||
continue | ||
res_artists = [artist["name"] for artist in result["artists"]] | ||
skip_result = False | ||
for word in [ | ||
"cover" | ||
]: | ||
if (word in result["title"].lower() and word not in song_name.lower()) or ( | ||
word in song_name.lower() and word not in result["title"].lower() | ||
): | ||
skip_result = True | ||
break | ||
if skip_result: | ||
continue | ||
simplified_results.append( | ||
{ | ||
"name": result["title"], | ||
"artists": res_artists, | ||
"album": res_album, | ||
"duration": res_duration, | ||
"link": f"https://www.youtube.com/watch?v={result['videoId']}", | ||
} | ||
) | ||
return simplified_results | ||
|
||
|
||
def __common_elm_fraction( | ||
src: typing.Union[typing.List[str], str], | ||
res: typing.Union[typing.List[str], str], | ||
) -> float: | ||
if isinstance(src, str): | ||
src = src.split(" ") | ||
if isinstance(res, str): | ||
res = res.split(" ") | ||
src_set = set(__prepare_word(word=word) for word in set(src)) | ||
res_set = set(__prepare_word(word=word) for word in set(res)) | ||
if "" in src_set: | ||
src_set.remove("") | ||
if "" in res_set: | ||
res_set.remove("") | ||
common_words = 0 | ||
for src_word in src_set: | ||
for res_word in res_set: | ||
if __is_similar(src_word=src_word, res_word=res_word): | ||
common_words += 1 | ||
break | ||
if len(src_set) > len(res_set): | ||
greater_word_length = len(src_set) | ||
else: | ||
greater_word_length = len(res_set) | ||
return common_words / greater_word_length | ||
|
||
|
||
def __prepare_word(word: str) -> str: | ||
prepared_word = "" | ||
for letter in word: | ||
if letter.isalnum(): | ||
prepared_word += letter | ||
return prepared_word.lower() | ||
|
||
|
||
def __is_similar(src_word: str, res_word: str) -> bool: | ||
if src_word == res_word: | ||
return True | ||
if not len(src_word) == len(res_word): | ||
return False | ||
difference_count = 0 | ||
for _i in range(len(src_word)): | ||
if src_word[_i] != res_word[_i]: | ||
difference_count += 1 | ||
if difference_count > 2: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[testoptions] | ||
limit-inference-results=0 |