-
Notifications
You must be signed in to change notification settings - Fork 0
/
fingerprinting.go
86 lines (80 loc) · 3.35 KB
/
fingerprinting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package search
import (
"html"
"regexp"
"strings"
)
// Drop any additional info: timestamps, release versions, etc.
// -->
var squareBracesRx, _ = regexp.Compile("^(.+(?:\\s+|\\)))\\[[^\\[\\]]+?\\](.*)$")
var precedingSquareBracesRx, _ = regexp.Compile("^(\\s*)\\[[^\\[\\]]+?\\](.+)$")
var roundBracesRx, _ = regexp.Compile("^(.+(?:\\s+|\\]))\\([^()]+?\\)(.*)$")
var angleBracesRx, _ = regexp.Compile("^(.+)\\s+<<.*?>>(.*)$")
var dateRx, _ = regexp.Compile("^(.+)\\s+(?:\\d{1,2}\\.\\d{1,2}\\.\\d{4}|\\d{4}\\.\\d{2}\\.\\d{2})(.*)$")
// Unable to merge it into date_regex due to some strange behaviour of re
// module.
var date2Rx, _ = regexp.Compile("^(.+)\\s+(?:по|от)\\s+(?:\\d{1,2}\\.\\d{1,2}\\.\\d{4}|\\d{4}\\.\\d{2}\\.\\d{2})(.*)$")
var releaseCounterRx, _ = regexp.Compile("^(.+)\\s+\\d+\\s*(?:в|из)\\s*\\d+(.*)$")
var spacesRx, _ = regexp.Compile("\\s+/.*")
var spaces2Rx, _ = regexp.Compile("\\s+")
var categoriesRx, _ = regexp.Compile("^(national\\s+geographic\\s*:|наука\\s+2\\.0)\\s+")
var arrowsRx, _ = regexp.Compile("^«([^»]{6,})»")
var cyrilicRx, _ = regexp.Compile("^([0-9a-zабвгдеёжзийклмнопрстуфхцчшщьъыэюя., \\-:]{6,}?(?:[:.?!]| - | — |\\|)).*")
var badKeywordsRx, _ = regexp.Compile("(?:\\s|\\()(:?выпуск|выпуски|выпусков|обновлено|передачи за|серия из|сезон|серия|серии|премьера|эфир с|эфир от|эфиры от|satrip)(?:\\s|\\)|$)")
func GetResultFingerprint(t *ExternalResultItem) string {
tagsRx, _ := regexp.Compile("</?[a-z]+>")
name := strings.Replace(t.Title, "ё", "e", -1)
name = html.UnescapeString(name)
name = tagsRx.ReplaceAllString(name, "")
oldTorrentName := ""
for name != oldTorrentName {
oldTorrentName = name
for _, rx := range []*regexp.Regexp{squareBracesRx, precedingSquareBracesRx, roundBracesRx, angleBracesRx, dateRx,
date2Rx, releaseCounterRx} {
name = rx.ReplaceAllString(strings.Trim(name, " .,"), "$1$2")
}
}
name = spacesRx.ReplaceAllString(name, "")
name = strings.ToLower(name)
//Shorten it if we can
name = categoriesRx.ReplaceAllString(name, "")
name = arrowsRx.ReplaceAllString(name, "$1")
name = cyrilicRx.ReplaceAllString(name, "$1")
name = strings.Replace(name, ".", " ", -1)
//Drop punctuation and other non-alphabet chars
chars := "abcdefghijklmnopqrstuvwxyzабвгдеёжзийклмнопрстуфхцчшщьъыэюя 123456789+-_.:!,"
var validatedNameChars []rune
for _, c := range []rune(name) {
if strings.ContainsRune(chars, c) {
validatedNameChars = append(validatedNameChars, c)
}
}
name = string(validatedNameChars)
name = strings.Replace(name, "г.", "", -1)
for true {
newName := badKeywordsRx.ReplaceAllString(name, "")
if newName == name {
break
}
name = newName
}
for _, month := range []string{
"январь", "января",
"февраль", "февраля",
"март", "марта",
"апрель", "апреля",
"май", "мая",
"июнь", "июня",
"июль", "июля",
"август", "августа",
"сентябрь", "сентября",
"октябрь", "октября",
"ноябрь", "ноября",
"декабрь", "декабря",
} {
monthRx, _ := regexp.Compile("\b" + month + "\b")
name = monthRx.ReplaceAllString(name, "")
}
name = spaces2Rx.ReplaceAllString(name, " ")
return name
}