Added a script to set the addedAt datetime to the file creation date

This commit is contained in:
Junior 2026-06-14 16:06:24 +00:00
parent 3a615c7eb6
commit a788f77f96

58
fixadded.py Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/python
import os
from datetime import datetime
from pathlib import Path
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
from plexapi.media import Media
from pprint import pprint
import sys
import time
try:
import apiconfig
except:
print("Error!")
print(" \"apiconfig.py\" seems to be missing!")
print(" Have you copied the default config to \"apiconfig.py\" and made appropriate edits?")
print()
sys.exit()
fixing = False
if len(sys.argv) > 1:
if sys.argv[1] == "--fix":
fixing = True
plex = PlexServer(apiconfig.url, apiconfig.apikey)
wrong_count = 0
print("Looking for videos that have added dates not matching ctime...")
videos = plex.library.section(apiconfig.libraryname).all()
nowstamp = time.time()
cutoff = 86400 * 10
for video in videos:
file = None
for part in video.iterParts():
if file == None: file = part.file
if not os.path.isfile(file):
print(f"*** Missing: {video.title} ({file})")
continue
path = Path(file)
stat = path.stat()
filestamp = stat.st_mtime
#if (nowstamp - addedstamp) < cutoff:
# print(f"{video.title}: {addedstamp}")
filedate = datetime.fromtimestamp(int(filestamp))
addeddate = video.addedAt
if filedate.date() != addeddate.date():
wrong_count += 1
print(f"{video.title}: ctime={filedate.date()} added={addeddate.date()}")
if fixing:
video.editAddedAt(filedate, locked=True)
print(f"Total Files With Wrong Added: {wrong_count}")
if not fixing and (wrong_count > 0):
print("Fix these files by using the '--fix' parameter")
print()