diff options
author | Parker M <contact@pkrm.dev> | 2023-09-16 21:52:24 -0500 |
---|---|---|
committer | Parker M <contact@pkrm.dev> | 2023-09-16 21:52:24 -0500 |
commit | 0fbdd7fced2c445a425621f3b106f257748dc0ba (patch) | |
tree | ab1451a8e4fd20d75624b282ca5a283367f805ef /app/db_removal.py | |
parent | 1edb565c2d8c3f8f17d511aea99dd0d914abc2d8 (diff) |
commit
Diffstat (limited to 'app/db_removal.py')
-rw-r--r-- | app/db_removal.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/app/db_removal.py b/app/db_removal.py new file mode 100644 index 0000000..e03ea2a --- /dev/null +++ b/app/db_removal.py @@ -0,0 +1,33 @@ +from apscheduler.schedulers.background import BackgroundScheduler +import sqlite3 +import requests + +from initialize_variables import radarr_host, headers + +# Remove all entries from the database of movies that have already finished downloading +# This helps to stop from entries building up in the database and slowing down everything +sched = BackgroundScheduler(daemon=True) +@sched.scheduled_job('cron', hour='0', minute='0') +def clear_database(): + db = sqlite3.connect('/data/movies.db') + cursor = db.cursor() + # First get all of the movie ids in the database + cursor.execute(''' + SELECT movie_id FROM movies + ''') + movie_ids = cursor.fetchall() + # Get all of the movie_ids that are currently downloading/queued and/or missing + response = requests.get(f'{radarr_host}/api/v3/queue/', headers=headers) + current_movie_ids = [] + for movie in response.json()['records']: + current_movie_ids.append(str(movie['movieId'])) + + # Loop through the movie_ids in the database, if they are not in the current_movie_ids list, + # that means they are not currently downloading/queued, so delete them from the database + for movie_id in movie_ids: + if movie_id[0] not in current_movie_ids: + cursor.execute(''' + DELETE FROM movies WHERE movie_id = ? + ''', (movie_id[0],)) + db.commit() + db.close()
\ No newline at end of file |