aboutsummaryrefslogtreecommitdiff
path: root/tag.go
blob: 3c069cad392f094035ff284509ed077cdf06662a (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package dhtsearch

import (
	"fmt"
	"regexp"
	"strings"
	"unicode"
)

// Default tags, can be supplimented or overwritten by config
var tags = map[string]string{
	"flac":        `\.flac$`,
	"episode":     "(season|episode|s[0-9]{2}e[0-9]{2})",
	"1080":        "1080",
	"720":         "720",
	"hd":          "hd|720|1080",
	"bdrip":       "bdrip",
	"adult":       `(xxx|p(orn|ussy)|censor|sex|urbat|a(ss|nal)|o(rgy|gasm)|(fu|di|co)ck|esbian|milf|lust|gay)|rotic|18(\+|yr)`,
	"dvdrip":      "dvdrip",
	"ebook":       "epub",
	"application": `\.(apk|exe|msi|dmg)$`,
	"android":     `\.apk$`,
	"apple":       `\.dmg$`,
	"subtitles":   `\.s(rt|ub)$`,
	"archive":     `\.(zip|rar|p7|tgz|bz2)$`,
	"video":       `\.(3g2|3gp|amv|asf|avi|drc|f4a|f4b|f4p|f4v|flv|gif|gifv|m2v|m4p|m4v|mkv|mng|mov|mp2|mp4|mpe|mpeg|mpg|mpv|mxf|net|nsv|ogv|qt|rm|rmvb|roq|svi|vob|webm|wmv|yuv)$`,
	"audio":       `\.(aa|aac|aax|act|aiff|amr|ape|au|awb|dct|dss|dvf|flac|gsm|iklax|ivs|m4a|m4b|mmf|mp3|mpc|msv|ogg|opus|ra|raw|sln|tta|vox|wav|wma|wv)$`,
	"document":    `\.(cbr|cbz|cb7|cbt|cba|epub|djvu|fb2|ibook|azw.|lit|prc|mobi|pdb|pdb|oxps|xps)$`,
	"font":        `(font|\.(ttf|fon)$)`,
}

func mergeCharacterTagREs(tagREs map[string]*regexp.Regexp) error {
	// Add character classes
	for cc := range unicode.Scripts {
		if cc == "Latin" || cc == "Common" {
			continue
		}
		className := strings.ToLower(cc)
		// Test for 3 or more characters per character class
		tagREs[className], err = regexp.Compile(fmt.Sprintf(`(?i)\p{%s}{3,}`, cc))
		if err != nil {
			return err
		}
	}
	return nil
}

func mergeTagRegexps(tagREs map[string]*regexp.Regexp, tags map[string]string) error {
	for tag, re := range tags {
		tagREs[tag], err = regexp.Compile("(?i)" + re)
		if err != nil {
			return err
		}
	}
	return nil
}

func createTag(tag string) (tagId int, err error) {
	err = DB.QueryRow(sqlSelectTag, tag).Scan(&tagId)
	if err == nil {
		if Config.Debug {
			fmt.Printf("Found existing tag %s\n", tag)
		}
	} else {
		err = DB.QueryRow(sqlInsertTag, tag).Scan(&tagId)
		if err != nil {
			fmt.Println(err)
			return -1, err
		}
		if Config.Debug {
			fmt.Printf("Created new tag %s\n", tag)
		}
	}
	return tagId, nil
}

func tagTorrent(t *Torrent) {
	ttags := make(map[string]bool)

	for tag, re := range tagREs {
		if re.MatchString(t.Name) {
			ttags[tag] = true
		}
		for _, f := range t.Files {
			if re.MatchString(f.Path) {
				ttags[tag] = true
			}
		}
	}
	// Make unique
	for tt := range ttags {
		t.Tags = append(t.Tags, tt)
	}
}

func hasTag(t Torrent, tag string) bool {
	for _, t := range t.Tags {
		if tag == t {
			return true
		}
	}
	return false
}

const (
	sqlSelectTag = `select id from tags where name = $1`
	sqlInsertTag = `insert into tags (name) values ($1) returning id`
)