45 Commits

Author SHA1 Message Date
Luigi311
9ffbc49ad3 Merge pull request #30 from luigi311/dev
Add ssl_bypass to skip hostname validation.
2022-11-21 17:39:00 -07:00
Luigi311
644dc8e3af Merge pull request #29 from lgtm-migrator/codeql
Add CodeQL workflow for GitHub code scanning
2022-11-21 17:38:45 -07:00
Luigi311
47bc4e94dc Fix dockerfile 2022-11-21 17:31:47 -07:00
LGTM Migrator
f17d39fe17 Add CodeQL workflow for GitHub code scanning 2022-11-10 14:41:07 +00:00
Luigi311
966dcacf8d Add ssl_bypass to skip hostname validation. 2022-09-25 14:16:01 -06:00
Luigi311
9afc00443c Merge pull request #27 from luigi311/dev
Cleanup issues
2022-08-18 00:46:00 -06:00
Luigi311
3ec177ea64 rename test_main 2022-08-18 00:17:32 -06:00
Luigi311
b360c9fd0b Remove unnecessary deepcopy 2022-08-18 00:15:42 -06:00
Luigi311
1ed791b1ed Fix jellyfin 2022-08-17 23:49:05 -06:00
Luigi311
f19b1a3063 Cleanup length and functions instead of methods 2022-08-17 23:34:45 -06:00
Luigi311
190a72bd3c Cleanup 2022-08-17 22:53:27 -06:00
Luigi311
c848106ce7 Black cleanup 2022-08-17 22:31:23 -06:00
Luigi311
dd319271bd Cleanup 2022-08-17 22:09:11 -06:00
Luigi311
16879cc728 Merge pull request #26 from luigi311/dev
Use async for jellyfin
2022-08-17 21:49:34 -06:00
Luigi311
942ec3533f Cleanup log file on runs 2022-08-17 21:43:51 -06:00
Luigi311
9f6edfc91a Merge branch 'main' into dev 2022-08-17 21:40:25 -06:00
Luigi311
827ace2e97 cleanup 2022-08-17 21:20:28 -06:00
Luigi311
f6b57a1b4d Update README.md 2022-07-10 01:38:42 -06:00
Luigi311
88a7526721 Use async for jellyfin (#23)
* Use async

* Massive jellyfin watched speedup

Co-authored-by: Luigi311 <luigi311.lg@gmail.com>
2022-07-10 01:30:12 -06:00
luigi311
1efb4d8543 Fix debug 2022-07-06 17:22:35 -06:00
Luigi311
7571e9a343 Merge pull request #22 from luigi311/dev
Fix errors on certain edge cases
2022-07-05 21:23:14 -06:00
Luigi311
7640e9ee03 fix typo 2022-07-05 19:26:58 -06:00
Luigi311
50ed3d6400 Fix user_name in plex 2022-07-05 19:26:22 -06:00
Luigi311
c9a373851f Remove indexnumber from logging 2022-07-05 19:16:25 -06:00
Luigi311
a3f3db8f4e Use generate_library_guids_dict instead of library type 2022-07-05 18:09:08 -06:00
Luigi311
de619de923 Add more logging, fix username in jellyfin mark. 2022-07-05 16:35:22 -06:00
Luigi311
852d8dc3c3 Merge pull request #18 from luigi311/dev
Dev
2022-06-21 02:53:32 -06:00
Luigi311
c104973f95 Add location based matching 2022-06-20 21:12:02 -06:00
Luigi311
8b7fc5e323 Merge pull request #17 from luigi311/pytest
Add Pytest
2022-06-20 16:10:29 -06:00
Luigi311
afb71d8e00 Handle locations in generate_library_guids_dict 2022-06-20 16:07:52 -06:00
Luigi311
34d97f8dde Add pytest action 2022-06-20 16:05:05 -06:00
Luigi311
2ad6b3afdf Add pytest 2022-06-20 15:48:07 -06:00
Luigi311
7cd492dc98 Remove worker=1 2022-06-19 03:03:17 -06:00
Luigi311
74b5ea7b5e Fix username differences in watch list. Add python version check. More error handling. 2022-06-19 02:56:50 -06:00
Luigi311
21fe4875eb Add ENVs to dockerfile 2022-06-15 13:38:28 -06:00
Luigi311
aeb86f6b85 Fix user when using plex login. Fix sleep duration 2022-06-15 13:21:03 -06:00
Luigi311
70ef31ff47 Fix threading 2022-06-15 12:51:09 -06:00
Luigi311
0584a85f90 Add parallel threading 2022-06-14 22:36:44 -06:00
Luigi311
beb4e667ae Cleanup 2022-06-13 22:43:56 -06:00
Luigi311
7695994ec2 Support x many servers of any combination 2022-06-13 22:30:41 -06:00
Luigi311
04a8da6478 Merge pull request #14 from luigi311/dev
Improve matching via IDs
2022-06-13 18:43:46 -06:00
Luigi311
7ef2986bde Remove unused variable 2022-06-13 18:41:43 -06:00
Luigi311
c18f0a2582 Add debug_level option 2022-06-13 18:16:55 -06:00
Luigi311
4657097f6d Cleanup 2022-06-13 16:46:29 -06:00
Luigi311
ca84bbb19d Improve show/movie matching by using IDs only 2022-06-13 14:49:37 -06:00
17 changed files with 3439 additions and 1807 deletions

View File

@@ -1,38 +1,43 @@
## Do not mark any shows/movies as played and instead just output to log if they would of been marked. ## Do not mark any shows/movies as played and instead just output to log if they would of been marked.
DRYRUN = "True" DRYRUN = "True"
## Additional logging information ## Additional logging information
DEBUG = "True" DEBUG = "True"
## How often to run the script in seconds ## Debugging level, "info" is default, "debug" is more verbose
SLEEP_DURATION = "3600" DEBUG_LEVEL = "info"
## Log file where all output will be written to ## How often to run the script in seconds
LOGFILE = "log.log" SLEEP_DURATION = "3600"
## Map usernames between plex and jellyfin in the event that they are different, order does not matter ## Log file where all output will be written to
#USER_MAPPING = { "testuser2": "testuser3" } LOGFILE = "log.log"
## Map libraries between plex and jellyfin in the even that they are different, order does not matter ## Map usernames between plex and jellyfin in the event that they are different, order does not matter
#LIBRARY_MAPPING = { "Shows": "TV Shows" } #USER_MAPPING = { "testuser2": "testuser3" }
## Map libraries between plex and jellyfin in the even that they are different, order does not matter
#LIBRARY_MAPPING = { "Shows": "TV Shows" }
## Recommended to use token as it is faster to connect as it is direct to the server instead of going through the plex servers
## URL of the plex server, use hostname or IP address if the hostname is not resolving correctly
PLEX_BASEURL = "http://localhost:32400" ## Recommended to use token as it is faster to connect as it is direct to the server instead of going through the plex servers
## Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/ ## URL of the plex server, use hostname or IP address if the hostname is not resolving correctly
PLEX_TOKEN = "SuperSecretToken" ## Comma seperated list for multiple servers
## If not using plex token then use username and password of the server admin along with the servername PLEX_BASEURL = "http://localhost:32400"
#PLEX_USERNAME = "" ## Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
#PLEX_PASSWORD = "" PLEX_TOKEN = "SuperSecretToken"
#PLEX_SERVERNAME = "Plex Server" ## If not using plex token then use username and password of the server admin along with the servername
#PLEX_USERNAME = ""
#PLEX_PASSWORD = ""
## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly #PLEX_SERVERNAME = "Plex Server"
JELLYFIN_BASEURL = "http://localhost:8096" ## Skip hostname validation for ssl certificates.
## Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key SSL_BYPASS = "False"
JELLYFIN_TOKEN = "SuperSecretToken"
## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly
## Comma seperated list for multiple servers
## Blacklisting/Whitelisting libraries, library types such as Movies/TV Shows, and users. Mappings apply so if the mapping for the user or library exist then both will be excluded. JELLYFIN_BASEURL = "http://localhost:8096"
#BLACKLIST_LIBRARY = "" ## Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key
#WHITELIST_LIBRARY = "" JELLYFIN_TOKEN = "SuperSecretToken"
#BLACKLIST_LIBRARY_TYPE = ""
#WHITELIST_LIBRARY_TYPE = ""
#BLACKLIST_USERS = "" ## Blacklisting/Whitelisting libraries, library types such as Movies/TV Shows, and users. Mappings apply so if the mapping for the user or library exist then both will be excluded.
WHITELIST_USERS = "testuser1,testuser2" #BLACKLIST_LIBRARY = ""
#WHITELIST_LIBRARY = ""
#BLACKLIST_LIBRARY_TYPE = ""
#WHITELIST_LIBRARY_TYPE = ""
#BLACKLIST_USERS = ""
WHITELIST_USERS = "testuser1,testuser2"

View File

@@ -1,74 +1,86 @@
name: CI name: CI
on: on:
push: push:
paths-ignore: paths-ignore:
- .gitignore - .gitignore
- "*.md" - "*.md"
pull_request: pull_request:
paths-ignore: paths-ignore:
- .gitignore - .gitignore
- "*.md" - "*.md"
jobs: jobs:
docker: pytest:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - uses: actions/checkout@v2
uses: actions/checkout@v2
- name: "Install dependencies"
- name: Docker meta run: pip install -r requirements.txt && pip install -r test/requirements.txt
id: docker_meta
env: - name: "Run tests"
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} run: pytest -vvv
if: "${{ env.DOCKER_USERNAME != '' }}"
uses: docker/metadata-action@v4 docker:
with: runs-on: ubuntu-latest
images: ${{ secrets.DOCKER_USERNAME }}/jellyplex-watched # list of Docker images to use as base name for tags needs: pytest
tags: | steps:
type=raw,value=latest,enable={{is_default_branch}} - name: Checkout
type=ref,event=branch uses: actions/checkout@v2
type=ref,event=pr
type=semver,pattern={{version}} - name: Docker meta
type=semver,pattern={{major}}.{{minor}} id: docker_meta
type=sha env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
- name: Set up QEMU if: "${{ env.DOCKER_USERNAME != '' }}"
uses: docker/setup-qemu-action@v1 uses: docker/metadata-action@v4
with:
- name: Set up Docker Buildx images: ${{ secrets.DOCKER_USERNAME }}/jellyplex-watched # list of Docker images to use as base name for tags
uses: docker/setup-buildx-action@v1 tags: |
type=raw,value=latest,enable={{is_default_branch}}
- name: Login to DockerHub type=ref,event=branch
if: "${{ steps.docker_meta.outcome == 'success' }}" type=ref,event=pr
uses: docker/login-action@v1 type=semver,pattern={{version}}
with: type=semver,pattern={{major}}.{{minor}}
username: ${{ secrets.DOCKER_USERNAME }} type=sha
password: ${{ secrets.DOCKER_TOKEN }}
- name: Set up QEMU
- name: Build uses: docker/setup-qemu-action@v1
id: build
if: "${{ steps.docker_meta.outcome == 'skipped' }}" - name: Set up Docker Buildx
uses: docker/build-push-action@v2 uses: docker/setup-buildx-action@v1
with:
context: . - name: Login to DockerHub
file: ./Dockerfile if: "${{ steps.docker_meta.outcome == 'success' }}"
platforms: linux/amd64,linux/arm64 uses: docker/login-action@v1
push: false with:
tags: jellyplex-watched:action username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build Push
id: build_push - name: Build
if: "${{ steps.docker_meta.outcome == 'success' }}" id: build
uses: docker/build-push-action@v2 if: "${{ steps.docker_meta.outcome == 'skipped' }}"
with: uses: docker/build-push-action@v2
context: . with:
file: ./Dockerfile context: .
platforms: linux/amd64,linux/arm64 file: ./Dockerfile
push: true platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_meta.outputs.tags }} push: false
labels: ${{ steps.docker_meta.outputs.labels }} tags: jellyplex-watched:action
# Echo digest so users can validate their image - name: Build Push
- name: Image digest id: build_push
if: "${{ steps.docker_meta.outcome == 'success' }}" if: "${{ steps.docker_meta.outcome == 'success' }}"
run: echo "${{ steps.build_push.outputs.digest }}" uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
# Echo digest so users can validate their image
- name: Image digest
if: "${{ steps.docker_meta.outcome == 'success' }}"
run: echo "${{ steps.build_push.outputs.digest }}"

41
.github/workflows/codeql.yml vendored Normal file
View File

@@ -0,0 +1,41 @@
name: "CodeQL"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: "23 20 * * 6"
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ python ]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: +security-and-quality
- name: Autobuild
uses: github/codeql-action/autobuild@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"

263
.gitignore vendored
View File

