Last active
April 19, 2023 07:58
-
-
Save stefanbc/f3eb7e3d885bbe591e61ae36c664eab5 to your computer and use it in GitHub Desktop.
Python script that deletes watched videos from a Plex Media Server, writes the results to a log file and sends a notification via IFTTT webhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import requests | |
import json | |
import xmltodict | |
from datetime import datetime | |
# Set the script properties | |
PLEX_URL = 'http://localhost:32400' | |
PLEX_TOKEN = '<your_plex_token_here>' | |
PLEX_LIBRARY = <your_plex_library_id> | |
IFTTT_WEBHOOK = '<your_ifttt_webhook_url_here>' | |
# Set the log file name | |
LOG_FILE = 'plex-cleanup.log' | |
LOG_FILE_MAX_SIZE = 2000000 # 2 MB | |
LOG_FILE_CURRENT_SIZE = os.path.getsize(LOG_FILE) | |
# Main cleanup method | |
def cleanup(): | |
# Check if the log file is larger than LOG_FILE_MAX_SIZE and empty it | |
if LOG_FILE_CURRENT_SIZE > LOG_FILE_MAX_SIZE: | |
with open(LOG_FILE, "w") as log_file: | |
log_file.truncate(0) | |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
# Fetch the Plex data | |
response = requests.get(f"{PLEX_URL}/library/sections/{PLEX_LIBRARY}/allLeaves", headers={"X-Plex-Token": PLEX_TOKEN}) | |
data = xmltodict.parse(response.content) | |
videos = data['MediaContainer']['Video'] | |
if videos: | |
watched_videos = [] | |
# Filter watched videos | |
if isinstance(videos, list): | |
watched_videos = [video for video in videos if video.get('@viewCount') and int(video['@viewCount']) >= 1] | |
else: | |
if videos.get('@viewCount') and int(videos['@viewCount']) >= 1: | |
watched_videos.append(videos) | |
# Delete watched videos and send notification | |
if watched_videos: | |
watched_titles = [] | |
# Delete watched videos | |
for video in watched_videos: | |
url = PLEX_URL + video['@key'] | |
title = f"{video['@grandparentTitle']} - {video['@title']}" | |
watched_titles.append(title) | |
requests.delete(url, headers={"X-Plex-Token": PLEX_TOKEN}) | |
# Write to log file | |
with open(LOG_FILE, 'a') as log_file: | |
log_file.write(f"{timestamp} - {len(watched_videos)} watched episodes were removed:\n") | |
log_file.write('\n'.join(watched_titles)) | |
log_file.write('\n') | |
# Send notification | |
notification = { | |
'value1': f"{len(watched_videos)} watched episodes were removed!\n" + '\n'.join(watched_titles) | |
} | |
requests.post(IFTTT_WEBHOOK, json=notification) | |
with open(LOG_FILE, 'a') as log_file: | |
log_file.write(f'{timestamp} - Notification sent!\n') | |
else: | |
with open(LOG_FILE, 'a') as log_file: | |
log_file.write(f'{timestamp} - No videos to delete!\n') | |
if __name__ == '__main__': | |
cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment