wareztb/wareztb.go

118 lines
2.8 KiB
Go
Raw Permalink Normal View History

2023-10-07 15:59:02 +02:00
package wareztb
import (
"errors"
"fmt"
"strings"
"git.0x0001f346.de/andreas/utils"
)
var whitelist map[string]string = utils.BuildWhitelistForStringSanitisation(
getWhitelistedCharacters(),
)
// GetComparableWarezString tries to make a file or folder name comparable in a warez context
func GetComparableWarezString(s string) (string, error) {
title, year, err := GetTitleAndYearFromWarezString(s)
if err != nil {
return "", err
}
2023-10-25 22:55:08 +02:00
result := fmt.Sprintf("%s.%d", title, year)
result = strings.ToLower(result)
result = utils.RemoveDoubledStrings(result, ".")
result = utils.RemoveLeadingStrings(result, ".")
return result, nil
2023-10-07 15:59:02 +02:00
}
// GetTitleAndYearFromWarezString tries to get the title and year from a string in a warez context
func GetTitleAndYearFromWarezString(s string) (string, int64, error) {
sanitized := SanitizeStringForWarezUsage(s)
year, err := getYearFromWarezString(sanitized)
if err != nil {
return "", 0, err
}
title, err := getTitleFromWarezString(sanitized, year)
if err != nil {
return "", 0, err
}
return title, year, nil
}
// SanitizeStringForWarezUsage sanitizes s for usage in a warez context ("John Wick: Kapitel 4 (2023)" --> "John.Wick.Kapitel.4.2023")
func SanitizeStringForWarezUsage(s string) string {
replacements := map[string]string{
"Ö": "Oe",
"ö": "oe",
"Ä": "Ae",
"ä": "ae",
"Ü": "Ue",
"ü": "ue",
"ß": "ss",
" ": ".",
":": "",
"_": ".",
"/": ".",
}
for old, new := range replacements {
s = strings.Replace(s, old, new, -1)
}
s = strings.Replace(s, ".-.", ".", -1)
return utils.SanitizeStringWithWhitelist(
utils.RemoveDoubledStrings(s, "."),
whitelist,
)
}
func getTitleFromWarezString(s string, year int64) (string, error) {
chuncks := strings.Split(s, ".")
sYear := fmt.Sprintf("%d", year)
for i := len(chuncks) - 1; i >= 0; i-- {
if chuncks[i] == sYear {
return strings.Join(chuncks[:i], "."), nil
}
}
return "", errors.New("could not determine a valid title in s")
}
func getWhitelistedCharacters() []string {
return []string{
".", "-",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
}
func getYearFromWarezString(s string) (int64, error) {
chuncks := strings.Split(s, ".")
for i := len(chuncks) - 1; i >= 0; i-- {
porentialYear := utils.ConvertStringToIntOrZeroOnError(chuncks[i])
if isValidYear(porentialYear) {
return porentialYear, nil
}
}
return 0, errors.New("could not find any valid year in s")
}
func isValidYear(i int64) bool {
if i < 1900 || i > (utils.GetCurrentYear()+3) {
return false
}
return true
}