@@ -1,131 +1,132 @@
.env .env
*.prof
# Byte-compiled / optimized / DLL files
__pycache__/ # Byte-compiled / optimized / DLL files
*.py[cod] __pycache__/
*$py.class *.py[cod]
*$py.class
# C extensions
*.so # C extensions
*.so
# Distribution / packaging
.Python # Distribution / packaging
build/ .Python
develop-eggs/ build/
dist/ develop-eggs/
downloads/ dist/
eggs/ downloads/
.eggs/ eggs/
lib/ .eggs/
lib64/ lib/
parts/ lib64/
sdist/ parts/
var/ sdist/
wheels/ var/
pip-wheel-metadata/ wheels/
share/python-wheels/ pip-wheel-metadata/
*.egg-info/ share/python-wheels/
.installed.cfg *.egg-info/
*.egg .installed.cfg
MANIFEST *.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template # PyInstaller
# before PyInstaller builds the exe, so as to inject date/other infos into it. # Usually these files are written by a python script from a template
*.manifest # before PyInstaller builds the exe, so as to inject date/other infos into it.
*.spec *.manifest
*.spec
# Installer logs
pip-log.txt # Installer logs
pip-delete-this-directory.txt pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/ # Unit test / coverage reports
.tox/ htmlcov/
.nox/ .tox/
.coverage .nox/
.coverage.* .coverage
.cache .coverage.*
nosetests.xml .cache
coverage.xml nosetests.xml
*.cover coverage.xml
*.py,cover *.cover
.hypothesis/ *.py,cover
.pytest_cache/ .hypothesis/
.pytest_cache/
# Translations
*.mo # Translations
*.pot *.mo
*.pot
# Django stuff:
*.log # Django stuff:
local_settings.py *.log
db.sqlite3 local_settings.py
db.sqlite3-journal db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/ # Flask stuff:
.webassets-cache instance/
.webassets-cache
# Scrapy stuff:
.scrapy # Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/ # Sphinx documentation
docs/_build/
# PyBuilder
target/ # PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints # Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/ # IPython
ipython_config.py profile_default/
ipython_config.py
# pyenv
.python-version # pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # pipenv
# However, in case of collaboration, if having platform-specific dependencies or dependencies # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# having no cross-platform support, pipenv may install dependencies that don't work, or not # However, in case of collaboration, if having platform-specific dependencies or dependencies
# install all needed dependencies. # having no cross-platform support, pipenv may install dependencies that don't work, or not
#Pipfile.lock # install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule # Celery stuff
celerybeat.pid celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py # SageMath parsed files
*.sage.py
# Environments
.env # Environments
.venv .env
env/ .venv
venv/ env/
ENV/ venv/
env.bak/ ENV/
venv.bak/ env.bak/
venv.bak/
# Spyder project settings
.spyderproject # Spyder project settings
.spyproject .spyderproject
.spyproject
# Rope project settings
.ropeproject # Rope project settings
.ropeproject
# mkdocs documentation
/site # mkdocs documentation
/site
# mypy
.mypy_cache/ # mypy
.dmypy.json .mypy_cache/
dmypy.json .dmypy.json
dmypy.json
# Pyre type checker
.pyre/ # Pyre type checker
.pyre/

16
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Main",
"type": "python",
"request": "launch",
"program": "main.py",
"console": "integratedTerminal",
"justMyCode": true
}
]
}

View File

@@ -1,10 +1,41 @@
FROM python:3-slim FROM python:3-slim
WORKDIR /app ENV DRYRUN 'True'
ENV DEBUG 'True'
COPY ./requirements.txt ./ ENV DEBUG_LEVEL 'INFO'
RUN pip install --no-cache-dir -r requirements.txt ENV SLEEP_DURATION '3600'
ENV LOGFILE 'log.log'
COPY . .
ENV USER_MAPPING '{ "User Test": "User Test2" }'
CMD ["python", "-u", "main.py"] ENV LIBRARY_MAPPING '{ "Shows Test": "TV Shows Test" }'
ENV PLEX_BASEURL 'http://localhost:32400'
ENV PLEX_TOKEN ''
ENV PLEX_USERNAME ''
ENV PLEX_PASSWORD ''
ENV PLEX_SERVERNAME ''
ENV JELLYFIN_BASEURL 'http://localhost:8096'
ENV JELLYFIN_TOKEN ''
ENV BLACKLIST_LIBRARY ''
ENV WHITELIST_LIBRARY ''
ENV BLACKLIST_LIBRARY_TYPE ''
ENV WHITELIST_LIBRARY_TYPE ''
ENV BLACKLIST_USERS ''
ENV WHITELIST_USERS ''
WORKDIR /app
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "-u", "main.py"]

1348
LICENSE

File diff suppressed because it is too large Load Diff

142
README.md
View File

