125 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			125 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package common
 | ||
| 
 | ||
| import (
 | ||
| 	"strconv"
 | ||
| 	"strings"
 | ||
| )
 | ||
| 
 | ||
| func Unify(s string) string {
 | ||
| 	s = strings.TrimSpace(s)
 | ||
| 	s = strings.ToLower(s)
 | ||
| 	for strings.Contains(s, "  ") {
 | ||
| 		s = strings.ReplaceAll(s, "  ", " ")
 | ||
| 	}
 | ||
| 	return s
 | ||
| }
 | ||
| 
 | ||
| func ZeroPad(num int, length int) string {
 | ||
| 	str := strconv.Itoa(num)
 | ||
| 	if len(str) >= length {
 | ||
| 		return str
 | ||
| 	}
 | ||
| 	return strings.Repeat("0", length-len(str)) + str
 | ||
| }
 | ||
| 
 | ||
| func IsNumeric(num string) bool {
 | ||
| 	for _, c := range num {
 | ||
| 		if c < '0' || c > '9' {
 | ||
| 			return false
 | ||
| 		}
 | ||
| 	}
 | ||
| 	return true
 | ||
| }
 | ||
| 
 | ||
| var commonTitleSuffix = []string{"(official video)"}
 | ||
| 
 | ||
| const openBrackets = "(["
 | ||
| const closeBrackets = ")]"
 | ||
| 
 | ||
| var forbiddenSubstringInBrackets = []string{"official", "video", "audio", "lyrics", "prod", "remix", "ft", "feat", "ft.", "feat."}
 | ||
| 
 | ||
| func CleanSongTitle(title string, artistName string) string {
 | ||
| 	/*
 | ||
| 		# remove brackets and their content if they contain disallowed substrings
 | ||
| 		for open_bracket, close_bracket in zip(OPEN_BRACKETS, CLOSE_BRACKETS):
 | ||
| 		    if open_bracket not in raw_song_title or close_bracket not in raw_song_title:
 | ||
| 		        continue
 | ||
| 
 | ||
| 		    start = 0
 | ||
| 
 | ||
| 		    while True:
 | ||
| 		        try:
 | ||
| 		            open_bracket_index = raw_song_title.index(open_bracket, start)
 | ||
| 		        except ValueError:
 | ||
| 		            break
 | ||
| 		        try:
 | ||
| 		            close_bracket_index = raw_song_title.index(close_bracket, open_bracket_index + 1)
 | ||
| 		        except ValueError:
 | ||
| 		            break
 | ||
| 
 | ||
| 		        substring = raw_song_title[open_bracket_index + 1:close_bracket_index]
 | ||
| 		        if any(disallowed_substring in substring.lower() for disallowed_substring in DISALLOWED_SUBSTRING_IN_BRACKETS):
 | ||
| 		            raw_song_title = raw_song_title[:open_bracket_index] + raw_song_title[close_bracket_index + 1:]
 | ||
| 		        else:
 | ||
| 		            start = close_bracket_index + 1
 | ||
| 
 | ||
| 		# everything that requires the artist name
 | ||
| 		if artist_name is not None:
 | ||
| 		    artist_name = artist_name.strip()
 | ||
| 
 | ||
| 		    # Remove artist from the start of the title
 | ||
| 		    if raw_song_title.lower().startswith(artist_name.lower()):
 | ||
| 
 | ||
| 		        possible_new_name = raw_song_title[len(artist_name):].strip()
 | ||
| 
 | ||
| 		        for char in ("-", "–", ":", "|"):
 | ||
| 		            if possible_new_name.startswith(char):
 | ||
| 		                raw_song_title = possible_new_name[1:].strip()
 | ||
| 		                break
 | ||
| 
 | ||
| 		return raw_song_title.strip()
 | ||
| 
 | ||
| 	*/
 | ||
| 
 | ||
| 	title = strings.TrimSpace(title)
 | ||
| 
 | ||
| 	for _, d := range commonTitleSuffix {
 | ||
| 		if strings.HasSuffix(strings.ToLower(title), d) {
 | ||
| 			title = strings.TrimSpace(title[:len(d)-1])
 | ||
| 		}
 | ||
| 	}
 | ||
| 
 | ||
| 	for b, open := range openBrackets {
 | ||
| 		close := closeBrackets[b]
 | ||
| 
 | ||
| 		s := -1
 | ||
| 		e := -1
 | ||
| 
 | ||
| 		for i, c := range title {
 | ||
| 			if c == open {
 | ||
| 				s = i
 | ||
| 			} else if c == rune(close) {
 | ||
| 				e = i
 | ||
| 			}
 | ||
| 		}
 | ||
| 
 | ||
| 		remove := false
 | ||
| 		if s > -1 {
 | ||
| 			substring := title[s:e]
 | ||
| 
 | ||
| 			for _, f := range forbiddenSubstringInBrackets {
 | ||
| 				if strings.Contains(substring, f) {
 | ||
| 					remove = true
 | ||
| 					break
 | ||
| 				}
 | ||
| 			}
 | ||
| 		}
 | ||
| 
 | ||
| 		if remove {
 | ||
| 			title = title[:s] + title[e:]
 | ||
| 		}
 | ||
| 	}
 | ||
| 
 | ||
| 	return title
 | ||
| }
 |