Skip to content

Commit

Permalink
Purge cached recommended shows afte x days (#10352)
Browse files Browse the repository at this point in the history
* Purge cached recommended shows afte x days

* Fix test

* Fix flake

* snapshot updated

* spaces
  • Loading branch information
p0psicles authored Feb 18, 2022
1 parent 18a5e65 commit 9b6680f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 8 deletions.
2 changes: 2 additions & 0 deletions medusa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ def initialize(self, console_logging=True):
0, min(23, check_setting_int(app.CFG, 'Recommended', 'recommended_show_update_hour', app.DEFAULT_RECOMMENDED_SHOW_UPDATE_HOUR))
)
app.CACHE_RECOMMENDED_TRAKT_LISTS = check_setting_list(app.CFG, 'Recommended', 'trakt_lists', app.CACHE_RECOMMENDED_TRAKT_LISTS)
app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS = check_setting_int(app.CFG, 'Recommended', 'purge_after_days', 180)

# Initialize trakt config path.
trakt.core.CONFIG_PATH = os.path.join(app.CACHE_DIR, '.pytrakt.json')
Expand Down Expand Up @@ -1749,6 +1750,7 @@ def save_config():
new_config['Recommended']['cache_anilist'] = app.CACHE_RECOMMENDED_ANILIST
new_config['Recommended']['recommended_show_update_hour'] = int(app.RECOMMENDED_SHOW_UPDATE_HOUR)
new_config['Recommended']['trakt_lists'] = app.CACHE_RECOMMENDED_TRAKT_LISTS
new_config['Recommended']['purge_after_days'] = int(app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS)

new_config['Blackhole'] = {}
new_config['Blackhole']['nzb_dir'] = app.NZB_DIR
Expand Down
1 change: 1 addition & 0 deletions medusa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ def __init__(self):
'trending', 'popular', 'anticipated', 'collected',
'watched', 'played', 'recommendations', 'newshow', 'newseason'
]
self.CACHE_RECOMMENDED_PURGE_AFTER_DAYS = 180

def _init_scheduler(self, app_prop=None, scheduler=None, enabled=None):
from medusa.logger.adapters.style import BraceAdapter
Expand Down
21 changes: 20 additions & 1 deletion medusa/generic_update_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import logging
from datetime import date, datetime, timedelta

from medusa import app, ws
from medusa import app, db, ws
from medusa.helper.exceptions import CantUpdateRecommendedShowsException
from medusa.logger.adapters.style import BraceAdapter
from medusa.queues import generic_queue
Expand Down Expand Up @@ -136,6 +136,23 @@ def __init__(self, update_action):
self.started = False
self.success = False

def _purge_after_days(self):
log.info('Purge shows that have been added more then {days} ago', {'days': app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS})
if not app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS:
return

sql = """
DELETE FROM shows
WHERE added < datetime('now', '-{days} days')
""".format(days=app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS)
params = []

if self.recommended_list != GenericQueueActions.UPDATE_RECOMMENDED_LIST_ALL:
sql += ' AND source = ?'
params = [self.recommended_list]

db.DBConnection('recommended.db').action(sql, params)

def _get_trakt_shows(self):
"""Get Trakt shows."""
if self.recommended_list not in (
Expand Down Expand Up @@ -213,6 +230,8 @@ def run(self):
# Update recommended shows from trakt, imdb and anidb
# recommended shows are dogpilled into cache/recommended.dbm

self._purge_after_days()

log.info(u'Started caching recommended shows')

self._get_trakt_shows()
Expand Down
2 changes: 2 additions & 0 deletions medusa/server/api/v2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class ConfigHandler(BaseRequestHandler):
'recommended.cache.imdb': BooleanField(app, 'CACHE_RECOMMENDED_IMDB'),
'recommended.cache.anidb': BooleanField(app, 'CACHE_RECOMMENDED_ANIDB'),
'recommended.cache.anilist': BooleanField(app, 'CACHE_RECOMMENDED_ANILIST'),
'recommended.cache.purgeAfterDays': IntegerField(app, 'CACHE_RECOMMENDED_PURGE_AFTER_DAYS'),
'recommended.trakt.selectedLists': ListField(app, 'CACHE_RECOMMENDED_TRAKT_LISTS'),

# Sections
Expand Down Expand Up @@ -709,6 +710,7 @@ def data_main():
section_data['recommended']['cache']['imdb'] = bool(app.CACHE_RECOMMENDED_IMDB)
section_data['recommended']['cache']['anidb'] = bool(app.CACHE_RECOMMENDED_ANIDB)
section_data['recommended']['cache']['anilist'] = bool(app.CACHE_RECOMMENDED_ANILIST)
section_data['recommended']['cache']['purgeAfterDays'] = int(app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS)
section_data['recommended']['trakt']['selectedLists'] = app.CACHE_RECOMMENDED_TRAKT_LISTS
section_data['recommended']['trakt']['availableLists'] = TraktPopular.CATEGORIES

Expand Down
1 change: 1 addition & 0 deletions tests/apiv2/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def config_main(monkeypatch, app_config):
section_data['recommended']['cache']['imdb'] = bool(app.CACHE_RECOMMENDED_IMDB)
section_data['recommended']['cache']['anidb'] = bool(app.CACHE_RECOMMENDED_ANIDB)
section_data['recommended']['cache']['anilist'] = bool(app.CACHE_RECOMMENDED_ANILIST)
section_data['recommended']['cache']['purgeAfterDays'] = int(app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS)
section_data['recommended']['trakt']['selectedLists'] = app.CACHE_RECOMMENDED_TRAKT_LISTS
section_data['recommended']['trakt']['availableLists'] = TraktPopular.CATEGORIES

Expand Down
5 changes: 5 additions & 0 deletions themes-default/slim/src/components/config-general.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<config-toggle-slider v-model="general.recommended.cache.imdb" label="Cache Imdb lists" id="cache_rec_imdb" />
<config-toggle-slider v-model="general.recommended.cache.anidb" label="Cache Anidb lists" id="cache_rec_anidb" />
<config-toggle-slider v-model="general.recommended.cache.anilist" label="Cache AniList lists" id="cache_rec_anilist" />

<config-textbox-number v-model="general.recommended.cache.purgeAfterDays" label="Purge shows from db after x days" id="cache_purge_days" :min="0" :step="1">
<p>Number of days to keep shows in the cache (default: 180) <b>(0 will not purge shows at all)</b></p>
</config-textbox-number>

</template>
</fieldset>
</div>
Expand Down
3 changes: 2 additions & 1 deletion themes-default/slim/src/store/modules/config/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const state = {
trakt: null,
imdb: null,
anidb: null,
anilist: null
anilist: null,
purgeAfterDays: null
},
trakt: {
selectedLists: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ exports[`ConfigGeneral.test.js renders 1`] = `
label="Cache AniList lists"
value="true"
/>
<config-textbox-number-stub
explanations=""
id="cache_purge_days"
inputclass="form-control input-sm input75"
label="Purge shows from db after x days"
min="0"
placeholder=""
step="1"
value="10"
>
<p>
Number of days to keep shows in the cache (default: 180)
<b>
(0 will not purge shows at all)
</b>
</p>
</config-textbox-number-stub>
</fieldset>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions themes/dark/assets/js/medusa-runtime.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions themes/light/assets/js/medusa-runtime.js

Large diffs are not rendered by default.

0 comments on commit 9b6680f

Please sign in to comment.