Skip to content

Instantly share code, notes, and snippets.

@Grygon
Created April 5, 2023 19:11
Show Gist options
  • Save Grygon/54ef90e8a2a1297416f1741c5c3c6965 to your computer and use it in GitHub Desktop.
Save Grygon/54ef90e8a2a1297416f1741c5c3c6965 to your computer and use it in GitHub Desktop.
ThePosterDB Checker to keep Plex-Meta-Manager Files up to date
import requests
from bs4 import BeautifulSoup
import yaml
import os
def find_url_poster(data):
base = []
if isinstance(data, dict):
for key, value in data.items():
if key == 'url_poster':
base = [value]
else:
base = base + find_url_poster(value)
elif isinstance(data, list):
for item in data:
base = base + find_url_poster(item)
return base
class Checker(object):
directory = ""
check_type = "Movie"
def __init__(self, directory):
if('movie' in directory):
self.check_type = "Movie"
elif('show' in directory):
self.check_type = "Show"
self.directory = directory
def get_posterdb_count(self, url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
movie_elements = soup.select(f'.container > .d-flex.flex-wrap [data-poster-type={self.check_type}]')
return len(movie_elements)
def get_local_count(self, file):
with open(file, 'r') as f:
data = yaml.safe_load(f)
count = len(find_url_poster(data.get('metadata',{})))
return count
def get_posterdb_url(self, file):
with open(file, 'r') as f:
lines = f.readlines()
for line in lines:
if line.startswith(('#', ' #')):
comment = line[1:].strip() # remove the '#' character and any leading/trailing whitespace
break
url = comment.split(" - ")[-1]
return url
def compare_directory(self):
files = os.listdir(self.directory)
for file in files:
try:
self.compare_file(file)
except:
print("Error reading " + file)
def compare_file(self, file):
full_path = self.directory + "\\" + file
local = self.get_local_count(full_path)
remote = self.get_posterdb_count(self.get_posterdb_url(full_path))
if (local != remote):
print("File: " + file)
print("Local: " + str(local))
print("Remote: " + str(remote))
# Add your directory below
checker = Checker("INSERT PATH TO DIRECTORY HERE")
checker.compare_directory()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment