diff --git a/lib/models/spotube_track.dart b/lib/models/spotube_track.dart index a3de6e388..268a273ec 100644 --- a/lib/models/spotube_track.dart +++ b/lib/models/spotube_track.dart @@ -68,62 +68,64 @@ class SpotubeTrack extends Track { onlyCleanArtist: true, ).trim(); - final List siblings = - await client.search("$title - ${artists.join(", ")}").then( + final query = "$title - ${artists.join(", ")}"; + final List siblings = await client.search(query).then( (res) { final isYoutubeApi = client.preferences.youtubeApiType == YoutubeApiType.youtube; final siblings = isYoutubeApi || client.preferences.searchMode == SearchMode.youtube - ? res - .sorted((a, b) => b.views.compareTo(a.views)) - .map((sibling) { - int score = 0; - - for (final artist in artists) { - final isSameChannelArtist = - sibling.channelName.toLowerCase() == - artist.toLowerCase(); - final channelContainsArtist = sibling.channelName - .toLowerCase() - .contains(artist.toLowerCase()); - - if (isSameChannelArtist || channelContainsArtist) { - score += 1; - } - - final titleContainsArtist = sibling.title - .toLowerCase() - .contains(artist.toLowerCase()); - - if (titleContainsArtist) { - score += 1; - } - } - - final titleContainsTrackName = sibling.title - .toLowerCase() - .contains(track.name!.toLowerCase()); - - final hasOfficialFlag = - officialMusicRegex.hasMatch(sibling.title.toLowerCase()); - - if (titleContainsTrackName) { - score += 3; - } - - if (hasOfficialFlag) { - score += 1; - } - - if (hasOfficialFlag && titleContainsTrackName) { - score += 2; - } - - return (sibling: sibling, score: score); - }) - .sorted((a, b) => b.score.compareTo(a.score)) - .map((e) => e.sibling) + ? ServiceUtils.onlyContainsEnglish(query) + ? res + : res + .sorted((a, b) => b.views.compareTo(a.views)) + .map((sibling) { + int score = 0; + + for (final artist in artists) { + final isSameChannelArtist = + sibling.channelName.toLowerCase() == + artist.toLowerCase(); + final channelContainsArtist = sibling.channelName + .toLowerCase() + .contains(artist.toLowerCase()); + + if (isSameChannelArtist || channelContainsArtist) { + score += 1; + } + + final titleContainsArtist = sibling.title + .toLowerCase() + .contains(artist.toLowerCase()); + + if (titleContainsArtist) { + score += 1; + } + } + + final titleContainsTrackName = sibling.title + .toLowerCase() + .contains(track.name!.toLowerCase()); + + final hasOfficialFlag = officialMusicRegex + .hasMatch(sibling.title.toLowerCase()); + + if (titleContainsTrackName) { + score += 3; + } + + if (hasOfficialFlag) { + score += 1; + } + + if (hasOfficialFlag && titleContainsTrackName) { + score += 2; + } + + return (sibling: sibling, score: score); + }) + .sorted((a, b) => b.score.compareTo(a.score)) + .map((e) => e.sibling) : res.sorted((a, b) => b.views.compareTo(a.views)).where((item) { return artists.any( (artist) => diff --git a/lib/utils/service_utils.dart b/lib/utils/service_utils.dart index 64aa45b6e..2d44b9841 100644 --- a/lib/utils/service_utils.dart +++ b/lib/utils/service_utils.dart @@ -17,6 +17,13 @@ import 'package:html/parser.dart' as parser; abstract class ServiceUtils { static final logger = getLogger("ServiceUtils"); + static final _englishMatcherRegex = RegExp( + "^[a-zA-Z0-9\\s!\"#\$%&\\'()*+,-.\\/:;<=>?@\\[\\]^_`{|}~]*\$", + ); + static bool onlyContainsEnglish(String text) { + return _englishMatcherRegex.hasMatch(text); + } + static String clearArtistsOfTitle(String title, List artists) { return title .replaceAll(RegExp(artists.join("|"), caseSensitive: false), "")