@@ -1,69 +1,73 @@
# JellyPlex-Watched # JellyPlex-Watched
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/26b47c5db63942f28f02f207f692dc85)](https://www.codacy.com/gh/luigi311/JellyPlex-Watched/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=luigi311/JellyPlex-Watched&amp;utm_campaign=Badge_Grade) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/26b47c5db63942f28f02f207f692dc85)](https://www.codacy.com/gh/luigi311/JellyPlex-Watched/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=luigi311/JellyPlex-Watched&amp;utm_campaign=Badge_Grade)
Sync watched between jellyfin and plex Sync watched between jellyfin and plex
## Description ## Description
Keep in sync all your users watched history between jellyfin and plex locally. This uses the imdb ids and any other matching id to find the correct episode/movie between the two. This is not perfect but it works for most cases. Keep in sync all your users watched history between jellyfin and plex servers locally. This uses the imdb ids and any other matching id to find the correct episode/movie between the two. This is not perfect but it works for most cases. You can use this for as many servers as you want by enterying multiple options in the .env plex/jellyfin section seperated by commas.
## Installation ## Configuration
### Baremeta
- Setup virtualenv of your choice ## Installation
- Install dependencies ### Baremetal
```bash - Setup virtualenv of your choice
pip install -r requirements.txt
``` - Install dependencies
- Create a .env file similar to .env.sample, uncomment whitelist and blacklist if needed, fill in baseurls and tokens ```bash
pip install -r requirements.txt
- Run ```
```bash - Create a .env file similar to .env.sample, uncomment whitelist and blacklist if needed, fill in baseurls and tokens
python main.py
``` - Run
### Docker ```bash
python main.py
- Build docker image ```
```bash ### Docker
docker build -t jellyplex-watched .
``` - Build docker image
- or use pre-built image ```bash
docker build -t jellyplex-watched .
```bash ```
docker pull luigi311/jellyplex-watched:latest
``` - or use pre-built image
#### With variables ```bash
docker pull luigi311/jellyplex-watched:latest
- Run ```
```bash #### With variables
docker run --rm -it -e PLEX_TOKEN='SuperSecretToken' luigi311/jellyplex-watched:latest
``` - Run
#### With .env ```bash
docker run --rm -it -e PLEX_TOKEN='SuperSecretToken' luigi311/jellyplex-watched:latest
- Create a .env file similar to .env.sample and set the MNEMONIC variable to your seed phrase ```
- Run #### With .env
```bash - Create a .env file similar to .env.sample and set the variables to match your setup
docker run --rm -it -v "$(pwd)/.env:/app/.env" luigi311/jellyplex-watched:latest
``` - Run
## Contributing ```bash
docker run --rm -it -v "$(pwd)/.env:/app/.env" luigi311/jellyplex-watched:latest
I am open to recieving pull requests. If you are submitting a pull request, please make sure run it locally for a day or two to make sure it is working as expected and stable. Make all pull requests against the dev branch and nothing will be merged into the main without going through the lower branches. ```
## License ## Contributing
This is currently under the GNU General Public License v3.0. I am open to recieving pull requests. If you are submitting a pull request, please make sure run it locally for a day or two to make sure it is working as expected and stable. Make all pull requests against the dev branch and nothing will be merged into the main without going through the lower branches.
## License
This is currently under the GNU General Public License v3.0.

305
main.py
View File

@@ -1,294 +1,11 @@
import copy, os, traceback, json import sys
from dotenv import load_dotenv
from time import sleep if __name__ == "__main__":
# Check python version 3.6 or higher
from src.functions import logger, str_to_bool, search_mapping if not (3, 6) <= tuple(map(int, sys.version_info[:2])):
from src.plex import Plex print("This script requires Python 3.6 or higher")
from src.jellyfin import Jellyfin sys.exit(1)
load_dotenv(override=True) from src.main import main
def cleanup_watched(watched_list_1, watched_list_2, user_mapping=None, library_mapping=None): main()
modified_watched_list_1 = copy.deepcopy(watched_list_1)
# remove entries from plex_watched that are in jellyfin_watched
for user_1 in watched_list_1:
user_other = None
if user_mapping:
user_other = search_mapping(user_mapping, user_1)
if user_1 in modified_watched_list_1:
if user_1 in watched_list_2:
user_2 = user_1
elif user_other in watched_list_2:
user_2 = user_other
else:
logger(f"User {user_1} and {user_other} not found in watched list 2", 1)
continue
for library_1 in watched_list_1[user_1]:
library_other = None
if library_mapping:
library_other = search_mapping(library_mapping, library_1)
if library_1 in modified_watched_list_1[user_1]:
if library_1 in watched_list_2[user_2]:
library_2 = library_1
elif library_other in watched_list_2[user_2]:
library_2 = library_other
else:
logger(f"User {library_1} and {library_other} not found in watched list 2", 1)
continue
for item in watched_list_1[user_1][library_1]:
if item in modified_watched_list_1[user_1][library_1]:
# Movies
if isinstance(watched_list_1[user_1][library_1], list):
for watch_list_1_key, watch_list_1_value in item.items():
for watch_list_2_item in watched_list_2[user_2][library_2]:
for watch_list_2_item_key, watch_list_2_item_value in watch_list_2_item.items():
if watch_list_1_key == watch_list_2_item_key and watch_list_1_value == watch_list_2_item_value:
if item in modified_watched_list_1[user_1][library_1]:
modified_watched_list_1[user_1][library_1].remove(item)
# TV Shows
elif isinstance(watched_list_1[user_1][library_1], dict):
if item in watched_list_2[user_2][library_2]:
for season in watched_list_1[user_1][library_1][item]:
if season in watched_list_2[user_2][library_2][item]:
for episode in watched_list_1[user_1][library_1][item][season]:
for watch_list_1_episode_key, watch_list_1_episode_value in episode.items():
for watch_list_2_episode in watched_list_2[user_2][library_2][item][season]:
for watch_list_2_episode_key, watch_list_2_episode_value in watch_list_2_episode.items():
if watch_list_1_episode_key == watch_list_2_episode_key and watch_list_1_episode_value == watch_list_2_episode_value:
if episode in modified_watched_list_1[user_1][library_1][item][season]:
modified_watched_list_1[user_1][library_1][item][season].remove(episode)
# If season is empty, remove season
if len(modified_watched_list_1[user_1][library_1][item][season]) == 0:
if season in modified_watched_list_1[user_1][library_1][item]:
del modified_watched_list_1[user_1][library_1][item][season]
# If the show is empty, remove the show
if len(modified_watched_list_1[user_1][library_1][item]) == 0:
if item in modified_watched_list_1[user_1][library_1]:
del modified_watched_list_1[user_1][library_1][item]
# If library is empty then remove it
if len(modified_watched_list_1[user_1][library_1]) == 0:
if library_1 in modified_watched_list_1[user_1]:
del modified_watched_list_1[user_1][library_1]
# If user is empty delete user
if len(modified_watched_list_1[user_1]) == 0:
del modified_watched_list_1[user_1]
return modified_watched_list_1
def setup_black_white_lists(library_mapping=None):
blacklist_library = os.getenv("BLACKLIST_LIBRARY")
if blacklist_library:
if len(blacklist_library) > 0:
blacklist_library = blacklist_library.split(",")
blacklist_library = [x.strip() for x in blacklist_library]
if library_mapping:
temp_library = []
for library in blacklist_library:
library_other = search_mapping(library_mapping, library)
if library_other:
temp_library.append(library_other)
blacklist_library = blacklist_library + temp_library
else:
blacklist_library = []
logger(f"Blacklist Library: {blacklist_library}", 1)
whitelist_library = os.getenv("WHITELIST_LIBRARY")
if whitelist_library:
if len(whitelist_library) > 0:
whitelist_library = whitelist_library.split(",")
whitelist_library = [x.strip() for x in whitelist_library]
if library_mapping:
temp_library = []
for library in whitelist_library:
library_other = search_mapping(library_mapping, library)
if library_other:
temp_library.append(library_other)
whitelist_library = whitelist_library + temp_library
else:
whitelist_library = []
logger(f"Whitelist Library: {whitelist_library}", 1)
blacklist_library_type = os.getenv("BLACKLIST_LIBRARY_TYPE")
if blacklist_library_type:
if len(blacklist_library_type) > 0:
blacklist_library_type = blacklist_library_type.split(",")
blacklist_library_type = [x.lower().strip() for x in blacklist_library_type]
else:
blacklist_library_type = []
logger(f"Blacklist Library Type: {blacklist_library_type}", 1)
whitelist_library_type = os.getenv("WHITELIST_LIBRARY_TYPE")
if whitelist_library_type:
if len(whitelist_library_type) > 0:
whitelist_library_type = whitelist_library_type.split(",")
whitelist_library_type = [x.lower().strip() for x in whitelist_library_type]
else:
whitelist_library_type = []
logger(f"Whitelist Library Type: {whitelist_library_type}", 1)
blacklist_users = os.getenv("BLACKLIST_USERS")
if blacklist_users:
if len(blacklist_users) > 0:
blacklist_users = blacklist_users.split(",")
blacklist_users = [x.lower().strip() for x in blacklist_users]
else:
blacklist_users = []
logger(f"Blacklist Users: {blacklist_users}", 1)
whitelist_users = os.getenv("WHITELIST_USERS")
if whitelist_users:
if len(whitelist_users) > 0:
whitelist_users = whitelist_users.split(",")
whitelist_users = [x.lower().strip() for x in whitelist_users]
else:
whitelist_users = []
else:
whitelist_users = []
logger(f"Whitelist Users: {whitelist_users}", 1)
return blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users
def setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping=None):
# generate list of users from plex.users
plex_users = [ x.title.lower() for x in plex.users ]
jellyfin_users = [ key.lower() for key in jellyfin.users.keys() ]
# combined list of overlapping users from plex and jellyfin
users = {}
for plex_user in plex_users:
if user_mapping:
jellyfin_plex_mapped_user = search_mapping(user_mapping, plex_user)
if jellyfin_plex_mapped_user:
users[plex_user] = jellyfin_plex_mapped_user
continue
if plex_user in jellyfin_users:
users[plex_user] = plex_user
for jellyfin_user in jellyfin_users:
if user_mapping:
plex_jellyfin_mapped_user = search_mapping(user_mapping, jellyfin_user)
if plex_jellyfin_mapped_user:
users[plex_jellyfin_mapped_user] = jellyfin_user
continue
if jellyfin_user in plex_users:
users[jellyfin_user] = jellyfin_user
logger(f"User list that exist on both servers {users}", 1)
users_filtered = {}
for user in users:
# whitelist_user is not empty and user lowercase is not in whitelist lowercase
if len(whitelist_users) > 0:
if user not in whitelist_users and users[user] not in whitelist_users:
logger(f"{user} or {users[user]} is not in whitelist", 1)
continue
if user not in blacklist_users and users[user] not in blacklist_users:
users_filtered[user] = users[user]
logger(f"Filtered user list {users_filtered}", 1)
plex_users = []
for plex_user in plex.users:
if plex_user.title.lower() in users_filtered.keys() or plex_user.title.lower() in users_filtered.values():
plex_users.append(plex_user)
jellyfin_users = {}
for jellyfin_user, jellyfin_id in jellyfin.users.items():
if jellyfin_user.lower() in users_filtered.keys() or jellyfin_user.lower() in users_filtered.values():
jellyfin_users[jellyfin_user] = jellyfin_id
if len(plex_users) == 0:
raise Exception(f"No plex users found, users found {users} filtered users {users_filtered}")
if len(jellyfin_users) == 0:
raise Exception(f"No jellyfin users found, users found {users} filtered users {users_filtered}")
logger(f"plex_users: {plex_users}", 1)
logger(f"jellyfin_users: {jellyfin_users}", 1)
return plex_users, jellyfin_users
def main():
logfile = os.getenv("LOGFILE","log.log")
# Delete logfile if it exists
if os.path.exists(logfile):
os.remove(logfile)
dryrun = str_to_bool(os.getenv("DRYRUN", "False"))
logger(f"Dryrun: {dryrun}", 1)
user_mapping = os.getenv("USER_MAPPING")
if user_mapping:
user_mapping = json.loads(user_mapping.lower())
logger(f"User Mapping: {user_mapping}", 1)
library_mapping = os.getenv("LIBRARY_MAPPING")
if library_mapping:
library_mapping = json.loads(library_mapping)
logger(f"Library Mapping: {library_mapping}", 1)
plex = Plex()
jellyfin = Jellyfin()
# Create (black/white)lists
blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users = setup_black_white_lists(library_mapping)
# Create users list
plex_users, jellyfin_users = setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping)
plex_watched = plex.get_plex_watched(plex_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping)
jellyfin_watched = jellyfin.get_jellyfin_watched(jellyfin_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping)
# clone watched so it isnt modified in the cleanup function so all duplicates are actually removed
plex_watched_filtered = copy.deepcopy(plex_watched)
jellyfin_watched_filtered = copy.deepcopy(jellyfin_watched)
plex_watched = cleanup_watched(plex_watched_filtered, jellyfin_watched_filtered, user_mapping, library_mapping)
logger(f"plex_watched that needs to be synced to jellyfin:\n{plex_watched}", 1)
jellyfin_watched = cleanup_watched(jellyfin_watched_filtered, plex_watched_filtered, user_mapping, library_mapping)
logger(f"jellyfin_watched that needs to be synced to plex:\n{jellyfin_watched}", 1)
# Update watched status
plex.update_watched(jellyfin_watched, user_mapping, library_mapping, dryrun)
jellyfin.update_watched(plex_watched, user_mapping, library_mapping, dryrun)
if __name__ == "__main__":
sleep_timer = float(os.getenv("SLEEP_TIMER", "3600"))
while(True):
try:
main()
logger(f"Looping in {sleep_timer}")
except Exception as error:
if isinstance(error, list):
for message in error:
logger(message, log_type=2)
else:
logger(error, log_type=2)
logger(traceback.format_exc(), 2)
logger(f"Retrying in {sleep_timer}", log_type=0)
except KeyboardInterrupt:
logger("Exiting", log_type=0)
os._exit(0)
sleep(sleep_timer)

View File

@@ -1,3 +1,4 @@
plexapi plexapi
requests requests
python-dotenv python-dotenv
aiohttp

View File

@@ -1,75 +1,189 @@
import os import os
from dotenv import load_dotenv from concurrent.futures import ThreadPoolExecutor
load_dotenv(override=True) from dotenv import load_dotenv
logfile = os.getenv("LOGFILE","log.log") load_dotenv(override=True)
def logger(message, log_type=0): logfile = os.getenv("LOGFILE", "log.log")
debug = str_to_bool(os.getenv("DEBUG", "True"))
output = str(message) def logger(message: str, log_type=0):
if log_type == 0: debug = str_to_bool(os.getenv("DEBUG", "True"))
pass debug_level = os.getenv("DEBUG_LEVEL", "info").lower()
elif log_type == 1 and debug:
output = f"[INFO]: {output}" output = str(message)
elif log_type == 2: if log_type == 0:
output = f"[ERROR]: {output}" pass
else: elif log_type == 1 and (debug and debug_level == "info"):
output = None output = f"[INFO]: {output}"
elif log_type == 2:
if output is not None: output = f"[ERROR]: {output}"
print(output) elif log_type == 3 and (debug and debug_level == "debug"):
file = open(logfile, "a", encoding="utf-8") output = f"[DEBUG]: {output}"
file.write(output + "\n") elif log_type == 4:
output = f"[WARNING]: {output}"
# Reimplementation of distutils.util.strtobool due to it being deprecated else:
# Source: https://github.com/PostHog/posthog/blob/01e184c29d2c10c43166f1d40a334abbc3f99d8a/posthog/utils.py#L668 output = None
def str_to_bool(value: any) -> bool:
if not value: if output is not None:
return False print(output)
return str(value).lower() in ("y", "yes", "t", "true", "on", "1") file = open(logfile, "a", encoding="utf-8")
file.write(output + "\n")
# Get mapped value
def search_mapping(dictionary: dict, key_value: str):
if key_value in dictionary.keys(): # Reimplementation of distutils.util.strtobool due to it being deprecated
return dictionary[key_value] # Source: https://github.com/PostHog/posthog/blob/01e184c29d2c10c43166f1d40a334abbc3f99d8a/posthog/utils.py#L668
elif key_value.lower() in dictionary.keys(): def str_to_bool(value: any) -> bool:
return dictionary[key_value] if not value:
elif key_value in dictionary.values(): return False
return list(dictionary.keys())[list(dictionary.values()).index(key_value)] return str(value).lower() in ("y", "yes", "t", "true", "on", "1")
elif key_value.lower() in dictionary.values():
return list(dictionary.keys())[list(dictionary.values()).index(key_value)]
else: # Get mapped value
return None def search_mapping(dictionary: dict, key_value: str):
if key_value in dictionary.keys():
return dictionary[key_value]
def check_skip_logic(library_title, library_type, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping): elif key_value.lower() in dictionary.keys():
skip_reason = None return dictionary[key_value.lower()]
elif key_value in dictionary.values():
if library_type.lower() in blacklist_library_type: return list(dictionary.keys())[list(dictionary.values()).index(key_value)]
skip_reason = "is blacklist_library_type" elif key_value.lower() in dictionary.values():
return list(dictionary.keys())[
if library_title.lower() in [x.lower() for x in blacklist_library]: list(dictionary.values()).index(key_value.lower())
skip_reason = "is blacklist_library" ]
else:
library_other = None return None
if library_mapping:
library_other = search_mapping(library_mapping, library_title)
if library_other: def check_skip_logic(
if library_other.lower() in [x.lower() for x in blacklist_library]: library_title,
skip_reason = "is blacklist_library" library_type,
blacklist_library,
if len(whitelist_library_type) > 0: whitelist_library,
if library_type.lower() not in whitelist_library_type: blacklist_library_type,
skip_reason = "is not whitelist_library_type" whitelist_library_type,
library_mapping,
# if whitelist is not empty and library is not in whitelist ):
if len(whitelist_library) > 0: skip_reason = None
if library_title.lower() not in [x.lower() for x in whitelist_library]:
skip_reason = "is not whitelist_library" if library_type.lower() in blacklist_library_type:
skip_reason = "is blacklist_library_type"
if library_other:
if library_other.lower() not in [x.lower() for x in whitelist_library]: if library_title.lower() in [x.lower() for x in blacklist_library]:
skip_reason = "is not whitelist_library" skip_reason = "is blacklist_library"
return skip_reason library_other = None
if library_mapping:
library_other = search_mapping(library_mapping, library_title)
if library_other:
if library_other.lower() in [x.lower() for x in blacklist_library]:
skip_reason = "is blacklist_library"
if len(whitelist_library_type) > 0:
if library_type.lower() not in whitelist_library_type:
skip_reason = "is not whitelist_library_type"
# if whitelist is not empty and library is not in whitelist
if len(whitelist_library) > 0:
if library_title.lower() not in [x.lower() for x in whitelist_library]:
skip_reason = "is not whitelist_library"
if library_other:
if library_other.lower() not in [x.lower() for x in whitelist_library]:
skip_reason = "is not whitelist_library"
return skip_reason
def generate_library_guids_dict(user_list: dict):
show_output_dict = {}
episode_output_dict = {}
movies_output_dict = {}
try:
show_output_keys = user_list.keys()
show_output_keys = [dict(x) for x in list(show_output_keys)]
for show_key in show_output_keys:
for provider_key, provider_value in show_key.items():
# Skip title
if provider_key.lower() == "title":
continue
if provider_key.lower() not in show_output_dict:
show_output_dict[provider_key.lower()] = []
if provider_key.lower() == "locations":
for show_location in provider_value:
show_output_dict[provider_key.lower()].append(show_location)
else:
show_output_dict[provider_key.lower()].append(
provider_value.lower()
)
except Exception:
logger("Generating show_output_dict failed, skipping", 1)
try:
for show in user_list:
for season in user_list[show]:
for episode in user_list[show][season]:
for episode_key, episode_value in episode.items():
if episode_key.lower() not in episode_output_dict:
episode_output_dict[episode_key.lower()] = []
if episode_key == "locations":
for episode_location in episode_value:
episode_output_dict[episode_key.lower()].append(
episode_location
)
else:
episode_output_dict[episode_key.lower()].append(
episode_value.lower()
)
except Exception:
logger("Generating episode_output_dict failed, skipping", 1)
try:
for movie in user_list:
for movie_key, movie_value in movie.items():
if movie_key.lower() not in movies_output_dict:
movies_output_dict[movie_key.lower()] = []
if movie_key == "locations":
for movie_location in movie_value:
movies_output_dict[movie_key.lower()].append(movie_location)
else:
movies_output_dict[movie_key.lower()].append(movie_value.lower())
except Exception:
logger("Generating movies_output_dict failed, skipping", 1)
return show_output_dict, episode_output_dict, movies_output_dict
def combine_watched_dicts(dicts: list):
combined_dict = {}
for single_dict in dicts:
for key, value in single_dict.items():
if key not in combined_dict:
combined_dict[key] = {}
for subkey, subvalue in value.items():
combined_dict[key][subkey] = subvalue
return combined_dict
def future_thread_executor(args: list, workers: int = -1):
futures_list = []
results = []
if workers == -1:
workers = min(32, os.cpu_count() * 1.25)
with ThreadPoolExecutor(max_workers=workers) as executor:
for arg in args:
# * arg unpacks the list into actual arguments
futures_list.append(executor.submit(*arg))
for future in futures_list:
try:
result = future.result()
results.append(result)
except Exception as e:
raise Exception(e)
return results

View File

@@ -1,223 +1,646 @@
import requests, os import asyncio, aiohttp
from dotenv import load_dotenv from src.functions import (
from src.functions import logger, search_mapping, str_to_bool, check_skip_logic logger,
search_mapping,
load_dotenv(override=True) check_skip_logic,
generate_library_guids_dict,
jellyfin_baseurl = os.getenv("JELLYFIN_BASEURL") combine_watched_dicts,
jellyfin_token = os.getenv("JELLYFIN_TOKEN") )
class Jellyfin():
def __init__(self): class Jellyfin:
self.baseurl = jellyfin_baseurl def __init__(self, baseurl, token):
self.token = jellyfin_token self.baseurl = baseurl
self.token = token
if not self.baseurl:
raise Exception("Jellyfin baseurl not set") if not self.baseurl:
raise Exception("Jellyfin baseurl not set")
if not self.token:
raise Exception("Jellyfin token not set") if not self.token:
raise Exception("Jellyfin token not set")
self.users = self.get_users()
self.users = asyncio.run(self.get_users())
def query(self, query, query_type): async def query(self, query, query_type, session, identifiers=None):
try: try:
response = None results = None
headers = {"Accept": "application/json", "X-Emby-Token": self.token}
if query_type == "get": authorization = (
response = requests.get(self.baseurl + query, headers={"accept":"application/json", "X-Emby-Token": self.token}) "MediaBrowser , "
'Client="other", '
elif query_type == "post": 'Device="script", '
authorization = ( 'DeviceId="script", '
'MediaBrowser , ' 'Version="0.0.0"'
'Client="other", ' )
'Device="script", ' headers["X-Emby-Authorization"] = authorization
'DeviceId="script", '
'Version="0.0.0"' if query_type == "get":
) async with session.get(
response = requests.post(self.baseurl + query, headers={"accept":"application/json", "X-Emby-Authorization": authorization, "X-Emby-Token": self.token}) self.baseurl + query, headers=headers
) as response:
return response.json() results = await response.json()
except Exception as e:
logger(e, 2) elif query_type == "post":
logger(response, 2) async with session.post(
self.baseurl + query, headers=headers
def get_users(self): ) as response:
users = {} results = await response.json()
query = "/Users" # append identifiers to results
response = self.query(query, "get") if identifiers:
results["Identifiers"] = identifiers
# If reponse is not empty return results
if response:
for user in response: except Exception as e:
users[user["Name"]] = user["Id"] logger(f"Jellyfin: Query failed {e}", 2)
raise Exception(e)
return users
async def get_users(self):
def get_jellyfin_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping=None): try:
users_watched = {} users = {}
for user_name, user_id in users.items(): query_string = "/Users"
# Get all libraries async with aiohttp.ClientSession() as session:
user_name = user_name.lower() response = await self.query(query_string, "get", session)
libraries = self.query(f"/Users/{user_id}/Views", "get")["Items"] # If reponse is not empty
if response:
for library in libraries: for user in response:
library_title = library["Name"] users[user["Name"]] = user["Id"]
library_id = library["Id"]
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Filters=IsPlayed&limit=1", "get") return users
except Exception as e:
if len(watched["Items"]) == 0: logger(f"Jellyfin: Get users failed {e}", 2)
logger(f"Jellyfin: No watched items found in library {library_title}", 1) raise Exception(e)
continue
else: async def get_user_watched(
library_type = watched["Items"][0]["Type"] self, user_name, user_id, library_type, library_id, library_title
):
skip_reason = check_skip_logic(library_title, library_type, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping) try:
user_name = user_name.lower()
if skip_reason: user_watched = {}
logger(f"Jellyfin: Skipping library {library_title} {skip_reason}", 1) user_watched[user_name] = {}
continue
logger(
logger(f"Jellyfin: Generating watched for {user_name} in library {library_title}", 0) f"Jellyfin: Generating watched for {user_name} in library {library_title}",
# Movies 0,
if library_type == "Movie": )
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Filters=IsPlayed&Fields=ItemCounts,ProviderIds", "get") # Movies
for movie in watched["Items"]: async with aiohttp.ClientSession() as session:
if movie["UserData"]["Played"] == True: if library_type == "Movie":
if movie["ProviderIds"]: user_watched[user_name][library_title] = []
if user_name not in users_watched: watched = await self.query(
users_watched[user_name] = {} f"/Users/{user_id}/Items"
if library_title not in users_watched[user_name]: + f"?ParentId={library_id}&Filters=IsPlayed&Fields=ItemCounts,ProviderIds,MediaSources",
users_watched[user_name][library_title] = [] "get",
# Lowercase movie["ProviderIds"] keys session,
movie["ProviderIds"] = {k.lower(): v for k, v in movie["ProviderIds"].items()} )
users_watched[user_name][library_title].append(movie["ProviderIds"]) for movie in watched["Items"]:
if movie["UserData"]["Played"] is True:
# TV Shows movie_guids = {}
if library_type == "Episode": movie_guids["title"] = movie["Name"]
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}", "get") if "ProviderIds" in movie:
watched_shows = [x for x in watched["Items"] if x["Type"] == "Series"] # Lowercase movie["ProviderIds"] keys
movie_guids = {
for show in watched_shows: k.lower(): v
seasons = self.query(f"/Shows/{show['Id']}/Seasons?userId={user_id}&Fields=ItemCounts", "get") for k, v in movie["ProviderIds"].items()
if len(seasons["Items"]) > 0: }
for season in seasons["Items"]: if "MediaSources" in movie:
episodes = self.query(f"/Shows/{show['Id']}/Episodes?seasonId={season['Id']}&userId={user_id}&Fields=ItemCounts,ProviderIds", "get") movie_guids["locations"] = tuple(
if len(episodes["Items"]) > 0: [
for episode in episodes["Items"]: x["Path"].split("/")[-1]
if episode["UserData"]["Played"] == True: for x in movie["MediaSources"]
if episode["ProviderIds"]: ]
if user_name not in users_watched: )
users_watched[user_name] = {} user_watched[user_name][library_title].append(movie_guids)
if library_title not in users_watched[user_name]:
users_watched[user_name][library_title] = {} # TV Shows
if show["Name"] not in users_watched[user_name][library_title]: if library_type == "Series":
users_watched[user_name][library_title][show["Name"]] = {} user_watched[user_name][library_title] = {}
if season["Name"] not in users_watched[user_name][library_title][show["Name"]]: watched_shows = await self.query(
users_watched[user_name][library_title][show["Name"]][season["Name"]] = [] f"/Users/{user_id}/Items"
+ f"?ParentId={library_id}&isPlaceHolder=false&Fields=ProviderIds,Path,RecursiveItemCount",
# Lowercase episode["ProviderIds"] keys "get",
episode["ProviderIds"] = {k.lower(): v for k, v in episode["ProviderIds"].items()} session,
users_watched[user_name][library_title][show["Name"]][season["Name"]].append(episode["ProviderIds"]) )
watched_shows_filtered = []
return users_watched for show in watched_shows["Items"]:
if "PlayedPercentage" in show["UserData"]:
def update_watched(self, watched_list, user_mapping=None, library_mapping=None, dryrun=False): if show["UserData"]["PlayedPercentage"] > 0:
for user, libraries in watched_list.items(): watched_shows_filtered.append(show)
if user_mapping: seasons_tasks = []
user_other = None for show in watched_shows_filtered:
show_guids = {
if user in user_mapping.keys(): k.lower(): v for k, v in show["ProviderIds"].items()
user_other = user_mapping[user] }
elif user in user_mapping.values(): show_guids["title"] = show["Name"]
user_other = search_mapping(user_mapping, user) show_guids["locations"] = tuple([show["Path"].split("/")[-1]])
show_guids = frozenset(show_guids.items())
if user_other: identifiers = {"show_guids": show_guids, "show_id": show["Id"]}
logger(f"Swapping user {user} with {user_other}", 1) task = asyncio.ensure_future(
user = user_other self.query(
f"/Shows/{show['Id']}/Seasons"
user_id = None + f"?userId={user_id}&isPlaceHolder=false&Fields=ProviderIds,RecursiveItemCount",
for key, value in self.users.items(): "get",
if user.lower() == key.lower(): session,
user_id = self.users[key] frozenset(identifiers.items()),
break )
)
if not user_id: seasons_tasks.append(task)
logger(f"{user} not found in Jellyfin", 2)
break seasons_watched = await asyncio.gather(*seasons_tasks)
seasons_watched_filtered = []
jellyfin_libraries = self.query(f"/Users/{user_id}/Views", "get")["Items"]
for seasons in seasons_watched:
for library, videos in libraries.items(): seasons_watched_filtered_dict = {}
if library_mapping: seasons_watched_filtered_dict["Identifiers"] = seasons[
library_other = None "Identifiers"
]
if library in library_mapping.keys(): seasons_watched_filtered_dict["Items"] = []
library_other = library_mapping[library] for season in seasons["Items"]:
elif library in library_mapping.values(): if "PlayedPercentage" in season["UserData"]:
library_other = search_mapping(library_mapping, library) if season["UserData"]["PlayedPercentage"] > 0:
seasons_watched_filtered_dict["Items"].append(
if library_other: season
logger(f"Swapping library {library} with {library_other}", 1) )
library = library_other
if seasons_watched_filtered_dict["Items"]:
if library not in [x["Name"] for x in jellyfin_libraries]: seasons_watched_filtered.append(
logger(f"{library} not found in Jellyfin", 2) seasons_watched_filtered_dict
continue )
library_id = None episodes_tasks = []
for jellyfin_library in jellyfin_libraries: for seasons in seasons_watched_filtered:
if jellyfin_library["Name"] == library: if len(seasons["Items"]) > 0:
library_id = jellyfin_library["Id"] for season in seasons["Items"]:
continue season_identifiers = dict(seasons["Identifiers"])
season_identifiers["season_id"] = season["Id"]
if library_id: season_identifiers["season_name"] = season["Name"]
logger(f"Jellyfin: Updating watched for {user} in library {library}", 1) task = asyncio.ensure_future(
library_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&limit=1", "get") self.query(
library_type = library_search["Items"][0]["Type"] f"/Shows/{season_identifiers['show_id']}/Episodes"
+ f"?seasonId={season['Id']}&userId={user_id}&isPlaceHolder=false&isPlayed=true&Fields=ProviderIds,MediaSources",
# Movies "get",
if library_type == "Movie": session,
jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&isPlayed=false&Fields=ItemCounts,ProviderIds", "get") frozenset(season_identifiers.items()),
for jellyfin_video in jellyfin_search["Items"]: )
if str_to_bool(jellyfin_video["UserData"]["Played"]) == False: )
jellyfin_video_id = jellyfin_video["Id"] episodes_tasks.append(task)
for video in videos:
for key, value in jellyfin_video["ProviderIds"].items(): watched_episodes = await asyncio.gather(*episodes_tasks)
if key.lower() in video.keys() and value.lower() == video[key.lower()].lower(): for episodes in watched_episodes:
msg = f"{jellyfin_video['Name']} as watched for {user} in {library} for Jellyfin" if len(episodes["Items"]) > 0:
if not dryrun: for episode in episodes["Items"]:
logger(f"Marking {msg}", 0) if episode["UserData"]["Played"] is True:
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_video_id}", "post") if (
else: "ProviderIds" in episode
logger(f"Dryrun {msg}", 0) or "MediaSources" in episode
break ):
episode_identifiers = dict(
# TV Shows episodes["Identifiers"]
if library_type == "Episode": )
jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&isPlayed=false", "get") show_guids = episode_identifiers["show_guids"]
jellyfin_shows = [x for x in jellyfin_search["Items"] if x["Type"] == "Series"] if (
show_guids
for jellyfin_show in jellyfin_shows: not in user_watched[user_name][
if jellyfin_show["Name"] in videos.keys(): library_title
jellyfin_show_id = jellyfin_show["Id"] ]
jellyfin_episodes = self.query(f"/Shows/{jellyfin_show_id}/Episodes?userId={user_id}&Fields=ItemCounts,ProviderIds", "get") ):
for jellyfin_episode in jellyfin_episodes["Items"]: user_watched[user_name][library_title][
if str_to_bool(jellyfin_episode["UserData"]["Played"]) == False: show_guids
jellyfin_episode_id = jellyfin_episode["Id"] ] = {}
for show in videos: if (
for season in videos[show]: episode_identifiers["season_name"]
for episode in videos[show][season]: not in user_watched[user_name][
for key, value in jellyfin_episode["ProviderIds"].items(): library_title
if key.lower() in episode.keys() and value.lower() == episode[key.lower()].lower(): ][show_guids]
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} {jellyfin_episode['Name']} as watched for {user} in {library} for Jellyfin" ):
if not dryrun: user_watched[user_name][library_title][
logger(f"Marked {msg}", 0) show_guids
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_episode_id}", "post") ][episode_identifiers["season_name"]] = []
else:
logger(f"Dryrun {msg}", 0) episode_guids = {}
break if "ProviderIds" in episode:
episode_guids = {
k.lower(): v
for k, v in episode[
"ProviderIds"
].items()
}
if "MediaSources" in episode:
episode_guids["locations"] = tuple(
[
x["Path"].split("/")[-1]
for x in episode["MediaSources"]
]
)
user_watched[user_name][library_title][
show_guids
][episode_identifiers["season_name"]].append(
episode_guids
)
return user_watched
except Exception as e:
logger(
f"Jellyfin: Failed to get watched for {user_name} in library {library_title}, Error: {e}",
2,
)
raise Exception(e)
async def get_users_watched(
self,
user_name,
user_id,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
):
try:
# Get all libraries
user_name = user_name.lower()
tasks_watched = []
tasks_libraries = []
async with aiohttp.ClientSession() as session:
libraries = await self.query(f"/Users/{user_id}/Views", "get", session)
for library in libraries["Items"]:
library_id = library["Id"]
library_title = library["Name"]
identifiers = {
"library_id": library_id,
"library_title": library_title,
}
task = asyncio.ensure_future(
self.query(
f"/Users/{user_id}/Items"
+ f"?ParentId={library_id}&Filters=IsPlayed&limit=1",
"get",
session,
identifiers=identifiers,
)
)
tasks_libraries.append(task)
libraries = await asyncio.gather(
*tasks_libraries, return_exceptions=True
)
for watched in libraries:
if len(watched["Items"]) == 0:
continue
library_id = watched["Identifiers"]["library_id"]
library_title = watched["Identifiers"]["library_title"]
library_type = watched["Items"][0]["Type"]
skip_reason = check_skip_logic(
library_title,
library_type,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
)
if skip_reason:
logger(
f"Jellyfin: Skipping library {library_title} {skip_reason}",
1,
)
continue
# Get watched for user
task = asyncio.ensure_future(
self.get_user_watched(
user_name, user_id, library_type, library_id, library_title
)
)
tasks_watched.append(task)
watched = await asyncio.gather(*tasks_watched, return_exceptions=True)
return watched
except Exception as e:
logger(f"Jellyfin: Failed to get users watched, Error: {e}", 2)
raise Exception(e)
async def get_watched(
self,
users,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping=None,
):
try:
users_watched = {}
watched = []
for user_name, user_id in users.items():
watched.append(
await self.get_users_watched(
user_name,
user_id,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
)
)
for user_watched in watched:
user_watched_temp = combine_watched_dicts(user_watched)
for user, user_watched_temp in user_watched_temp.items():
if user not in users_watched:
users_watched[user] = {}
users_watched[user].update(user_watched_temp)
return users_watched
except Exception as e:
logger(f"Jellyfin: Failed to get watched, Error: {e}", 2)
raise Exception(e)
async def update_user_watched(
self, user_name, user_id, library, library_id, videos, dryrun
):
try:
logger(
f"Jellyfin: Updating watched for {user_name} in library {library}", 1
)
(
videos_shows_ids,
videos_episodes_ids,
videos_movies_ids,
) = generate_library_guids_dict(videos)
logger(
f"Jellyfin: mark list\nShows: {videos_shows_ids}\nEpisodes: {videos_episodes_ids}\nMovies: {videos_movies_ids}",
1,
)
async with aiohttp.ClientSession() as session:
if videos_movies_ids:
jellyfin_search = await self.query(
f"/Users/{user_id}/Items"
+ f"?SortBy=SortName&SortOrder=Ascending&Recursive=false&ParentId={library_id}"
+ "&isPlayed=false&Fields=ItemCounts,ProviderIds,MediaSources",
"get",
session,
)
for jellyfin_video in jellyfin_search["Items"]:
movie_found = False
if "MediaSources" in jellyfin_video:
for movie_location in jellyfin_video["MediaSources"]:
if (
movie_location["Path"].split("/")[-1]
in videos_movies_ids["locations"]
):
movie_found = True
break
if not movie_found:
for (
movie_provider_source,
movie_provider_id,
) in jellyfin_video["ProviderIds"].items():
if movie_provider_source.lower() in videos_movies_ids:
if (
movie_provider_id.lower()
in videos_movies_ids[
movie_provider_source.lower()
]
):
movie_found = True
break
if movie_found:
jellyfin_video_id = jellyfin_video["Id"]
msg = f"{jellyfin_video['Name']} as watched for {user_name} in {library} for Jellyfin"
if not dryrun:
logger(f"Marking {msg}", 0)
await self.query(
f"/Users/{user_id}/PlayedItems/{jellyfin_video_id}",
"post",
session,
)
else:
logger(f"Dryrun {msg}", 0)
else:
logger(
f"Jellyfin: Skipping movie {jellyfin_video['Name']} as it is not in mark list for {user_name}",
1,
)
# TV Shows
if videos_shows_ids and videos_episodes_ids:
jellyfin_search = await self.query(
f"/Users/{user_id}/Items"
+ f"?SortBy=SortName&SortOrder=Ascending&Recursive=false&ParentId={library_id}"
+ "&isPlayed=false&Fields=ItemCounts,ProviderIds,Path",
"get",
session,
)
jellyfin_shows = [x for x in jellyfin_search["Items"]]
for jellyfin_show in jellyfin_shows:
show_found = False
if "Path" in jellyfin_show:
if (
jellyfin_show["Path"].split("/")[-1]
in videos_shows_ids["locations"]
):
show_found = True
if not show_found:
for show_provider_source, show_provider_id in jellyfin_show[
"ProviderIds"
].items():
if show_provider_source.lower() in videos_shows_ids:
if (
show_provider_id.lower()
in videos_shows_ids[
show_provider_source.lower()
]
):
show_found = True
break
if show_found:
logger(
f"Jellyfin: Updating watched for {user_name} in library {library} for show {jellyfin_show['Name']}",
1,
)
jellyfin_show_id = jellyfin_show["Id"]
jellyfin_episodes = await self.query(
f"/Shows/{jellyfin_show_id}/Episodes"
+ f"?userId={user_id}&Fields=ItemCounts,ProviderIds,MediaSources",
"get",
session,
)
for jellyfin_episode in jellyfin_episodes["Items"]:
episode_found = False
if "MediaSources" in jellyfin_episode:
for episode_location in jellyfin_episode[
"MediaSources"
]:
if (
episode_location["Path"].split("/")[-1]
in videos_episodes_ids["locations"]
):
episode_found = True
break
if not episode_found:
for (
episode_provider_source,
episode_provider_id,
) in jellyfin_episode["ProviderIds"].items():
if (
episode_provider_source.lower()
in videos_episodes_ids
):
if (
episode_provider_id.lower()
in videos_episodes_ids[
episode_provider_source.lower()
]
):
episode_found = True
break
if episode_found:
jellyfin_episode_id = jellyfin_episode["Id"]
msg = (
f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['Name']}"
+ f" as watched for {user_name} in {library} for Jellyfin"
)
if not dryrun:
logger(f"Marked {msg}", 0)
await self.query(
f"/Users/{user_id}/PlayedItems/{jellyfin_episode_id}",
"post",
session,
)
else:
logger(f"Dryrun {msg}", 0)
else:
logger(
f"Jellyfin: Skipping episode {jellyfin_episode['Name']} as it is not in mark list for {user_name}",
1,
)
else:
logger(
f"Jellyfin: Skipping show {jellyfin_show['Name']} as it is not in mark list for {user_name}",
1,
)
if (
not videos_movies_ids
and not videos_shows_ids
and not videos_episodes_ids
):
logger(
f"Jellyfin: No videos to mark as watched for {user_name} in library {library}",
1,
)
except Exception as e:
logger(
f"Jellyfin: Error updating watched for {user_name} in library {library}, {e}",
2,
)
raise Exception(e)
async def update_watched(
self, watched_list, user_mapping=None, library_mapping=None, dryrun=False
):
try:
tasks = []
async with aiohttp.ClientSession() as session:
for user, libraries in watched_list.items():
logger(f"Jellyfin: Updating for entry {user}, {libraries}", 1)
user_other = None
user_name = None
if user_mapping:
if user in user_mapping.keys():
user_other = user_mapping[user]
elif user in user_mapping.values():
user_other = search_mapping(user_mapping, user)
user_id = None
for key in self.users.keys():
if user.lower() == key.lower():
user_id = self.users[key]
user_name = key
break
elif user_other and user_other.lower() == key.lower():
user_id = self.users[key]
user_name = key
break
if not user_id:
logger(f"{user} {user_other} not found in Jellyfin", 2)
continue
jellyfin_libraries = await self.query(
f"/Users/{user_id}/Views", "get", session
)
jellyfin_libraries = [x for x in jellyfin_libraries["Items"]]
for library, videos in libraries.items():
library_other = None
if library_mapping:
if library in library_mapping.keys():
library_other = library_mapping[library]
elif library in library_mapping.values():
library_other = search_mapping(library_mapping, library)
if library.lower() not in [
x["Name"].lower() for x in jellyfin_libraries
]:
if library_other:
if library_other.lower() in [
x["Name"].lower() for x in jellyfin_libraries
]:
logger(
f"Jellyfin: Library {library} not found, but {library_other} found, using {library_other}",
1,
)
library = library_other
else:
logger(
f"Jellyfin: Library {library} or {library_other} not found in library list",
2,
)
continue
else:
logger(
f"Jellyfin: Library {library} not found in library list",
2,
)
continue
library_id = None
for jellyfin_library in jellyfin_libraries:
if jellyfin_library["Name"] == library:
library_id = jellyfin_library["Id"]
continue
if library_id:
task = self.update_user_watched(
user_name, user_id, library, library_id, videos, dryrun
)
tasks.append(task)
await asyncio.gather(*tasks, return_exceptions=True)
except Exception as e:
logger(f"Jellyfin: Error updating watched, {e}", 2)
raise Exception(e)

662
src/main.py Normal file
View File

@@ -0,0 +1,662 @@
import copy, os, traceback, json, asyncio
from dotenv import load_dotenv
from time import sleep, perf_counter
from src.functions import (
logger,
str_to_bool,
search_mapping,
generate_library_guids_dict,
)
from src.plex import Plex
from src.jellyfin import Jellyfin
load_dotenv(override=True)
def cleanup_watched(
watched_list_1, watched_list_2, user_mapping=None, library_mapping=None
):
modified_watched_list_1 = copy.deepcopy(watched_list_1)
# remove entries from plex_watched that are in jellyfin_watched
for user_1 in watched_list_1:
user_other = None
if user_mapping:
user_other = search_mapping(user_mapping, user_1)
if user_1 in modified_watched_list_1:
if user_1 in watched_list_2:
user_2 = user_1
elif user_other in watched_list_2:
user_2 = user_other
else:
logger(f"User {user_1} and {user_other} not found in watched list 2", 1)
continue
for library_1 in watched_list_1[user_1]:
library_other = None
if library_mapping:
library_other = search_mapping(library_mapping, library_1)
if library_1 in modified_watched_list_1[user_1]:
if library_1 in watched_list_2[user_2]:
library_2 = library_1
elif library_other in watched_list_2[user_2]:
library_2 = library_other
else:
logger(
f"library {library_1} and {library_other} not found in watched list 2",
1,
)
continue
(
_,
episode_watched_list_2_keys_dict,
movies_watched_list_2_keys_dict,
) = generate_library_guids_dict(watched_list_2[user_2][library_2])
# Movies
if isinstance(watched_list_1[user_1][library_1], list):
for movie in watched_list_1[user_1][library_1]:
movie_found = False
for movie_key, movie_value in movie.items():
if movie_key == "locations":
if (
"locations"
in movies_watched_list_2_keys_dict.keys()
):
for location in movie_value:
if (
location
in movies_watched_list_2_keys_dict[
"locations"
]
):
movie_found = True
break
else:
if (
movie_key
in movies_watched_list_2_keys_dict.keys()
):
if (
movie_value
in movies_watched_list_2_keys_dict[
movie_key
]
):
movie_found = True
if movie_found:
logger(f"Removing {movie} from {library_1}", 3)
modified_watched_list_1[user_1][library_1].remove(
movie
)
break
# TV Shows
elif isinstance(watched_list_1[user_1][library_1], dict):
# Generate full list of provider ids for episodes in watch_list_2 to easily compare if they exist in watch_list_1
for show_key_1 in watched_list_1[user_1][library_1].keys():
show_key_dict = dict(show_key_1)
for season in watched_list_1[user_1][library_1][show_key_1]:
for episode in watched_list_1[user_1][library_1][
show_key_1
][season]:
episode_found = False
for episode_key, episode_value in episode.items():
# If episode_key and episode_value are in episode_watched_list_2_keys_dict exactly, then remove from watch_list_1
if episode_key == "locations":
if (
"locations"
in episode_watched_list_2_keys_dict.keys()
):
for location in episode_value:
if (
location
in episode_watched_list_2_keys_dict[
"locations"
]
):
episode_found = True
break
else:
if (
episode_key
in episode_watched_list_2_keys_dict.keys()
):
if (
episode_value
in episode_watched_list_2_keys_dict[
episode_key
]
):
episode_found = True
if episode_found:
if (
episode
in modified_watched_list_1[user_1][
library_1
][show_key_1][season]
):
logger(
f"Removing {episode} from {show_key_dict['title']}",
3,
)
modified_watched_list_1[user_1][
library_1
][show_key_1][season].remove(episode)
break
# Remove empty seasons
if (
len(
modified_watched_list_1[user_1][library_1][
show_key_1
][season]
)
== 0
):
if (
season
in modified_watched_list_1[user_1][library_1][
show_key_1
]
):
logger(
f"Removing {season} from {show_key_dict['title']} because it is empty",
3,
)
del modified_watched_list_1[user_1][library_1][
show_key_1
][season]
# If the show is empty, remove the show
if (
len(
modified_watched_list_1[user_1][library_1][
show_key_1
]
)
== 0
):
if (
show_key_1
in modified_watched_list_1[user_1][library_1]
):
logger(
f"Removing {show_key_dict['title']} from {library_1} because it is empty",
1,
)
del modified_watched_list_1[user_1][library_1][
show_key_1
]
for user_1 in watched_list_1:
for library_1 in watched_list_1[user_1]:
if library_1 in modified_watched_list_1[user_1]:
# If library is empty then remove it
if len(modified_watched_list_1[user_1][library_1]) == 0:
logger(f"Removing {library_1} from {user_1} because it is empty", 1)
del modified_watched_list_1[user_1][library_1]
if user_1 in modified_watched_list_1:
# If user is empty delete user
if len(modified_watched_list_1[user_1]) == 0:
logger(f"Removing {user_1} from watched list 1 because it is empty", 1)
del modified_watched_list_1[user_1]
return modified_watched_list_1
def setup_black_white_lists(
blacklist_library: str,
whitelist_library: str,
blacklist_library_type: str,
whitelist_library_type: str,
blacklist_users: str,
whitelist_users: str,
library_mapping=None,
user_mapping=None,
):
if blacklist_library:
if len(blacklist_library) > 0:
blacklist_library = blacklist_library.split(",")
blacklist_library = [x.strip() for x in blacklist_library]
if library_mapping:
temp_library = []
for library in blacklist_library:
library_other = search_mapping(library_mapping, library)
if library_other:
temp_library.append(library_other)
blacklist_library = blacklist_library + temp_library
else:
blacklist_library = []
logger(f"Blacklist Library: {blacklist_library}", 1)
if whitelist_library:
if len(whitelist_library) > 0:
whitelist_library = whitelist_library.split(",")
whitelist_library = [x.strip() for x in whitelist_library]
if library_mapping:
temp_library = []
for library in whitelist_library:
library_other = search_mapping(library_mapping, library)
if library_other:
temp_library.append(library_other)
whitelist_library = whitelist_library + temp_library
else:
whitelist_library = []
logger(f"Whitelist Library: {whitelist_library}", 1)
if blacklist_library_type:
if len(blacklist_library_type) > 0:
blacklist_library_type = blacklist_library_type.split(",")
blacklist_library_type = [x.lower().strip() for x in blacklist_library_type]
else:
blacklist_library_type = []
logger(f"Blacklist Library Type: {blacklist_library_type}", 1)
if whitelist_library_type:
if len(whitelist_library_type) > 0:
whitelist_library_type = whitelist_library_type.split(",")
whitelist_library_type = [x.lower().strip() for x in whitelist_library_type]
else:
whitelist_library_type = []
logger(f"Whitelist Library Type: {whitelist_library_type}", 1)
if blacklist_users:
if len(blacklist_users) > 0:
blacklist_users = blacklist_users.split(",")
blacklist_users = [x.lower().strip() for x in blacklist_users]
if user_mapping:
temp_users = []
for user in blacklist_users:
user_other = search_mapping(user_mapping, user)
if user_other:
temp_users.append(user_other)
blacklist_users = blacklist_users + temp_users
else:
blacklist_users = []
logger(f"Blacklist Users: {blacklist_users}", 1)
if whitelist_users:
if len(whitelist_users) > 0:
whitelist_users = whitelist_users.split(",")
whitelist_users = [x.lower().strip() for x in whitelist_users]
if user_mapping:
temp_users = []
for user in whitelist_users:
user_other = search_mapping(user_mapping, user)
if user_other:
temp_users.append(user_other)
whitelist_users = whitelist_users + temp_users
else:
whitelist_users = []
else:
whitelist_users = []
logger(f"Whitelist Users: {whitelist_users}", 1)
return (
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
blacklist_users,
whitelist_users,
)
def setup_users(
server_1, server_2, blacklist_users, whitelist_users, user_mapping=None
):
# generate list of users from server 1 and server 2
server_1_type = server_1[0]
server_1_connection = server_1[1]
server_2_type = server_2[0]
server_2_connection = server_2[1]
print(f"Server 1: {server_1_type} {server_1_connection}")
print(f"Server 2: {server_2_type} {server_2_connection}")
server_1_users = []
if server_1_type == "plex":
server_1_users = [x.title.lower() for x in server_1_connection.users]
elif server_1_type == "jellyfin":
server_1_users = [key.lower() for key in server_1_connection.users.keys()]
server_2_users = []
if server_2_type == "plex":
server_2_users = [x.title.lower() for x in server_2_connection.users]
elif server_2_type == "jellyfin":
server_2_users = [key.lower() for key in server_2_connection.users.keys()]
# combined list of overlapping users from plex and jellyfin
users = {}
for server_1_user in server_1_users:
if user_mapping:
jellyfin_plex_mapped_user = search_mapping(user_mapping, server_1_user)
if jellyfin_plex_mapped_user:
users[server_1_user] = jellyfin_plex_mapped_user
continue
if server_1_user in server_2_users:
users[server_1_user] = server_1_user
for server_2_user in server_2_users:
if user_mapping:
plex_jellyfin_mapped_user = search_mapping(user_mapping, server_2_user)
if plex_jellyfin_mapped_user:
users[plex_jellyfin_mapped_user] = server_2_user
continue
if server_2_user in server_1_users:
users[server_2_user] = server_2_user
logger(f"User list that exist on both servers {users}", 1)
users_filtered = {}
for user in users:
# whitelist_user is not empty and user lowercase is not in whitelist lowercase
if len(whitelist_users) > 0:
if user not in whitelist_users and users[user] not in whitelist_users:
logger(f"{user} or {users[user]} is not in whitelist", 1)
continue
if user not in blacklist_users and users[user] not in blacklist_users:
users_filtered[user] = users[user]
logger(f"Filtered user list {users_filtered}", 1)
if server_1_type == "plex":
output_server_1_users = []
for plex_user in server_1_connection.users:
if (
plex_user.title.lower() in users_filtered.keys()
or plex_user.title.lower() in users_filtered.values()
):
output_server_1_users.append(plex_user)
elif server_1_type == "jellyfin":
output_server_1_users = {}
for jellyfin_user, jellyfin_id in server_1_connection.users.items():
if (
jellyfin_user.lower() in users_filtered.keys()
or jellyfin_user.lower() in users_filtered.values()
):
output_server_1_users[jellyfin_user] = jellyfin_id
if server_2_type == "plex":
output_server_2_users = []
for plex_user in server_2_connection.users:
if (
plex_user.title.lower() in users_filtered.keys()
or plex_user.title.lower() in users_filtered.values()
):
output_server_2_users.append(plex_user)
elif server_2_type == "jellyfin":
output_server_2_users = {}
for jellyfin_user, jellyfin_id in server_2_connection.users.items():
if (
jellyfin_user.lower() in users_filtered.keys()
or jellyfin_user.lower() in users_filtered.values()
):
output_server_2_users[jellyfin_user] = jellyfin_id
if len(output_server_1_users) == 0:
raise Exception(
f"No users found for server 1, users found {users} filtered users {users_filtered}"
)
if len(output_server_2_users) == 0:
raise Exception(
f"No users found for server 2, users found {users} filtered users {users_filtered}"
)
logger(f"Server 1 users: {output_server_1_users}", 1)
logger(f"Server 2 users: {output_server_2_users}", 1)
return output_server_1_users, output_server_2_users
def generate_server_connections():
servers = []
plex_baseurl = os.getenv("PLEX_BASEURL", None)
plex_token = os.getenv("PLEX_TOKEN", None)
plex_username = os.getenv("PLEX_USERNAME", None)
plex_password = os.getenv("PLEX_PASSWORD", None)
plex_servername = os.getenv("PLEX_SERVERNAME", None)
ssl_bypass = str_to_bool(os.getenv("SSL_BYPASS", "False"))
if plex_baseurl and plex_token:
plex_baseurl = plex_baseurl.split(",")
plex_token = plex_token.split(",")
if len(plex_baseurl) != len(plex_token):
raise Exception(
"PLEX_BASEURL and PLEX_TOKEN must have the same number of entries"
)
for i, url in enumerate(plex_baseurl):
servers.append(
(
"plex",
Plex(
baseurl=url.strip(),
token=plex_token[i].strip(),
username=None,
password=None,
servername=None,
ssl_bypass=ssl_bypass,
),
)
)
if plex_username and plex_password and plex_servername:
plex_username = plex_username.split(",")
plex_password = plex_password.split(",")
plex_servername = plex_servername.split(",")
if len(plex_username) != len(plex_password) or len(plex_username) != len(
plex_servername
):
raise Exception(
"PLEX_USERNAME, PLEX_PASSWORD and PLEX_SERVERNAME must have the same number of entries"
)
for i, username in enumerate(plex_username):
servers.append(
(
"plex",
Plex(
baseurl=None,
token=None,
username=username.strip(),
password=plex_password[i].strip(),
servername=plex_servername[i].strip(),
ssl_bypass=ssl_bypass,
),
)
)
jellyfin_baseurl = os.getenv("JELLYFIN_BASEURL", None)
jellyfin_token = os.getenv("JELLYFIN_TOKEN", None)
if jellyfin_baseurl and jellyfin_token:
jellyfin_baseurl = jellyfin_baseurl.split(",")
jellyfin_token = jellyfin_token.split(",")
if len(jellyfin_baseurl) != len(jellyfin_token):
raise Exception(
"JELLYFIN_BASEURL and JELLYFIN_TOKEN must have the same number of entries"
)
for i, baseurl in enumerate(jellyfin_baseurl):
servers.append(
(
"jellyfin",
Jellyfin(baseurl=baseurl.strip(), token=jellyfin_token[i].strip()),
)
)
return servers
def main_loop():
logfile = os.getenv("LOGFILE", "log.log")
# Delete logfile if it exists
if os.path.exists(logfile):
os.remove(logfile)
dryrun = str_to_bool(os.getenv("DRYRUN", "False"))
logger(f"Dryrun: {dryrun}", 1)
user_mapping = os.getenv("USER_MAPPING")
if user_mapping:
user_mapping = json.loads(user_mapping.lower())
logger(f"User Mapping: {user_mapping}", 1)
library_mapping = os.getenv("LIBRARY_MAPPING")
if library_mapping:
library_mapping = json.loads(library_mapping)
logger(f"Library Mapping: {library_mapping}", 1)
# Create (black/white)lists
logger("Creating (black/white)lists", 1)
blacklist_library = os.getenv("BLACKLIST_LIBRARY", None)
whitelist_library = os.getenv("WHITELIST_LIBRARY", None)
blacklist_library_type = os.getenv("BLACKLIST_LIBRARY_TYPE", None)
whitelist_library_type = os.getenv("WHITELIST_LIBRARY_TYPE", None)
blacklist_users = os.getenv("BLACKLIST_USERS", None)
whitelist_users = os.getenv("WHITELIST_USERS", None)
(
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
blacklist_users,
whitelist_users,
) = setup_black_white_lists(
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
blacklist_users,
whitelist_users,
library_mapping,
user_mapping,
)
# Create server connections
logger("Creating server connections", 1)
servers = generate_server_connections()
for server_1 in servers:
# If server is the final server in the list, then we are done with the loop
if server_1 == servers[-1]:
break
# Start server_2 at the next server in the list
for server_2 in servers[servers.index(server_1) + 1 :]:
server_1_connection = server_1[1]
server_2_connection = server_2[1]
# Create users list
logger("Creating users list", 1)
server_1_users, server_2_users = setup_users(
server_1, server_2, blacklist_users, whitelist_users, user_mapping
)
logger("Creating watched lists", 1)
server_1_watched = server_1_connection.get_watched(
server_1_users,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
)
logger("Finished creating watched list server 1", 1)
server_2_watched = asyncio.run(
server_2_connection.get_watched(
server_2_users,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
)
)
logger("Finished creating watched list server 2", 1)
logger(f"Server 1 watched: {server_1_watched}", 3)
logger(f"Server 2 watched: {server_2_watched}", 3)
logger("Cleaning Server 1 Watched", 1)
server_1_watched_filtered = cleanup_watched(
server_1_watched, server_2_watched, user_mapping, library_mapping
)
logger("Cleaning Server 2 Watched", 1)
server_2_watched_filtered = cleanup_watched(
server_2_watched, server_1_watched, user_mapping, library_mapping
)
logger(
f"server 1 watched that needs to be synced to server 2:\n{server_1_watched_filtered}",
1,
)
logger(
f"server 2 watched that needs to be synced to server 1:\n{server_2_watched_filtered}",
1,
)
server_1_connection.update_watched(
server_2_watched_filtered, user_mapping, library_mapping, dryrun
)
asyncio.run(
server_2_connection.update_watched(
server_1_watched_filtered, user_mapping, library_mapping, dryrun
)
)
def main():
sleep_duration = float(os.getenv("SLEEP_DURATION", "3600"))
times = []
while True:
try:
start = perf_counter()
main_loop()
end = perf_counter()
times.append(end - start)
if len(times) > 0:
logger(f"Average time: {sum(times) / len(times)}", 0)
logger(f"Looping in {sleep_duration}")
sleep(sleep_duration)
except Exception as error:
if isinstance(error, list):
for message in error:
logger(message, log_type=2)
else:
logger(error, log_type=2)
logger(traceback.format_exc(), 2)
logger(f"Retrying in {sleep_duration}", log_type=0)
sleep(sleep_duration)
except KeyboardInterrupt:
logger("Exiting", log_type=0)
os._exit(0)

View File

@@ -1,216 +1,441 @@
import re, os import re, requests
from dotenv import load_dotenv from urllib3.poolmanager import PoolManager
from src.functions import logger, search_mapping, check_skip_logic from plexapi.server import PlexServer
from plexapi.server import PlexServer from plexapi.myplex import MyPlexAccount
from plexapi.myplex import MyPlexAccount
from src.functions import (
load_dotenv(override=True) logger,
search_mapping,
plex_baseurl = os.getenv("PLEX_BASEURL") check_skip_logic,
plex_token = os.getenv("PLEX_TOKEN") generate_library_guids_dict,
username = os.getenv("PLEX_USERNAME") future_thread_executor,
password = os.getenv("PLEX_PASSWORD") )
servername = os.getenv("PLEX_SERVERNAME")
# Bypass hostname validation for ssl. Taken from https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186
# class plex accept base url and token and username and password but default with none class HostNameIgnoringAdapter(requests.adapters.HTTPAdapter):
class Plex: def init_poolmanager(self, connections, maxsize, block=..., **pool_kwargs):
def __init__(self): self.poolmanager = PoolManager(num_pools=connections,
self.baseurl = plex_baseurl maxsize=maxsize,
self.token = plex_token block=block,
self.username = username assert_hostname=False,
self.password = password **pool_kwargs)
self.servername = servername
self.plex = self.plex_login() def get_user_watched(user, user_plex, library):
self.admin_user = self.plex.myPlexAccount() try:
self.users = self.get_plex_users() user_name = user.title.lower()
user_watched = {}
def plex_login(self): user_watched[user_name] = {}
try:
if self.baseurl and self.token: logger(
# Login via token f"Plex: Generating watched for {user_name} in library {library.title}",
plex = PlexServer(self.baseurl, self.token) 0,
elif self.username and self.password and self.servername: )
# Login via plex account
account = MyPlexAccount(self.username, self.password) if library.type == "movie":
plex = account.resource(self.servername).connect() user_watched[user_name][library.title] = []
else:
raise Exception("No complete plex credentials provided") library_videos = user_plex.library.section(library.title)
for video in library_videos.search(unwatched=False):
return plex movie_guids = {}
except Exception as e: for guid in video.guids:
if self.username or self.password: guid_source = re.search(r"(.*)://", guid.id).group(1).lower()
msg = f"Failed to login via plex account {self.username}" guid_id = re.search(r"://(.*)", guid.id).group(1)
logger(f"Plex: Failed to login, {msg}, Error: {e}", 2) movie_guids[guid_source] = guid_id
else:
logger(f"Plex: Failed to login, Error: {e}", 2) movie_guids["title"] = video.title
return None movie_guids["locations"] = tuple(
[x.split("/")[-1] for x in video.locations]
)
def get_plex_users(self):
users = self.plex.myPlexAccount().users() user_watched[user_name][library.title].append(movie_guids)
# append self to users elif library.type == "show":
users.append(self.plex.myPlexAccount()) user_watched[user_name][library.title] = {}
return users library_videos = user_plex.library.section(library.title)
for show in library_videos.search(unwatched=False):
def get_plex_user_watched(self, user, library): show_guids = {}
if self.admin_user == user: for show_guid in show.guids:
user_plex = self.plex # Extract after :// from guid.id
else: show_guid_source = (
user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier)) re.search(r"(.*)://", show_guid.id).group(1).lower()
)
watched = None show_guid_id = re.search(r"://(.*)", show_guid.id).group(1)
show_guids[show_guid_source] = show_guid_id
if library.type == "movie":
watched = [] show_guids["title"] = show.title
library_videos = user_plex.library.section(library.title) show_guids["locations"] = tuple(
for video in library_videos.search(unmatched=False, unwatched=False): [x.split("/")[-1] for x in show.locations]
guids = {} )
for guid in video.guids: show_guids = frozenset(show_guids.items())
guid_source = re.search(r'(.*)://', guid.id).group(1).lower()
guid_id = re.search(r'://(.*)', guid.id).group(1) for season in show.seasons():
guids[guid_source] = guid_id episode_guids = []
watched.append(guids) for episode in season.episodes():
if episode.viewCount > 0:
elif library.type == "show": episode_guids_temp = {}
watched = {} for guid in episode.guids:
library_videos = user_plex.library.section(library.title) # Extract after :// from guid.id
for show in library_videos.search(unmatched=False, unwatched=False): guid_source = (
for season in show.seasons(): re.search(r"(.*)://", guid.id).group(1).lower()
guids = [] )
for episode in season.episodes(): guid_id = re.search(r"://(.*)", guid.id).group(1)
if episode.viewCount > 0: episode_guids_temp[guid_source] = guid_id
guids_temp = {}
for guid in episode.guids: episode_guids_temp["locations"] = tuple(
# Extract after :// from guid.id [x.split("/")[-1] for x in episode.locations]
guid_source = re.search(r'(.*)://', guid.id).group(1).lower() )
guid_id = re.search(r'://(.*)', guid.id).group(1) episode_guids.append(episode_guids_temp)
guids_temp[guid_source] = guid_id
if episode_guids:
guids.append(guids_temp) # append show, season, episode
if show_guids not in user_watched[user_name][library.title]:
if guids: user_watched[user_name][library.title][show_guids] = {}
# append show, season, episode if (
if show.title not in watched: season.title
watched[show.title] = {} not in user_watched[user_name][library.title][show_guids]
if season.title not in watched[show.title]: ):
watched[show.title][season.title] = {} user_watched[user_name][library.title][show_guids][
watched[show.title][season.title] = guids season.title
] = {}
return watched user_watched[user_name][library.title][show_guids][
season.title
def get_plex_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping): ] = episode_guids
# Get all libraries
libraries = self.plex.library.sections() return user_watched
users_watched = {} except Exception as e:
logger(
# for not in blacklist f"Plex: Failed to get watched for {user_name} in library {library.title}, Error: {e}",
for library in libraries: 2,
library_title = library.title )
library_type = library.type raise Exception(e)
skip_reason = check_skip_logic(library_title, library_type, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping)
def update_user_watched(user, user_plex, library, videos, dryrun):
if skip_reason: try:
logger(f"Plex: Skipping library {library_title} {skip_reason}", 1) logger(f"Plex: Updating watched for {user.title} in library {library}", 1)
continue (
videos_shows_ids,
for user in users: videos_episodes_ids,
logger(f"Plex: Generating watched for {user.title} in library {library_title}", 0) videos_movies_ids,
user_name = user.title.lower() ) = generate_library_guids_dict(videos)
watched = self.get_plex_user_watched(user, library) logger(
if watched: f"Plex: mark list\nShows: {videos_shows_ids}\nEpisodes: {videos_episodes_ids}\nMovies: {videos_movies_ids}",
if user_name not in users_watched: 1,
users_watched[user_name] = {} )
if library_title not in users_watched[user_name]:
users_watched[user_name][library_title] = [] library_videos = user_plex.library.section(library)
users_watched[user_name][library_title] = watched if videos_movies_ids:
for movies_search in library_videos.search(unwatched=True):
return users_watched movie_found = False
for movie_location in movies_search.locations:
def update_watched(self, watched_list, user_mapping=None, library_mapping=None, dryrun=False): if movie_location.split("/")[-1] in videos_movies_ids["locations"]:
for user, libraries in watched_list.items(): movie_found = True
if user_mapping: break
user_other = None
if not movie_found:
if user in user_mapping.keys(): for movie_guid in movies_search.guids:
user_other = user_mapping[user] movie_guid_source = (
elif user in user_mapping.values(): re.search(r"(.*)://", movie_guid.id).group(1).lower()
user_other = search_mapping(user_mapping, user) )
movie_guid_id = re.search(r"://(.*)", movie_guid.id).group(1)
if user_other:
logger(f"Swapping user {user} with {user_other}", 1) # If movie provider source and movie provider id are in videos_movie_ids exactly, then the movie is in the list
user = user_other if movie_guid_source in videos_movies_ids.keys():
if movie_guid_id in videos_movies_ids[movie_guid_source]:
for index, value in enumerate(self.users): movie_found = True
if user.lower() == value.title.lower(): break
user = self.users[index]
break if movie_found:
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
if self.admin_user == user: if not dryrun:
user_plex = self.plex logger(f"Marked {msg}", 0)
else: movies_search.markWatched()
user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier)) else:
logger(f"Dryrun {msg}", 0)
for library, videos in libraries.items(): else:
if library_mapping: logger(
library_other = None f"Plex: Skipping movie {movies_search.title} as it is not in mark list for {user.title}",
1,
if library in library_mapping.keys(): )
library_other = library_mapping[library]
elif library in library_mapping.values(): if videos_shows_ids and videos_episodes_ids:
library_other = search_mapping(library_mapping, library) for show_search in library_videos.search(unwatched=True):
show_found = False
if library_other: for show_location in show_search.locations:
logger(f"Swapping library {library} with {library_other}", 1) if show_location.split("/")[-1] in videos_shows_ids["locations"]:
library = library_other show_found = True
break
# if library in plex library list
library_list = user_plex.library.sections() if not show_found:
if library.lower() not in [x.title.lower() for x in library_list]: for show_guid in show_search.guids:
logger(f"Library {library} not found in Plex library list", 2) show_guid_source = (
continue re.search(r"(.*)://", show_guid.id).group(1).lower()
)
logger(f"Plex: Updating watched for {user.title} in library {library}", 1) show_guid_id = re.search(r"://(.*)", show_guid.id).group(1)
library_videos = user_plex.library.section(library)
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
if library_videos.type == "movie": if show_guid_source in videos_shows_ids.keys():
for movies_search in library_videos.search(unmatched=False, unwatched=True): if show_guid_id in videos_shows_ids[show_guid_source]:
for guid in movies_search.guids: show_found = True
guid_source = re.search(r'(.*)://', guid.id).group(1).lower() break
guid_id = re.search(r'://(.*)', guid.id).group(1)
for video in videos: if show_found:
for video_keys, video_id in video.items(): for episode_search in show_search.episodes():
if video_keys == guid_source and video_id == guid_id: episode_found = False
if movies_search.viewCount == 0:
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex" for episode_location in episode_search.locations:
if not dryrun: if (
logger(f"Marked {msg}", 0) episode_location.split("/")[-1]
movies_search.markWatched() in videos_episodes_ids["locations"]
else: ):
logger(f"Dryrun {msg}", 0) episode_found = True
break break
elif library_videos.type == "show": if not episode_found:
for show_search in library_videos.search(unmatched=False, unwatched=True): for episode_guid in episode_search.guids:
if show_search.title in videos: episode_guid_source = (
for season_search in show_search.seasons(): re.search(r"(.*)://", episode_guid.id)
for episode_search in season_search.episodes(): .group(1)
for guid in episode_search.guids: .lower()
guid_source = re.search(r'(.*)://', guid.id).group(1).lower() )
guid_id = re.search(r'://(.*)', guid.id).group(1) episode_guid_id = re.search(
for show in videos: r"://(.*)", episode_guid.id
for season in videos[show]: ).group(1)
for episode in videos[show][season]:
for episode_keys, episode_id in episode.items(): # If episode provider source and episode provider id are in videos_episodes_ids exactly, then the episode is in the list
if episode_keys == guid_source and episode_id == guid_id: if episode_guid_source in videos_episodes_ids.keys():
if episode_search.viewCount == 0: if (
msg = f"{show_search.title} {season_search.title} {episode_search.title} as watched for {user.title} in {library} for Plex" episode_guid_id
if not dryrun: in videos_episodes_ids[episode_guid_source]
logger(f"Marked {msg}", 0) ):
episode_search.markWatched() episode_found = True
else: break
logger(f"Dryrun {msg}", 0)
break if episode_found:
msg = f"{show_search.title} {episode_search.title} as watched for {user.title} in {library} for Plex"
if not dryrun:
logger(f"Marked {msg}", 0)
episode_search.markWatched()
else:
logger(f"Dryrun {msg}", 0)
else:
logger(
f"Plex: Skipping episode {episode_search.title} as it is not in mark list for {user.title}",
1,
)
else:
logger(
f"Plex: Skipping show {show_search.title} as it is not in mark list for {user.title}",
1,
)
if not videos_movies_ids and not videos_shows_ids and not videos_episodes_ids:
logger(
f"Jellyfin: No videos to mark as watched for {user.title} in library {library}",
1,
)
except Exception as e:
logger(
f"Plex: Failed to update watched for {user.title} in library {library}, Error: {e}",
2,
)
raise Exception(e)
# class plex accept base url and token and username and password but default with none
class Plex:
def __init__(
self,
baseurl=None,
token=None,
username=None,
password=None,
servername=None,
ssl_bypass=False,
):
self.baseurl = baseurl
self.token = token
self.username = username
self.password = password
self.servername = servername
self.ssl_bypass = ssl_bypass
self.plex = self.login(self.baseurl, self.token, ssl_bypass)
self.admin_user = self.plex.myPlexAccount()
self.users = self.get_users()
def login(self, baseurl, token, ssl_bypass=False):
try:
if baseurl and token:
# Login via token
if ssl_bypass:
session = requests.Session()
# By pass ssl hostname check https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186
session.mount("https://", HostNameIgnoringAdapter())
plex = PlexServer(baseurl, token, session=session)
else:
plex = PlexServer(baseurl, token)
elif self.username and self.password and self.servername:
# Login via plex account
account = MyPlexAccount(self.username, self.password)
plex = account.resource(self.servername).connect()
else:
raise Exception("No complete plex credentials provided")
return plex
except Exception as e:
if self.username or self.password:
msg = f"Failed to login via plex account {self.username}"
logger(f"Plex: Failed to login, {msg}, Error: {e}", 2)
else:
logger(f"Plex: Failed to login, Error: {e}", 2)
raise Exception(e)
def get_users(self):
try:
users = self.plex.myPlexAccount().users()
# append self to users
users.append(self.plex.myPlexAccount())
return users
except Exception as e:
logger(f"Plex: Failed to get users, Error: {e}", 2)
raise Exception(e)
def get_watched(
self,
users,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
):
try:
# Get all libraries
users_watched = {}
args = []
for user in users:
if self.admin_user == user:
user_plex = self.plex
else:
user_plex = self.login(
self.plex._baseurl, user.get_token(self.plex.machineIdentifier), self.ssl_bypass
)
libraries = user_plex.library.sections()
for library in libraries:
library_title = library.title
library_type = library.type
skip_reason = check_skip_logic(
library_title,
library_type,
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
library_mapping,
)
if skip_reason:
logger(
f"Plex: Skipping library {library_title} {skip_reason}", 1
)
continue
args.append([get_user_watched, user, user_plex, library])
for user_watched in future_thread_executor(args):
for user, user_watched_temp in user_watched.items():
if user not in users_watched:
users_watched[user] = {}
users_watched[user].update(user_watched_temp)
return users_watched
except Exception as e:
logger(f"Plex: Failed to get watched, Error: {e}", 2)
raise Exception(e)
def update_watched(
self, watched_list, user_mapping=None, library_mapping=None, dryrun=False
):
try:
args = []
for user, libraries in watched_list.items():
user_other = None
# If type of user is dict
if user_mapping:
if user in user_mapping.keys():
user_other = user_mapping[user]
elif user in user_mapping.values():
user_other = search_mapping(user_mapping, user)
for index, value in enumerate(self.users):
if user.lower() == value.title.lower():
user = self.users[index]
break
elif user_other and user_other.lower() == value.title.lower():
user = self.users[index]
break
if self.admin_user == user:
user_plex = self.plex
else:
user_plex = PlexServer(
self.plex._baseurl, user.get_token(self.plex.machineIdentifier)
)
for library, videos in libraries.items():
library_other = None
if library_mapping:
if library in library_mapping.keys():
library_other = library_mapping[library]
elif library in library_mapping.values():
library_other = search_mapping(library_mapping, library)
# if library in plex library list
library_list = user_plex.library.sections()
if library.lower() not in [x.title.lower() for x in library_list]:
if library_other:
if library_other.lower() in [
x.title.lower() for x in library_list
]:
logger(
f"Plex: Library {library} not found, but {library_other} found, using {library_other}",
1,
)
library = library_other
else:
logger(
f"Plex: Library {library} or {library_other} not found in library list",
2,
)
continue
else:
logger(
f"Plex: Library {library} not found in library list", 2
)
continue
args.append(
[
update_user_watched,
user,
user_plex,
library,
videos,
dryrun,
]
)
future_thread_executor(args)
except Exception as e:
logger(f"Plex: Failed to update watched, Error: {e}", 2)
raise Exception(e)

1
test/requirements.txt Normal file
View File

@@ -0,0 +1 @@
pytest

78
test/test_main.py Normal file
View File

@@ -0,0 +1,78 @@
import sys
import os
# getting the name of the directory
# where the this file is present.
current = os.path.dirname(os.path.realpath(__file__))
# Getting the parent directory name
# where the current directory is present.
parent = os.path.dirname(current)
# adding the parent directory to
# the sys.path.
sys.path.append(parent)
from src.main import setup_black_white_lists
def test_setup_black_white_lists():
# Simple
blacklist_library = "library1, library2"
whitelist_library = "library1, library2"
blacklist_library_type = "library_type1, library_type2"
whitelist_library_type = "library_type1, library_type2"
blacklist_users = "user1, user2"
whitelist_users = "user1, user2"
(
results_blacklist_library,
return_whitelist_library,
return_blacklist_library_type,
return_whitelist_library_type,
return_blacklist_users,
return_whitelist_users,
) = setup_black_white_lists(
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
blacklist_users,
whitelist_users,
)
assert results_blacklist_library == ["library1", "library2"]
assert return_whitelist_library == ["library1", "library2"]
assert return_blacklist_library_type == ["library_type1", "library_type2"]
assert return_whitelist_library_type == ["library_type1", "library_type2"]
assert return_blacklist_users == ["user1", "user2"]
assert return_whitelist_users == ["user1", "user2"]
# Library Mapping and user mapping
library_mapping = {"library1": "library3"}
user_mapping = {"user1": "user3"}
(
results_blacklist_library,
return_whitelist_library,
return_blacklist_library_type,
return_whitelist_library_type,
return_blacklist_users,
return_whitelist_users,
) = setup_black_white_lists(
blacklist_library,
whitelist_library,
blacklist_library_type,
whitelist_library_type,
blacklist_users,
whitelist_users,
library_mapping,
user_mapping,
)
assert results_blacklist_library == ["library1", "library2", "library3"]
assert return_whitelist_library == ["library1", "library2", "library3"]
assert return_blacklist_library_type == ["library_type1", "library_type2"]
assert return_whitelist_library_type == ["library_type1", "library_type2"]
assert return_blacklist_users == ["user1", "user2", "user3"]
assert return_whitelist_users == ["user1", "user2", "user3"]

View File

@@ -0,0 +1,301 @@
import sys
import os
# getting the name of the directory
# where the this file is present.
current = os.path.dirname(os.path.realpath(__file__))
# Getting the parent directory name
# where the current directory is present.
parent = os.path.dirname(current)
# adding the parent directory to
# the sys.path.
sys.path.append(parent)
from src.main import cleanup_watched
tv_shows_watched_list_1 = {
frozenset(
{
("tvdb", "75710"),
("title", "Criminal Minds"),
("imdb", "tt0452046"),
("locations", ("Criminal Minds",)),
("tmdb", "4057"),
}
): {
"Season 1": [
{
"imdb": "tt0550489",
"tmdb": "282843",
"tvdb": "176357",
"locations": (
"Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv",
),
},
{
"imdb": "tt0550487",
"tmdb": "282861",
"tvdb": "300385",
"locations": ("Criminal Minds S01E02 Compulsion WEBDL-720p.mkv",),
},
]
},
frozenset({("title", "Test"), ("locations", ("Test",))}): {
"Season 1": [
{"locations": ("Test S01E01.mkv",)},
{"locations": ("Test S01E02.mkv",)},
]
},
}
movies_watched_list_1 = [
{
"imdb": "tt2380307",
"tmdb": "354912",
"title": "Coco",
"locations": ("Coco (2017) Remux-1080p.mkv",),
},
{
"tmdbcollection": "448150",
"imdb": "tt1431045",
"tmdb": "293660",
"title": "Deadpool",
"locations": ("Deadpool (2016) Remux-1080p.mkv",),
},
]
tv_shows_watched_list_2 = {
frozenset(
{
("tvdb", "75710"),
("title", "Criminal Minds"),
("imdb", "tt0452046"),
("locations", ("Criminal Minds",)),
("tmdb", "4057"),
}
): {
"Season 1": [
{
"imdb": "tt0550487",
"tmdb": "282861",
"tvdb": "300385",
"locations": ("Criminal Minds S01E02 Compulsion WEBDL-720p.mkv",),
},
{
"imdb": "tt0550498",
"tmdb": "282865",
"tvdb": "300474",
"locations": (
"Criminal Minds S01E03 Won't Get Fooled Again WEBDL-720p.mkv",
),
},
]
},
frozenset({("title", "Test"), ("locations", ("Test",))}): {
"Season 1": [
{"locations": ("Test S01E02.mkv",)},
{"locations": ("Test S01E03.mkv",)},
]
},
}
movies_watched_list_2 = [
{
"imdb": "tt2380307",
"tmdb": "354912",
"title": "Coco",
"locations": ("Coco (2017) Remux-1080p.mkv",),
},
{
"imdb": "tt0384793",
"tmdb": "9788",
"tvdb": "9103",
"title": "Accepted",
"locations": ("Accepted (2006) Remux-1080p.mkv",),
},
]
# Test to see if objects get deleted all the way up to the root.
tv_shows_2_watched_list_1 = {
frozenset(
{
("tvdb", "75710"),
("title", "Criminal Minds"),
("imdb", "tt0452046"),
("locations", ("Criminal Minds",)),
("tmdb", "4057"),
}
): {
"Season 1": [
{
"imdb": "tt0550489",
"tmdb": "282843",
"tvdb": "176357",
"locations": (
"Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv",
),
},
]
}
}
expected_tv_show_watched_list_1 = {
frozenset(
{
("tvdb", "75710"),
("title", "Criminal Minds"),
("imdb", "tt0452046"),
("locations", ("Criminal Minds",)),
("tmdb", "4057"),
}
): {
"Season 1": [
{
"imdb": "tt0550489",
"tmdb": "282843",
"tvdb": "176357",
"locations": (
"Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv",
),
}
]
},
frozenset({("title", "Test"), ("locations", ("Test",))}): {
"Season 1": [{"locations": ("Test S01E01.mkv",)}]
},
}
expected_movie_watched_list_1 = [
{
"tmdbcollection": "448150",
"imdb": "tt1431045",
"tmdb": "293660",
"title": "Deadpool",
"locations": ("Deadpool (2016) Remux-1080p.mkv",),
}
]
expected_tv_show_watched_list_2 = {
frozenset(
{
("tvdb", "75710"),
("title", "Criminal Minds"),
("imdb", "tt0452046"),
("locations", ("Criminal Minds",)),
("tmdb", "4057"),
}
): {
"Season 1": [
{
"imdb": "tt0550498",
"tmdb": "282865",
"tvdb": "300474",
"locations": (
"Criminal Minds S01E03 Won't Get Fooled Again WEBDL-720p.mkv",
),
}
]
},
frozenset({("title", "Test"), ("locations", ("Test",))}): {
"Season 1": [{"locations": ("Test S01E03.mkv",)}]
},
}
expected_movie_watched_list_2 = [
{
"imdb": "tt0384793",
"tmdb": "9788",
"tvdb": "9103",
"title": "Accepted",
"locations": ("Accepted (2006) Remux-1080p.mkv",),
}
]
def test_simple_cleanup_watched():
user_watched_list_1 = {
"user1": {
"TV Shows": tv_shows_watched_list_1,
"Movies": movies_watched_list_1,
"Other Shows": tv_shows_2_watched_list_1,
},
}
user_watched_list_2 = {
"user1": {
"TV Shows": tv_shows_watched_list_2,
"Movies": movies_watched_list_2,
"Other Shows": tv_shows_2_watched_list_1,
}
}
expected_watched_list_1 = {
"user1": {
"TV Shows": expected_tv_show_watched_list_1,
"Movies": expected_movie_watched_list_1,
}
}
expected_watched_list_2 = {
"user1": {
"TV Shows": expected_tv_show_watched_list_2,
"Movies": expected_movie_watched_list_2,
}
}
return_watched_list_1 = cleanup_watched(user_watched_list_1, user_watched_list_2)
return_watched_list_2 = cleanup_watched(user_watched_list_2, user_watched_list_1)
assert return_watched_list_1 == expected_watched_list_1
assert return_watched_list_2 == expected_watched_list_2
def test_mapping_cleanup_watched():
user_watched_list_1 = {
"user1": {
"TV Shows": tv_shows_watched_list_1,
"Movies": movies_watched_list_1,
"Other Shows": tv_shows_2_watched_list_1,
},
}
user_watched_list_2 = {
"user2": {
"Shows": tv_shows_watched_list_2,
"Movies": movies_watched_list_2,
"Other Shows": tv_shows_2_watched_list_1,
}
}
expected_watched_list_1 = {
"user1": {
"TV Shows": expected_tv_show_watched_list_1,
"Movies": expected_movie_watched_list_1,
}
}
expected_watched_list_2 = {
"user2": {
"Shows": expected_tv_show_watched_list_2,
"Movies": expected_movie_watched_list_2,
}
}
user_mapping = {"user1": "user2"}
library_mapping = {"TV Shows": "Shows"}
return_watched_list_1 = cleanup_watched(
user_watched_list_1,
user_watched_list_2,
user_mapping=user_mapping,
library_mapping=library_mapping,
)
return_watched_list_2 = cleanup_watched(
user_watched_list_2,
user_watched_list_1,
user_mapping=user_mapping,
library_mapping=library_mapping,
)
assert return_watched_list_1 == expected_watched_list_1
assert return_watched_list_2 == expected_watched_list_2