autobrr/internal/list/process_list_plaintext.go
ze0s 221bc35371
feat(lists): integrate Omegabrr (#1885)
* feat(lists): integrate Omegabrr

* feat(lists): add missing lists index

* feat(lists): add db repo

* feat(lists): add db migrations

* feat(lists): labels

* feat(lists): url lists and more arrs

* fix(lists): db migrations client_id wrong type

* fix(lists): db fields

* feat(lists): create list form wip

* feat(lists): show in list and create

* feat(lists): update and delete

* feat(lists): trigger via webhook

* feat(lists): add webhook handler

* fix(arr): encode json to pointer

* feat(lists): rename endpoint to lists

* feat(lists): fetch tags from arr

* feat(lists): process plaintext lists

* feat(lists): add background refresh job

* run every 6th hour with a random start delay between 1-35 seconds

* feat(lists): refresh on save and improve logging

* feat(lists): cast arr client to pointer

* feat(lists): improve error handling

* feat(lists): reset shows field with match release

* feat(lists): filter opts all lists

* feat(lists): trigger on update if enabled

* feat(lists): update option for lists

* feat(lists): show connected filters in list

* feat(lists): missing listSvc dep

* feat(lists): cleanup

* feat(lists): typo arr list

* feat(lists): radarr include original

* feat(lists): rename ExcludeAlternateTitle to IncludeAlternateTitle

* fix(lists): arr client type conversion to pointer

* fix(actions): only log panic recover if err not nil

* feat(lists): show spinner on save

* feat(lists): show icon in filters list

* feat(lists): change icon color in filters list

* feat(lists): delete relations on filter delete
2024-12-25 13:23:37 +01:00

96 lines
2.4 KiB
Go

package list
import (
"context"
"io"
"net/http"
"strings"
"github.com/autobrr/autobrr/internal/domain"
"github.com/pkg/errors"
)
func (s *service) plaintext(ctx context.Context, list *domain.List) error {
l := s.log.With().Str("type", "plaintext").Str("list", list.Name).Logger()
if list.URL == "" {
return errors.New("no URL provided for plaintext")
}
l.Debug().Msgf("fetching titles from %s", list.URL)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, list.URL, nil)
if err != nil {
return errors.Wrapf(err, "could not make new request for URL: %s", list.URL)
}
list.SetRequestHeaders(req)
//setUserAgent(req)
resp, err := s.httpClient.Do(req)
if err != nil {
return errors.Wrapf(err, "failed to fetch titles from URL: %s", list.URL)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Wrapf(err, "failed to fetch titles from URL: %s", list.URL)
}
contentType := resp.Header.Get("Content-Type")
if !strings.HasPrefix(contentType, "text/plain") {
return errors.Wrapf(err, "unexpected content type for URL: %s expected text/plain got %s", list.URL, contentType)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "failed to read response body from URL: %s", list.URL)
}
var titles []string
titleLines := strings.Split(string(body), "\n")
for _, titleLine := range titleLines {
title := strings.TrimSpace(titleLine)
if title == "" {
continue
}
titles = append(titles, title)
}
filterTitles := []string{}
for _, title := range titles {
filterTitles = append(filterTitles, processTitle(title, list.MatchRelease)...)
}
if len(filterTitles) == 0 {
l.Debug().Msgf("no titles found to update for list: %v", list.Name)
return nil
}
joinedTitles := strings.Join(filterTitles, ",")
l.Trace().Str("titles", joinedTitles).Msgf("found %d titles", len(joinedTitles))
filterUpdate := domain.FilterUpdate{Shows: &joinedTitles}
if list.MatchRelease {
filterUpdate.Shows = &nullString
filterUpdate.MatchReleases = &joinedTitles
}
for _, filter := range list.Filters {
l.Debug().Msgf("updating filter: %v", filter.ID)
filterUpdate.ID = filter.ID
if err := s.filterSvc.UpdatePartial(ctx, filterUpdate); err != nil {
return errors.Wrapf(err, "error updating filter: %v", filter.ID)
}
l.Debug().Msgf("successfully updated filter: %v", filter.ID)
}
return nil
}