Skip to content

Commit 8ace2b4

Browse files
authored
refactor: receivers and var names (#131)
1 parent aece83a commit 8ace2b4

File tree

5 files changed

+69
-68
lines changed

5 files changed

+69
-68
lines changed

internal/http/health.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ func newHealthHandler() *healthHandler {
1616
return &healthHandler{}
1717
}
1818

19-
func (h healthHandler) Routes(r chi.Router) {
19+
func (h *healthHandler) Routes(r chi.Router) {
2020
r.Get("/liveness", h.handleLiveness)
2121
r.Get("/readiness", h.handleReadiness)
2222
}
2323

24-
func (h healthHandler) handleLiveness(w http.ResponseWriter, r *http.Request) {
24+
func (h *healthHandler) handleLiveness(w http.ResponseWriter, r *http.Request) {
2525
writeHealthy(w, r)
2626
}
2727

28-
func (h healthHandler) handleReadiness(w http.ResponseWriter, r *http.Request) {
28+
func (h *healthHandler) handleReadiness(w http.ResponseWriter, r *http.Request) {
2929
writeHealthy(w, r)
3030
}
3131

internal/http/processor.go

+57-57
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ type request struct {
3939
ClientName string
4040
}
4141

42-
type entryTime struct {
43-
e map[string][]domain.Entry
44-
d map[string]rls.Release
45-
t time.Time
46-
err error
42+
type torrentRlsEntries struct {
43+
entriesMap map[string][]domain.Entry
44+
rlsMap map[string]rls.Release
45+
lastUpdated time.Time
46+
err error
4747
sync.Mutex
4848
}
4949

@@ -68,80 +68,80 @@ func newProcessor(log logger.Logger, config *config.AppConfig, notification doma
6868
}
6969

7070
func (p *processor) getClient(client *domain.Client) error {
71-
s := qbittorrent.Config{
71+
clientCfg := qbittorrent.Config{
7272
Host: fmt.Sprintf("http://%s:%d", client.Host, client.Port),
7373
Username: client.Username,
7474
Password: client.Password,
7575
}
7676

77-
c, ok := clientMap.Load(s)
77+
c, ok := clientMap.Load(clientCfg)
7878
if !ok {
79-
c = qbittorrent.NewClient(s)
79+
c = qbittorrent.NewClient(clientCfg)
8080

8181
if err := c.(*qbittorrent.Client).Login(); err != nil {
8282
return errors.Wrap(err, "failed to login to qbittorrent")
8383
}
8484

85-
clientMap.Store(s, c)
85+
clientMap.Store(clientCfg, c)
8686
}
8787

8888
p.req.Client = c.(*qbittorrent.Client)
8989
return nil
9090
}
9191

92-
func (p *processor) getAllTorrents(client *domain.Client) entryTime {
93-
set := qbittorrent.Config{
92+
func (p *processor) getAllTorrents(client *domain.Client) torrentRlsEntries {
93+
clientCfg := qbittorrent.Config{
9494
Host: fmt.Sprintf("http://%s:%d", client.Host, client.Port),
9595
Username: client.Username,
9696
Password: client.Password,
9797
}
9898

99-
f := func() *entryTime {
100-
te, ok := torrentMap.Load(set)
99+
f := func() *torrentRlsEntries {
100+
tre, ok := torrentMap.Load(clientCfg)
101101
if ok {
102-
return te.(*entryTime)
102+
return tre.(*torrentRlsEntries)
103103
}
104104

105-
res := &entryTime{d: make(map[string]rls.Release)}
106-
torrentMap.Store(set, res)
107-
return res
105+
entries := &torrentRlsEntries{rlsMap: make(map[string]rls.Release)}
106+
torrentMap.Store(clientCfg, entries)
107+
return entries
108108
}
109109

110-
res := f()
110+
entries := f()
111111
cur := time.Now()
112-
if res.t.After(cur) {
113-
return *res
112+
if entries.lastUpdated.After(cur) {
113+
return *entries
114114
}
115115

116-
res.Lock()
117-
defer res.Unlock()
116+
entries.Lock()
117+
defer entries.Unlock()
118118

119-
res = f()
120-
if res.t.After(cur) {
121-
return *res
119+
entries = f()
120+
if entries.lastUpdated.After(cur) {
121+
return *entries
122122
}
123123

124124
ts, err := p.req.Client.GetTorrents(qbittorrent.TorrentFilterOptions{})
125125
if err != nil {
126-
return entryTime{err: err}
126+
return torrentRlsEntries{err: err}
127127
}
128128

129-
nt := time.Now()
130-
res = &entryTime{e: make(map[string][]domain.Entry), t: nt.Add(nt.Sub(cur)), d: res.d}
129+
after := time.Now()
130+
entries = &torrentRlsEntries{entriesMap: make(map[string][]domain.Entry), lastUpdated: after.Add(after.Sub(cur)), rlsMap: entries.rlsMap}
131131

132132
for _, t := range ts {
133-
r, ok := res.d[t.Name]
133+
r, ok := entries.rlsMap[t.Name]
134134
if !ok {
135135
r = rls.ParseString(t.Name)
136-
res.d[t.Name] = r
136+
entries.rlsMap[t.Name] = r
137137
}
138138

139-
s := utils.GetFormattedTitle(r)
140-
res.e[s] = append(res.e[s], domain.Entry{T: t, R: r})
139+
fmtTitle := utils.GetFormattedTitle(r)
140+
entries.entriesMap[fmtTitle] = append(entries.entriesMap[fmtTitle], domain.Entry{T: t, R: r})
141141
}
142142

143-
torrentMap.Store(set, res)
144-
return *res
143+
torrentMap.Store(clientCfg, entries)
144+
return *entries
145145
}
146146

147147
func (p *processor) getFiles(hash string) (*qbittorrent.TorrentFiles, error) {
@@ -217,76 +217,76 @@ func (p *processor) processSeasonPack() (int, error) {
217217
return domain.StatusGetClientError, err
218218
}
219219

220-
mp := p.getAllTorrents(client)
221-
if mp.err != nil {
222-
return domain.StatusGetTorrentsError, mp.err
220+
tre := p.getAllTorrents(client)
221+
if tre.err != nil {
222+
return domain.StatusGetTorrentsError, tre.err
223223
}
224224

225-
requestRls := domain.Entry{R: rls.ParseString(p.req.Name)}
226-
v, ok := mp.e[utils.GetFormattedTitle(requestRls.R)]
225+
requestEntry := domain.Entry{R: rls.ParseString(p.req.Name)}
226+
matchingEntries, ok := tre.entriesMap[utils.GetFormattedTitle(requestEntry.R)]
227227
if !ok {
228228
return domain.StatusNoMatches, fmt.Errorf("no matching releases in client")
229229
}
230230

231231
announcedPackName := utils.FormatSeasonPackTitle(p.req.Name)
232232
p.log.Debug().Msgf("formatted season pack name: %s", announcedPackName)
233233

234-
for _, child := range v {
235-
if release.CheckCandidates(&requestRls, &child, p.cfg.Config.FuzzyMatching) == domain.StatusAlreadyInClient {
234+
for _, entry := range matchingEntries {
235+
if release.CheckCandidates(&requestEntry, &entry, p.cfg.Config.FuzzyMatching) == domain.StatusAlreadyInClient {
236236
return domain.StatusAlreadyInClient, fmt.Errorf("release already in client")
237237
}
238238
}
239239

240240
var matchedEps []int
241241
var respCodes []int
242242

243-
for _, child := range v {
244-
switch res := release.CheckCandidates(&requestRls, &child, p.cfg.Config.FuzzyMatching); res {
243+
for _, entry := range matchingEntries {
244+
switch res := release.CheckCandidates(&requestEntry, &entry, p.cfg.Config.FuzzyMatching); res {
245245
case domain.StatusResolutionMismatch:
246246
p.log.Info().Msgf("resolution did not match: request(%s => %s), client(%s => %s)",
247-
requestRls.R.String(), requestRls.R.Resolution, child.R.String(), child.R.Resolution)
247+
requestEntry.R.String(), requestEntry.R.Resolution, entry.R.String(), entry.R.Resolution)
248248
respCodes = append(respCodes, res)
249249
continue
250250

251251
case domain.StatusSourceMismatch:
252252
p.log.Info().Msgf("source did not match: request(%s => %s), client(%s => %s)",
253-
requestRls.R.String(), requestRls.R.Source, child.R.String(), child.R.Source)
253+
requestEntry.R.String(), requestEntry.R.Source, entry.R.String(), entry.R.Source)
254254
respCodes = append(respCodes, res)
255255
continue
256256

257257
case domain.StatusRlsGrpMismatch:
258258
p.log.Info().Msgf("release group did not match: request(%s => %s), client(%s => %s)",
259-
requestRls.R.String(), requestRls.R.Group, child.R.String(), child.R.Group)
259+
requestEntry.R.String(), requestEntry.R.Group, entry.R.String(), entry.R.Group)
260260
respCodes = append(respCodes, res)
261261
continue
262262

263263
case domain.StatusCutMismatch:
264264
p.log.Info().Msgf("cut did not match: request(%s => %s), client(%s => %s)",
265-
requestRls.R.String(), requestRls.R.Cut, child.R.String(), child.R.Cut)
265+
requestEntry.R.String(), requestEntry.R.Cut, entry.R.String(), entry.R.Cut)
266266
respCodes = append(respCodes, res)
267267
continue
268268

269269
case domain.StatusEditionMismatch:
270270
p.log.Info().Msgf("edition did not match: request(%s => %s), client(%s => %s)",
271-
requestRls.R.String(), requestRls.R.Edition, child.R.String(), child.R.Edition)
271+
requestEntry.R.String(), requestEntry.R.Edition, entry.R.String(), entry.R.Edition)
272272
respCodes = append(respCodes, res)
273273
continue
274274

275275
case domain.StatusRepackStatusMismatch:
276276
p.log.Info().Msgf("repack status did not match: request(%s => %s), client(%s => %s)",
277-
requestRls.R.String(), requestRls.R.Other, child.R.String(), child.R.Other)
277+
requestEntry.R.String(), requestEntry.R.Other, entry.R.String(), entry.R.Other)
278278
respCodes = append(respCodes, res)
279279
continue
280280

281281
case domain.StatusHdrMismatch:
282282
p.log.Info().Msgf("hdr metadata did not match: request(%s => %s), client(%s => %s)",
283-
requestRls.R.String(), requestRls.R.HDR, child.R.String(), child.R.HDR)
283+
requestEntry.R.String(), requestEntry.R.HDR, entry.R.String(), entry.R.HDR)
284284
respCodes = append(respCodes, res)
285285
continue
286286

287287
case domain.StatusStreamingServiceMismatch:
288288
p.log.Info().Msgf("streaming service did not match: request(%s => %s), client(%s => %s)",
289-
requestRls.R.String(), requestRls.R.Collection, child.R.String(), child.R.Collection)
289+
requestEntry.R.String(), requestEntry.R.Collection, entry.R.String(), entry.R.Collection)
290290
respCodes = append(respCodes, res)
291291
continue
292292

@@ -297,9 +297,9 @@ func (p *processor) processSeasonPack() (int, error) {
297297
return domain.StatusNotASeasonPack, fmt.Errorf("release is not a season pack")
298298

299299
case domain.StatusSuccessfulMatch:
300-
torrentFiles, err := p.getFiles(child.T.Hash)
300+
torrentFiles, err := p.getFiles(entry.T.Hash)
301301
if err != nil {
302-
p.log.Error().Err(err).Msgf("error getting files: %s", child.T.Name)
302+
p.log.Error().Err(err).Msgf("error getting files: %s", entry.T.Name)
303303
continue
304304
}
305305

@@ -315,12 +315,12 @@ func (p *processor) processSeasonPack() (int, error) {
315315
break
316316
}
317317
if len(fileName) == 0 || size == 0 {
318-
p.log.Error().Err(err).Msgf("error getting filename or size: %s", child.T.Name)
318+
p.log.Error().Err(err).Msgf("error getting filename or size: %s", entry.T.Name)
319319
continue
320320
}
321321

322-
epRls := rls.ParseString(child.T.Name)
323-
epPathClient := filepath.Join(child.T.SavePath, fileName)
322+
epRls := rls.ParseString(entry.T.Name)
323+
epPathClient := filepath.Join(entry.T.SavePath, fileName)
324324
announcedEpPath := filepath.Join(client.PreImportPath, announcedPackName, filepath.Base(fileName))
325325

326326
matchedEps = append(matchedEps, epRls.Episode)
@@ -341,7 +341,7 @@ func (p *processor) processSeasonPack() (int, error) {
341341
newMatches := append(oldMatches.([]matchPaths), currentMatch...)
342342
matchesMap.Store(p.req.Name, newMatches)
343343
p.log.Debug().Msgf("matched torrent from client: name(%s), size(%d), hash(%s)",
344-
child.T.Name, size, child.T.Hash)
344+
entry.T.Name, size, entry.T.Hash)
345345
respCodes = append(respCodes, res)
346346
continue
347347
}

internal/http/webhook.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ func newWebhookHandler(log logger.Logger, cfg *config.AppConfig, notification do
2828
}
2929
}
3030

31-
func (h webhookHandler) Routes(r chi.Router) {
31+
func (h *webhookHandler) Routes(r chi.Router) {
3232
r.Post("/pack", h.pack)
3333
r.Post("/parse", h.parse)
3434
}
3535

36-
func (h webhookHandler) pack(w http.ResponseWriter, r *http.Request) {
36+
func (h *webhookHandler) pack(w http.ResponseWriter, r *http.Request) {
3737
newProcessor(h.log, h.cfg, h.noti).ProcessSeasonPackHandler(w, r)
3838
render.Status(r, http.StatusOK)
3939
}
4040

41-
func (h webhookHandler) parse(w http.ResponseWriter, r *http.Request) {
41+
func (h *webhookHandler) parse(w http.ResponseWriter, r *http.Request) {
4242
newProcessor(h.log, h.cfg, h.noti).ParseTorrentHandler(w, r)
4343
render.Status(r, http.StatusOK)
4444
}

internal/release/release.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"github.com/moistari/rls"
1111
)
1212

13-
func CheckCandidates(requestrls, child *domain.Entry, fuzzyMatching domain.FuzzyMatching) int {
14-
rlsRelease := requestrls.R
15-
rlsInClient := child.R
13+
func CheckCandidates(requestEntry, clientEntry *domain.Entry, fuzzyMatching domain.FuzzyMatching) int {
14+
requestRls := requestEntry.R
15+
clientRls := clientEntry.R
1616

1717
// check if season pack and no extension
18-
if rlsRelease.Type.Is(rls.Series) && rlsRelease.Ext == "" {
18+
if requestRls.Type.Is(rls.Series) && requestRls.Ext == "" {
1919
// compare releases
20-
return compareReleases(rlsInClient, rlsRelease, fuzzyMatching)
20+
return compareReleases(clientRls, requestRls, fuzzyMatching)
2121
}
2222
// not a season pack
2323
return 211

internal/utils/tvmaze.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package utils
55

66
import (
77
"fmt"
8+
89
"github.com/mrobinsn/go-tvmaze/tvmaze"
910
)
1011

0 commit comments

Comments
 (0)