Skip to content

Commit

Permalink
Make logging of found torrents optional
Browse files Browse the repository at this point in the history
And false by default
  • Loading branch information
doingodswork committed Aug 23, 2020
1 parent 6c78d25 commit ed0ba73
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 54 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ Usage of deflix-stremio:
Prefix for environment variables
-extraHeadersRD string
Additional HTTP request headers to set for requests to RealDebrid, in a format like "X-Foo: bar", separated by newline characters ("\n")
-logFoundTorrents
Set to true to log each single torrent that was found by one of the torrent site clients (with DEBUG level)
-logLevel string
Log level to show only logs with the given and more severe levels. Can be "trace", "debug", "info", "warn", "error", "fatal", "panic". (default "debug")
Log level to show only logs with the given and more severe levels. Can be "debug", "info", "warn", "error". (default "debug")
-port int
Port to listen on (default 8080)
-rootURL string
Expand Down
13 changes: 12 additions & 1 deletion cmd/deflix-stremio/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type config struct {
BaseURLibit string `json:"baseURLibit"`
BaseURLrd string `json:"baseURLrd"`
LogLevel string `json:"logLevel"`
LogFoundTorrents bool `json:logFoundTorrents`
RootURL string `json:"rootURL"`
ExtraHeadersRD []string `json:"extraHeadersRD"`
SocksProxyAddrTPB string `json:"socksProxyAddrTPB"`
Expand All @@ -47,7 +48,8 @@ func parseConfig(logger *zap.Logger) config {
baseURL1337x = flag.String("baseURL1337x", "https://1337x.to", "Base URL for 1337x")
baseURLibit = flag.String("baseURLibit", "https://ibit.am", "Base URL for ibit")
baseURLrd = flag.String("baseURLrd", "https://api.real-debrid.com", "Base URL for RealDebrid")
logLevel = flag.String("logLevel", "debug", `Log level to show only logs with the given and more severe levels. Can be "trace", "debug", "info", "warn", "error", "fatal", "panic".`)
logLevel = flag.String("logLevel", "debug", `Log level to show only logs with the given and more severe levels. Can be "debug", "info", "warn", "error".`)
logFoundTorrents = flag.Bool("logFoundTorrents", false, "Set to true to log each single torrent that was found by one of the torrent site clients (with DEBUG level)")
rootURL = flag.String("rootURL", "https://www.deflix.tv", "Redirect target for the root")
extraHeadersRD = flag.String("extraHeadersRD", "", "Additional HTTP request headers to set for requests to RealDebrid, in a format like \"X-Foo: bar\", separated by newline characters (\"\\n\")")
socksProxyAddrTPB = flag.String("socksProxyAddrTPB", "", "SOCKS5 proxy address for accessing TPB, required for accessing TPB via the TOR network (where \"127.0.0.1:9050\" would be typical value)")
Expand Down Expand Up @@ -163,6 +165,15 @@ func parseConfig(logger *zap.Logger) config {
}
result.LogLevel = *logLevel

if !isArgSet("logFoundTorrents") {
if val, ok := os.LookupEnv(*envPrefix + "LOG_FOUND_TORRENTS"); ok {
if *logFoundTorrents, err = strconv.ParseBool(val); err != nil {
logger.Fatal("Couldn't convert environment variable from string to bool", zap.Error(err), zap.String("envVar", "LOG_FOUND_TORRENTS"))
}
}
}
result.LogFoundTorrents = *logFoundTorrents

if !isArgSet("rootURL") {
if val, ok := os.LookupEnv(*envPrefix + "ROOT_URL"); ok {
*rootURL = val
Expand Down
8 changes: 4 additions & 4 deletions cmd/deflix-stremio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,15 @@ func initClients(config config, logger *zap.Logger) {
rdClientOpts := realdebrid.NewClientOpts(config.BaseURLrd, timeout, config.CacheAgeRD, config.ExtraHeadersRD)

cinemetaClient = cinemeta.NewClient(cinemeta.DefaultClientOpts, cinemetaCache, logger)
tpbClient, err := imdb2torrent.NewTPBclient(tpbClientOpts, torrentCache, cinemetaClient, logger)
tpbClient, err := imdb2torrent.NewTPBclient(tpbClientOpts, torrentCache, cinemetaClient, logger, config.LogFoundTorrents)
if err != nil {
logger.Fatal("Couldn't create TPB client", zap.Error(err))
}
siteClients := map[string]imdb2torrent.MagnetSearcher{
"YTS": imdb2torrent.NewYTSclient(ytsClientOpts, torrentCache, logger),
"YTS": imdb2torrent.NewYTSclient(ytsClientOpts, torrentCache, logger, config.LogFoundTorrents),
"TPB": tpbClient,
"1337X": imdb2torrent.NewLeetxClient(leetxClientOpts, torrentCache, cinemetaClient, logger),
"ibit": imdb2torrent.NewIbitClient(ibitClientOpts, torrentCache, logger),
"1337X": imdb2torrent.NewLeetxClient(leetxClientOpts, torrentCache, cinemetaClient, logger, config.LogFoundTorrents),
"ibit": imdb2torrent.NewIbitClient(ibitClientOpts, torrentCache, logger, config.LogFoundTorrents),
}
searchClient = imdb2torrent.NewClient(siteClients, timeout, logger)
conversionClient, err = realdebrid.NewClient(rdClientOpts, tokenCache, availabilityCache, logger)
Expand Down
28 changes: 16 additions & 12 deletions pkg/imdb2torrent/1337x.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,26 @@ var DefaultLeetxClientOpts = LeetxClientOptions{
var _ MagnetSearcher = (*leetxClient)(nil)

type leetxClient struct {
baseURL string
httpClient *http.Client
cache Cache
cinemetaClient *cinemeta.Client
cacheAge time.Duration
logger *zap.Logger
baseURL string
httpClient *http.Client
cache Cache
cinemetaClient *cinemeta.Client
cacheAge time.Duration
logger *zap.Logger
logFoundTorrents bool
}

func NewLeetxClient(opts LeetxClientOptions, cache Cache, cinemetaClient *cinemeta.Client, logger *zap.Logger) *leetxClient {
func NewLeetxClient(opts LeetxClientOptions, cache Cache, cinemetaClient *cinemeta.Client, logger *zap.Logger, logFoundTorrents bool) *leetxClient {
return &leetxClient{
baseURL: opts.BaseURL,
httpClient: &http.Client{
Timeout: opts.Timeout,
},
cache: cache,
cinemetaClient: cinemetaClient,
cacheAge: opts.CacheAge,
logger: logger,
cache: cache,
cinemetaClient: cinemetaClient,
cacheAge: opts.CacheAge,
logger: logger,
logFoundTorrents: logFoundTorrents,
}
}

Expand Down Expand Up @@ -215,7 +217,9 @@ func (c *leetxClient) Find(ctx context.Context, imdbID string) ([]Result, error)
InfoHash: infoHash,
MagnetURL: magnet,
}
c.logger.Debug("Found torrent", zap.String("title", meta.Name), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnet), zapFieldID, zapFieldTorrentSite)
if c.logFoundTorrents {
c.logger.Debug("Found torrent", zap.String("title", meta.Name), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnet), zapFieldID, zapFieldTorrentSite)
}

resultChan <- result
}(torrentPageURL)
Expand Down
28 changes: 16 additions & 12 deletions pkg/imdb2torrent/ibit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,26 @@ var DefaultIbitClientOpts = IbitClientOptions{
var _ MagnetSearcher = (*ibitClient)(nil)

type ibitClient struct {
baseURL string
httpClient *http.Client
cache Cache
lock *sync.Mutex
cacheAge time.Duration
logger *zap.Logger
baseURL string
httpClient *http.Client
cache Cache
lock *sync.Mutex
cacheAge time.Duration
logger *zap.Logger
logFoundTorrents bool
}

func NewIbitClient(opts IbitClientOptions, cache Cache, logger *zap.Logger) *ibitClient {
func NewIbitClient(opts IbitClientOptions, cache Cache, logger *zap.Logger, logFoundTorrents bool) *ibitClient {
return &ibitClient{
baseURL: opts.BaseURL,
httpClient: &http.Client{
Timeout: opts.Timeout,
},
cache: cache,
lock: &sync.Mutex{},
cacheAge: opts.CacheAge,
logger: logger,
cache: cache,
lock: &sync.Mutex{},
cacheAge: opts.CacheAge,
logger: logger,
logFoundTorrents: logFoundTorrents,
}
}

Expand Down Expand Up @@ -219,7 +221,9 @@ func (c *ibitClient) Find(ctx context.Context, imdbID string) ([]Result, error)
InfoHash: infoHash,
MagnetURL: magnet,
}
c.logger.Debug("Found torrent", zap.String("title", title), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnet), zapFieldID, zapFieldTorrentSite)
if c.logFoundTorrents {
c.logger.Debug("Found torrent", zap.String("title", title), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnet), zapFieldID, zapFieldTorrentSite)
}

results = append(results, result)
}
Expand Down
32 changes: 18 additions & 14 deletions pkg/imdb2torrent/tpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ var DefaultTPBclientOpts = TPBclientOptions{
var _ MagnetSearcher = (*tpbClient)(nil)

type tpbClient struct {
baseURL string
httpClient *http.Client
cache Cache
cacheAge time.Duration
cinemetaClient *cinemeta.Client
logger *zap.Logger
baseURL string
httpClient *http.Client
cache Cache
cacheAge time.Duration
cinemetaClient *cinemeta.Client
logger *zap.Logger
logFoundTorrents bool
}

func NewTPBclient(opts TPBclientOptions, cache Cache, cinemetaClient *cinemeta.Client, logger *zap.Logger) (*tpbClient, error) {
func NewTPBclient(opts TPBclientOptions, cache Cache, cinemetaClient *cinemeta.Client, logger *zap.Logger, logFoundTorrents bool) (*tpbClient, error) {
// Using a SOCKS5 proxy allows us to make requests to TPB via the TOR network
var httpClient *http.Client
if opts.SocksProxyAddr != "" {
Expand All @@ -75,12 +76,13 @@ func NewTPBclient(opts TPBclientOptions, cache Cache, cinemetaClient *cinemeta.C
}
}
return &tpbClient{
baseURL: opts.BaseURL,
httpClient: httpClient,
cache: cache,
cacheAge: opts.CacheAge,
cinemetaClient: cinemetaClient,
logger: logger,
baseURL: opts.BaseURL,
httpClient: httpClient,
cache: cache,
cacheAge: opts.CacheAge,
cinemetaClient: cinemetaClient,
logger: logger,
logFoundTorrents: logFoundTorrents,
}, nil
}

Expand Down Expand Up @@ -161,7 +163,9 @@ func (c *tpbClient) Find(ctx context.Context, imdbID string) ([]Result, error) {
continue
}
magnetURL := createMagnetURL(ctx, infoHash, meta.Name, trackersTPB)
c.logger.Debug("Found torrent", zap.String("title", meta.Name), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnetURL), zapFieldID, zapFieldTorrentSite)
if c.logFoundTorrents {
c.logger.Debug("Found torrent", zap.String("title", meta.Name), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnetURL), zapFieldID, zapFieldTorrentSite)
}
result := Result{
Title: meta.Name,
Quality: quality,
Expand Down
24 changes: 14 additions & 10 deletions pkg/imdb2torrent/yts.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ var DefaultYTSclientOpts = YTSclientOptions{
var _ MagnetSearcher = (*ytsClient)(nil)

type ytsClient struct {
baseURL string
httpClient *http.Client
cache Cache
cacheAge time.Duration
logger *zap.Logger
baseURL string
httpClient *http.Client
cache Cache
cacheAge time.Duration
logger *zap.Logger
logFoundTorrents bool
}

func NewYTSclient(opts YTSclientOptions, cache Cache, logger *zap.Logger) *ytsClient {
func NewYTSclient(opts YTSclientOptions, cache Cache, logger *zap.Logger, logFoundTorrents bool) *ytsClient {
return &ytsClient{
baseURL: opts.BaseURL,
httpClient: &http.Client{
Timeout: opts.Timeout,
},
cache: cache,
cacheAge: opts.CacheAge,
logger: logger,
cache: cache,
cacheAge: opts.CacheAge,
logger: logger,
logFoundTorrents: logFoundTorrents,
}
}

Expand Down Expand Up @@ -121,7 +123,9 @@ func (c *ytsClient) Find(ctx context.Context, imdbID string) ([]Result, error) {
if ripType != "" {
quality += " (" + ripType + ")"
}
c.logger.Debug("Found torrent", zap.String("title", title), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnetURL), zapFieldID, zapFieldTorrentSite)
if c.logFoundTorrents {
c.logger.Debug("Found torrent", zap.String("title", title), zap.String("quality", quality), zap.String("infoHash", infoHash), zap.String("magnet", magnetURL), zapFieldID, zapFieldTorrentSite)
}
result := Result{
Title: title,
Quality: quality,
Expand Down

0 comments on commit ed0ba73

Please sign in to comment.