Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
providers: addic7ed: properly implement limits #723
Browse files Browse the repository at this point in the history
  • Loading branch information
pannal committed Apr 13, 2020
1 parent ea03f3f commit 289f174
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions Contents/Libraries/Shared/subliminal_patch/providers/addic7ed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
import re
import datetime
import types

import pytz
import subliminal
import time

Expand Down Expand Up @@ -402,31 +402,22 @@ def query(self, show_id, series, season, year=None, country=None):
return subtitles

def download_subtitle(self, subtitle):
last_dl = region.get("addic7ed_dl_ts")
last_reset = region.get("addic7ed_dl_ts_lr")
amount = region.get("addic7ed_dl_amount")
if amount is NO_VALUE:
amount = 0
last_dls = region.get("addic7ed_dls")
now = datetime.datetime.now()
one_day = datetime.timedelta(hours=24)

if last_reset is NO_VALUE:
last_reset = "never"
if not isinstance(last_dls, types.ListType):
last_dls = []
else:
# filter all non-expired DLs
last_dls = filter(lambda t: t + one_day > now, last_dls)
region.set("addic7ed_dls", last_dls)

cap = self.vip and 80 or 40

# we may be falsely assuming germany here; also they might just use UTC.
germany_now = datetime.datetime.now(tz=pytz.timezone("Europe/Berlin"))
midnight = datetime.datetime.combine(germany_now, datetime.time.min)

# reset at night
if last_dl and last_dl != NO_VALUE and last_dl <= midnight:
logger.info("Reset dl amount (max: %s)", cap)
region.set("addic7ed_dl_amount", 0)
region.set("addic7ed_dl_ts_lr", germany_now)
last_reset = germany_now
amount = 0
amount = len(last_dls)

if amount >= cap:
logger.info("Used %s/%s of downloads since %s", (amount, cap, last_dl))
logger.info("Addic7ed: Downloads per day exceeded (%s)", cap)
raise DownloadLimitPerDayExceeded

# download the subtitle
Expand All @@ -440,14 +431,14 @@ def download_subtitle(self, subtitle):
if not r.content:
# Provider wrongful return a status of 304 Not Modified with an empty content
# raise_for_status won't raise exception for that status code
logger.error('Unable to download subtitle. No data returned from provider')
logger.error('Addic7ed: Unable to download subtitle. No data returned from provider')
return

# detect download limit exceeded
if r.headers['Content-Type'] == 'text/html':
raise DownloadLimitExceeded

subtitle.content = fix_line_ending(r.content)
region.set("addic7ed_dl_amount", amount + 1)
region.set("addic7ed_dl_ts", germany_now)
logger.info("Used %s/%s of downloads since %s", (amount + 1, cap, last_reset))
last_dls.append(datetime.datetime.now())
region.set("addic7ed_dls", last_dls)
logger.info("Addic7ed: Used %s/%s downloads", amount+1, cap)

0 comments on commit 289f174

Please sign in to comment.