-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
132 lines (109 loc) · 2.71 KB
/
processor.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package app
import (
"strconv"
"strings"
rift "github.com/dashotv/rift/client"
"github.com/dashotv/runic/internal/newznab"
"github.com/dashotv/runic/internal/parser"
"github.com/dashotv/runic/internal/reader"
)
func init() {
initializers = append(initializers, setupReader)
initializers = append(initializers, setupRift)
initializers = append(initializers, setupProcessor)
}
func setupProcessor(a *Application) error {
app.Processor = &Processor{
db: a.DB,
reader: a.Reader,
}
return nil
}
func setupReader(app *Application) error {
r := &reader.Reader{}
app.Reader = r
if err := r.Add("geek", app.Config.NZBGeekURL, app.Config.NZBGeekKey, 0, false); err != nil {
return err
}
if err := r.AddJackett(app.Config.JackettURL, app.Config.JackettKey); err != nil {
return err
}
return nil
}
func setupRift(app *Application) error {
r := rift.New(app.Config.RiftURL)
app.Rift = r
return nil
}
func catsToInt(cats []string) []int {
out := make([]int, len(cats))
for i, c := range cats {
out[i], _ = strconv.Atoi(c)
}
return out
}
type Processor struct {
db *Connector
reader *reader.Reader
}
func (p *Processor) Parse(source string, categories []int) ([]*Release, error) {
list, err := p.reader.Read(source, categories)
if err != nil {
return nil, err
}
return p.Process(source, list)
}
func (p *Processor) Process(source string, list []*newznab.NZB) ([]*Release, error) {
releases := []*Release{}
for _, nzb := range list {
t := reader.IdentifyType(catsToInt(nzb.Category))
if t == "" {
t = "unknown"
}
r := &Release{
Source: source,
Type: t,
Raw: nzb,
Download: nzb.DownloadURL,
Description: nzb.Description,
Size: nzb.Size,
PublishedAt: nzb.PubDate,
Infohash: nzb.InfoHash,
Checksum: nzb.ID,
}
if r.Checksum == "" && nzb.IsTorrent && nzb.InfoHash != "" {
r.Checksum = nzb.InfoHash
}
if nzb.IMDBTitle != "" {
r.Title = nzb.IMDBTitle
r.Year = nzb.IMDBYear
}
r.Downloader = "nzb"
if nzb.IsTorrent {
r.Downloader = "torrent"
}
if r.Downloader == "torrent" && r.Infohash != "" && strings.Contains(nzb.DownloadURL, app.Config.JackettURL) {
r.Download = "magnet:?xt=urn:btih:" + r.Infohash
}
info, err := parser.Parse(nzb.Title, t)
if err != nil {
return nil, err
}
r.Info = info
r.Title = info.Title
r.Season = info.Season
r.Episode = info.Episode
r.Year = info.Year
r.Group = info.Group
r.Website = info.Website
r.Resolution = info.Resolution
r.Quality = info.Quality
r.Encodings = info.Encodings
r.Unrated = info.Unrated
r.Uncensored = info.Uncensored
r.ThreeD = info.ThreeD
r.Bluray = info.Bluray
releases = append(releases, r)
}
return releases, nil
}