package win.taptap.musicmanager.discovery private const val NOISE_WORDS = "official|lyrics?|audio|video|visuali[sz]er|mv|hd|4k|remaster(?:ed)?" private val TITLE_NOISE = Regex( "\\((?:$NOISE_WORDS)[^)]*\\)" + "|\\[(?:$NOISE_WORDS)[^\\]]*\\]" + "|\\s*\\|\\s*(?:$NOISE_WORDS).*$", RegexOption.IGNORE_CASE, ) private val WHITESPACE = Regex("\\s+") /** * Removes the upload noise that YouTube titles carry ("(Official Video)", * "[4K Remaster]", "| Lyrics"), so a search hit, a download tag and an existing * collection row compare as the same song. */ internal fun cleanTrackTitle(rawTitle: String): String = rawTitle.replace(TITLE_NOISE, "") .replace(WHITESPACE, " ") .trim() .ifEmpty { rawTitle.trim() } /** * `"Daft Punk - Get Lucky"` tagged by the plain YouTube extractor still has the * artist in the title. Strip it when it matches the tagged artist. */ internal fun stripArtistPrefix(title: String, artist: String): String { val name = artist.trim() if (name.isEmpty() || name.equals("Unknown artist", ignoreCase = true)) return title val prefix = "$name - " if (!title.startsWith(prefix, ignoreCase = true)) return title return title.substring(prefix.length).trim().ifEmpty { title } }