diff --git a/docs/Settings.md b/docs/Settings.md
index ddb62d66..35c38930 100644
--- a/docs/Settings.md
+++ b/docs/Settings.md
@@ -19,6 +19,7 @@ Settings related to the download process.
- **Download Speed Limit**: Set your download speed limit in KB/s. This will pass the option `--limit-rate` to yt-dlp.
- **Throttled Rate Limit**: Restart download if the download speed drops below this value in KB/s. This will pass the option `--throttled-rate` to yt-dlp. Using this option might have a negative effect if you have an unstable or slow internet connection.
- **Sleep Interval**: Time in seconds to sleep between requests to YouTube. It's a good idea to set this to **3** seconds. Might be necessary to avoid throttling.
+- **Auto Delete Watched Videos**: Automatically delete videos marked as watched after selected days. If activated, checks your videos after download task is finished.
## Download Format
Additional settings passed to yt-dlp.
diff --git a/tubearchivist/home/config.json b/tubearchivist/home/config.json
index def21ec5..bf754f4c 100644
--- a/tubearchivist/home/config.json
+++ b/tubearchivist/home/config.json
@@ -18,6 +18,7 @@
"limit_count": false,
"limit_speed": false,
"sleep_interval": 3,
+ "autodelete_days": false,
"format": false,
"add_metadata": false,
"add_thumbnail": false,
diff --git a/tubearchivist/home/forms.py b/tubearchivist/home/forms.py
index 6f144bba..f39bb8bc 100644
--- a/tubearchivist/home/forms.py
+++ b/tubearchivist/home/forms.py
@@ -60,6 +60,7 @@ class ApplicationSettingsForm(forms.Form):
downloads_add_thumbnail = forms.ChoiceField(
widget=forms.Select, choices=THUMBNAIL_CHOICES, required=False
)
+ downloads_autodelete_days = forms.IntegerField(required=False)
class SchedulerSettingsForm(forms.Form):
diff --git a/tubearchivist/home/src/download.py b/tubearchivist/home/src/download.py
index fd2ffbb2..1d34128f 100644
--- a/tubearchivist/home/src/download.py
+++ b/tubearchivist/home/src/download.py
@@ -25,6 +25,7 @@ from home.src.index import (
IndexPaginate,
YoutubeChannel,
YoutubePlaylist,
+ YoutubeVideo,
index_new_video,
)
@@ -554,6 +555,11 @@ class VideoDownloader:
self.move_to_archive(vid_dict)
self.delete_from_pending(youtube_id)
+ autodelete_days = self.config["downloads"]["autodelete_days"]
+ if autodelete_days:
+ print(f"auto delete older than {autodelete_days} days")
+ self.auto_delete_watched(autodelete_days)
+
@staticmethod
def add_pending():
"""add pending videos to download queue"""
@@ -772,3 +778,19 @@ class VideoDownloader:
)
else:
RedisArchivist().set_message("message:download", mess_dict)
+
+ @staticmethod
+ def auto_delete_watched(autodelete_days):
+ """delete watched videos after x days"""
+ now = int(datetime.now().strftime("%s"))
+ now_lte = now - autodelete_days * 24 * 60 * 60
+ data = {
+ "query": {"range": {"player.watched_date": {"lte": now_lte}}},
+ "sort": [{"player.watched_date": {"order": "asc"}}]
+ }
+ all_to_delete = IndexPaginate("ta_video", data).get_results()
+
+ for to_delete in all_to_delete:
+ youtube_id = to_delete["youtube_id"]
+ print(f"autodelete {youtube_id}")
+ YoutubeVideo(youtube_id).delete_media_file()
diff --git a/tubearchivist/home/templates/home/settings.html b/tubearchivist/home/templates/home/settings.html
index f88a2631..e1b4ce98 100644
--- a/tubearchivist/home/templates/home/settings.html
+++ b/tubearchivist/home/templates/home/settings.html
@@ -59,6 +59,11 @@
Seconds to sleep between calls to YouTube. Might be necessary to avoid throttling. Recommended 3.
{{ app_form.downloads_sleep_interval }}
+
Danger Zone: Current auto delete watched videos: {{ config.downloads.autodelete_days }}
+ Auto delete watched videos after x days, 0 (zero) to deactivate: