Compare commits
79 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18df322c41 | ||
|
|
fc80f50560 | ||
|
|
4870ff9e7a | ||
|
|
58337bd38c | ||
|
|
e6d1e0933a | ||
|
|
68e3f25ba4 | ||
|
|
c981426db6 | ||
|
|
916b16b12c | ||
|
|
a178d230de | ||
|
|
fffb04728a | ||
|
|
658361383a | ||
|
|
3330026de6 | ||
|
|
25fe426720 | ||
|
|
8d53b5b8c0 | ||
|
|
0774735f0f | ||
|
|
a5540b94d5 | ||
|
|
c69d59858d | ||
|
|
962b1149ad | ||
|
|
a8edee0354 | ||
|
|
3627dde64d | ||
|
|
80ec0e42c2 | ||
|
|
fd64088bde | ||
|
|
7832e41a3b | ||
|
|
cadd65d69b | ||
|
|
9f004797fc | ||
|
|
9041fee7ad | ||
|
|
9af6c9057c | ||
|
|
757ce91138 | ||
|
|
98f96ed5c7 | ||
|
|
3e15120e2a | ||
|
|
5824e6c0cc | ||
|
|
7087d75efb | ||
|
|
b2a06b8fd3 | ||
|
|
1ee055faf5 | ||
|
|
404089dfca | ||
|
|
ed24948dee | ||
|
|
1f16fcb8eb | ||
|
|
03de3affd7 | ||
|
|
2bad887659 | ||
|
|
796be47a63 | ||
|
|
dc1fe11590 | ||
|
|
13b4ff3215 | ||
|
|
dca54cf4fb | ||
|
|
a4365e59f3 | ||
|
|
b960bccb86 | ||
|
|
218037200c | ||
|
|
4ac670e837 | ||
|
|
96eff65c3e | ||
|
|
45471607c8 | ||
|
|
14885744b1 | ||
|
|
d1fd61f1d1 | ||
|
|
6c1ee4a7dc | ||
|
|
9a8e799e68 | ||
|
|
ffec4e2f28 | ||
|
|
00102891a5 | ||
|
|
aa76b83428 | ||
|
|
a644189ea5 | ||
|
|
c5d987a8c9 | ||
|
|
bdd68ad68d | ||
|
|
2d86bca781 | ||
|
|
1b01ff6ec2 | ||
|
|
f08ec43507 | ||
|
|
7f9424260a | ||
|
|
5f21943353 | ||
|
|
a5a795f43c | ||
|
|
fcb6d7625f | ||
|
|
fd2179998f | ||
|
|
654e7f20e1 | ||
|
|
1eb92cf7c1 | ||
|
|
111e284cc8 | ||
|
|
1a4e3f4ec4 | ||
|
|
4066228e57 | ||
|
|
59c6d278e3 | ||
|
|
39b33f3d43 | ||
|
|
e8faf52b2b | ||
|
|
370e9bac63 | ||
|
|
d0746cec5a | ||
|
|
251937431b | ||
|
|
50faf061af |
84
.env.sample
84
.env.sample
@@ -1,43 +1,77 @@
|
||||
# Global Settings
|
||||
|
||||
## Do not mark any shows/movies as played and instead just output to log if they would of been marked.
|
||||
DRYRUN = "True"
|
||||
|
||||
## Additional logging information
|
||||
DEBUG = "True"
|
||||
DEBUG = "False"
|
||||
|
||||
## Debugging level, "info" is default, "debug" is more verbose
|
||||
DEBUG_LEVEL = "info"
|
||||
|
||||
## If set to true then the script will only run once and then exit
|
||||
RUN_ONLY_ONCE = "False"
|
||||
|
||||
## How often to run the script in seconds
|
||||
SLEEP_DURATION = "3600"
|
||||
|
||||
## Log file where all output will be written to
|
||||
LOGFILE = "log.log"
|
||||
## Map usernames between plex and jellyfin in the event that they are different, order does not matter
|
||||
#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" }
|
||||
|
||||
## Map usernames between servers in the event that they are different, order does not matter
|
||||
## Comma separated for multiple options
|
||||
#USER_MAPPING = { "testuser2": "testuser3", "testuser1":"testuser4" }
|
||||
|
||||
## 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
|
||||
## Comma seperated list for multiple servers
|
||||
PLEX_BASEURL = "http://localhost:32400"
|
||||
## Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
|
||||
PLEX_TOKEN = "SuperSecretToken"
|
||||
## If not using plex token then use username and password of the server admin along with the servername
|
||||
#PLEX_USERNAME = ""
|
||||
#PLEX_PASSWORD = ""
|
||||
#PLEX_SERVERNAME = "Plex Server"
|
||||
## Skip hostname validation for ssl certificates.
|
||||
SSL_BYPASS = "False"
|
||||
|
||||
## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly
|
||||
## Comma seperated list for multiple servers
|
||||
JELLYFIN_BASEURL = "http://localhost:8096"
|
||||
## Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key
|
||||
JELLYFIN_TOKEN = "SuperSecretToken"
|
||||
|
||||
## Map libraries between servers in the even that they are different, order does not matter
|
||||
## Comma separated for multiple options
|
||||
#LIBRARY_MAPPING = { "Shows": "TV Shows", "Movie": "Movies" }
|
||||
|
||||
## 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.
|
||||
## Comma separated for multiple options
|
||||
#BLACKLIST_LIBRARY = ""
|
||||
#WHITELIST_LIBRARY = ""
|
||||
#BLACKLIST_LIBRARY_TYPE = ""
|
||||
#BLACKLIST_LIBRARY_TYPE = ""
|
||||
#WHITELIST_LIBRARY_TYPE = ""
|
||||
#BLACKLIST_USERS = ""
|
||||
WHITELIST_USERS = "testuser1,testuser2"
|
||||
|
||||
|
||||
|
||||
# Plex
|
||||
|
||||
## 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
|
||||
## Comma separated list for multiple servers
|
||||
PLEX_BASEURL = "http://localhost:32400, https://nas:32400"
|
||||
|
||||
## Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
|
||||
## Comma separated list for multiple servers
|
||||
PLEX_TOKEN = "SuperSecretToken, SuperSecretToken2"
|
||||
|
||||
## If not using plex token then use username and password of the server admin along with the servername
|
||||
## Comma separated for multiple options
|
||||
#PLEX_USERNAME = "PlexUser, PlexUser2"
|
||||
#PLEX_PASSWORD = "SuperSecret, SuperSecret2"
|
||||
#PLEX_SERVERNAME = "Plex Server1, Plex Server2"
|
||||
|
||||
## Skip hostname validation for ssl certificates.
|
||||
## Set to True if running into ssl certificate errors
|
||||
SSL_BYPASS = "False"
|
||||
|
||||
## control the direction of syncing. e.g. SYNC_FROM_PLEX_TO_JELLYFIN set to true will cause the updates from plex
|
||||
## to be updated in jellyfin. SYNC_FROM_PLEX_TO_PLEX set to true will sync updates between multiple plex servers
|
||||
SYNC_FROM_PLEX_TO_JELLYFIN = "True"
|
||||
SYNC_FROM_JELLYFIN_TO_PLEX = "True"
|
||||
SYNC_FROM_PLEX_TO_PLEX = "True"
|
||||
SYNC_FROM_JELLYFIN_TO_JELLYFIN = "True"
|
||||
|
||||
|
||||
# Jellyfin
|
||||
|
||||
## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly
|
||||
## Comma separated list for multiple servers
|
||||
JELLYFIN_BASEURL = "http://localhost:8096, http://nas:8096"
|
||||
|
||||
## Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key
|
||||
## Comma separated list for multiple servers
|
||||
JELLYFIN_TOKEN = "SuperSecretToken, SuperSecretToken2"
|
||||
|
||||
31
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
31
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: "[BUG]"
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Logs**
|
||||
If applicable, add logs to help explain your problem ideally with DEBUG set to true, be sure to remove sensitive information
|
||||
|
||||
**Type:**
|
||||
- [ ] Docker
|
||||
- [ ] Native
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: "[Feature Request]"
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
14
.github/workflows/ci.yml
vendored
14
.github/workflows/ci.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
||||
pytest:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: "Install dependencies"
|
||||
run: pip install -r requirements.txt && pip install -r test/requirements.txt
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
needs: pytest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Docker meta
|
||||
id: docker_meta
|
||||
@@ -45,14 +45,14 @@ jobs:
|
||||
type=sha
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v1
|
||||
uses: docker/setup-qemu-action@v2
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: Login to DockerHub
|
||||
if: "${{ steps.docker_meta.outcome == 'success' }}"
|
||||
uses: docker/login-action@v1
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_TOKEN }}
|
||||
@@ -60,7 +60,7 @@ jobs:
|
||||
- name: Build
|
||||
id: build
|
||||
if: "${{ steps.docker_meta.outcome == 'skipped' }}"
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
@@ -71,7 +71,7 @@ jobs:
|
||||
- name: Build Push
|
||||
id: build_push
|
||||
if: "${{ steps.docker_meta.outcome == 'success' }}"
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
|
||||
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"[python]" : {
|
||||
"editor.formatOnSave": true,
|
||||
},
|
||||
"python.formatting.provider": "black",
|
||||
|
||||
}
|
||||
13
Dockerfile
13
Dockerfile
@@ -6,18 +6,23 @@ ENV DEBUG_LEVEL 'INFO'
|
||||
ENV SLEEP_DURATION '3600'
|
||||
ENV LOGFILE 'log.log'
|
||||
|
||||
ENV USER_MAPPING '{ "User Test": "User Test2" }'
|
||||
ENV LIBRARY_MAPPING '{ "Shows Test": "TV Shows Test" }'
|
||||
ENV USER_MAPPING ''
|
||||
ENV LIBRARY_MAPPING ''
|
||||
|
||||
ENV PLEX_BASEURL 'http://localhost:32400'
|
||||
ENV PLEX_BASEURL ''
|
||||
ENV PLEX_TOKEN ''
|
||||
ENV PLEX_USERNAME ''
|
||||
ENV PLEX_PASSWORD ''
|
||||
ENV PLEX_SERVERNAME ''
|
||||
|
||||
ENV JELLYFIN_BASEURL 'http://localhost:8096'
|
||||
ENV JELLYFIN_BASEURL ''
|
||||
ENV JELLYFIN_TOKEN ''
|
||||
|
||||
ENV SYNC_FROM_PLEX_TO_JELLYFIN 'True'
|
||||
ENV SYNC_FROM_JELLYFIN_TO_PLEX 'True'
|
||||
ENV SYNC_FROM_PLEX_TO_PLEX 'True'
|
||||
ENV SYNC_FROM_JELLYFIN_TO_JELLYFIN 'True'
|
||||
|
||||
ENV BLACKLIST_LIBRARY ''
|
||||
ENV WHITELIST_LIBRARY ''
|
||||
ENV BLACKLIST_LIBRARY_TYPE ''
|
||||
|
||||
257
README.md
257
README.md
@@ -1,73 +1,184 @@
|
||||
# JellyPlex-Watched
|
||||
|
||||
[](https://www.codacy.com/gh/luigi311/JellyPlex-Watched/dashboard?utm_source=github.com&utm_medium=referral&utm_content=luigi311/JellyPlex-Watched&utm_campaign=Badge_Grade)
|
||||
|
||||
Sync watched between jellyfin and plex
|
||||
|
||||
## Description
|
||||
|
||||
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.
|
||||
|
||||
## Configuration
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
### Baremetal
|
||||
|
||||
- Setup virtualenv of your choice
|
||||
|
||||
- Install dependencies
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
- Create a .env file similar to .env.sample, uncomment whitelist and blacklist if needed, fill in baseurls and tokens
|
||||
|
||||
- Run
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
### Docker
|
||||
|
||||
- Build docker image
|
||||
|
||||
```bash
|
||||
docker build -t jellyplex-watched .
|
||||
```
|
||||
|
||||
- or use pre-built image
|
||||
|
||||
```bash
|
||||
docker pull luigi311/jellyplex-watched:latest
|
||||
```
|
||||
|
||||
#### With variables
|
||||
|
||||
- Run
|
||||
|
||||
```bash
|
||||
docker run --rm -it -e PLEX_TOKEN='SuperSecretToken' luigi311/jellyplex-watched:latest
|
||||
```
|
||||
|
||||
#### With .env
|
||||
|
||||
- Create a .env file similar to .env.sample and set the variables to match your setup
|
||||
|
||||
- Run
|
||||
|
||||
```bash
|
||||
docker run --rm -it -v "$(pwd)/.env:/app/.env" luigi311/jellyplex-watched:latest
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
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.
|
||||
# JellyPlex-Watched
|
||||
|
||||
[](https://www.codacy.com/gh/luigi311/JellyPlex-Watched/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=luigi311/JellyPlex-Watched\&utm_campaign=Badge_Grade)
|
||||
|
||||
Sync watched between jellyfin and plex locally
|
||||
|
||||
## Description
|
||||
|
||||
Keep in sync all your users watched history between jellyfin and plex servers locally. This uses file names and provider ids 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 entering multiple options in the .env plex/jellyfin section separated by commas.
|
||||
|
||||
## Features
|
||||
|
||||
### Plex
|
||||
|
||||
* \[x] Match via Filenames
|
||||
* \[x] Match via provider ids
|
||||
* \[x] Map usersnames
|
||||
* \[x] Use single login
|
||||
* \[x] One Way/Multi Way sync
|
||||
* \[x] Sync Watched
|
||||
* \[x] Sync Inprogress
|
||||
|
||||
### Jellyfin
|
||||
|
||||
* \[x] Match via Filenames
|
||||
* \[x] Match via provider ids
|
||||
* \[x] Map usersnames
|
||||
* \[x] Use single login
|
||||
* \[x] One Way/Multi Way sync
|
||||
* \[x] Sync Watched
|
||||
* \[ ] Sync Inprogress
|
||||
|
||||
### Emby
|
||||
|
||||
* \[ ] Match via Filenames
|
||||
* \[ ] Match via provider ids
|
||||
* \[ ] Map usersnames
|
||||
* \[ ] Use single login
|
||||
* \[ ] One Way/Multi Way sync
|
||||
* \[ ] Sync Watched
|
||||
* \[ ] Sync Inprogress
|
||||
|
||||
## Configuration
|
||||
|
||||
```bash
|
||||
# Global Settings
|
||||
|
||||
## Do not mark any shows/movies as played and instead just output to log if they would of been marked.
|
||||
DRYRUN = "True"
|
||||
|
||||
## Additional logging information
|
||||
DEBUG = "False"
|
||||
|
||||
## Debugging level, "info" is default, "debug" is more verbose
|
||||
DEBUG_LEVEL = "info"
|
||||
|
||||
## If set to true then the script will only run once and then exit
|
||||
RUN_ONLY_ONCE = "False"
|
||||
|
||||
## How often to run the script in seconds
|
||||
SLEEP_DURATION = "3600"
|
||||
|
||||
## Log file where all output will be written to
|
||||
LOGFILE = "log.log"
|
||||
|
||||
## Map usernames between servers in the event that they are different, order does not matter
|
||||
## Comma separated for multiple options
|
||||
USER_MAPPING = { "testuser2": "testuser3", "testuser1":"testuser4" }
|
||||
|
||||
## Map libraries between servers in the even that they are different, order does not matter
|
||||
## Comma separated for multiple options
|
||||
LIBRARY_MAPPING = { "Shows": "TV Shows", "Movie": "Movies" }
|
||||
|
||||
## 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.
|
||||
## Comma separated for multiple options
|
||||
BLACKLIST_LIBRARY = ""
|
||||
WHITELIST_LIBRARY = ""
|
||||
BLACKLIST_LIBRARY_TYPE = ""
|
||||
WHITELIST_LIBRARY_TYPE = ""
|
||||
BLACKLIST_USERS = ""
|
||||
WHITELIST_USERS = "testuser1,testuser2"
|
||||
|
||||
|
||||
|
||||
# Plex
|
||||
|
||||
## 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
|
||||
## Comma separated list for multiple servers
|
||||
PLEX_BASEURL = "http://localhost:32400, https://nas:32400"
|
||||
|
||||
## Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
|
||||
## Comma separated list for multiple servers
|
||||
PLEX_TOKEN = "SuperSecretToken, SuperSecretToken2"
|
||||
|
||||
## If not using plex token then use username and password of the server admin along with the servername
|
||||
## Comma separated for multiple options
|
||||
#PLEX_USERNAME = "PlexUser, PlexUser2"
|
||||
#PLEX_PASSWORD = "SuperSecret, SuperSecret2"
|
||||
#PLEX_SERVERNAME = "Plex Server1, Plex Server2"
|
||||
|
||||
## Skip hostname validation for ssl certificates.
|
||||
## Set to True if running into ssl certificate errors
|
||||
SSL_BYPASS = "False"
|
||||
|
||||
|
||||
## control the direction of syncing. e.g. SYNC_FROM_PLEX_TO_JELLYFIN set to true will cause the updates from plex
|
||||
## to be updated in jellyfin. SYNC_FROM_PLEX_TO_PLEX set to true will sync updates between multiple plex servers
|
||||
SYNC_FROM_PLEX_TO_JELLYFIN = "True"
|
||||
SYNC_FROM_JELLYFIN_TO_PLEX = "True"
|
||||
SYNC_FROM_PLEX_TO_PLEX = "True"
|
||||
SYNC_FROM_JELLYFIN_TO_JELLYFIN = "True"
|
||||
|
||||
|
||||
# Jellyfin
|
||||
|
||||
## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly
|
||||
## Comma separated list for multiple servers
|
||||
JELLYFIN_BASEURL = "http://localhost:8096, http://nas:8096"
|
||||
|
||||
## Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key
|
||||
## Comma separated list for multiple servers
|
||||
JELLYFIN_TOKEN = "SuperSecretToken, SuperSecretToken2"
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
### Baremetal
|
||||
|
||||
* Setup virtualenv of your choice
|
||||
|
||||
* Install dependencies
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
* Create a .env file similar to .env.sample, uncomment whitelist and blacklist if needed, fill in baseurls and tokens
|
||||
|
||||
* Run
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
### Docker
|
||||
|
||||
* Build docker image
|
||||
|
||||
```bash
|
||||
docker build -t jellyplex-watched .
|
||||
```
|
||||
|
||||
* or use pre-built image
|
||||
|
||||
```bash
|
||||
docker pull luigi311/jellyplex-watched:latest
|
||||
```
|
||||
|
||||
#### With variables
|
||||
|
||||
* Run
|
||||
|
||||
```bash
|
||||
docker run --rm -it -e PLEX_TOKEN='SuperSecretToken' luigi311/jellyplex-watched:latest
|
||||
```
|
||||
|
||||
#### With .env
|
||||
|
||||
* Create a .env file similar to .env.sample and set the variables to match your setup
|
||||
|
||||
* Run
|
||||
|
||||
```bash
|
||||
docker run --rm -it -v "$(pwd)/.env:/app/.env" luigi311/jellyplex-watched:latest
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
I am open to receiving 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.
|
||||
|
||||
139
src/black_white.py
Normal file
139
src/black_white.py
Normal file
@@ -0,0 +1,139 @@
|
||||
from src.functions import logger, search_mapping
|
||||
|
||||
|
||||
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,
|
||||
):
|
||||
blacklist_library, blacklist_library_type, blacklist_users = setup_black_lists(
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
blacklist_users,
|
||||
library_mapping,
|
||||
user_mapping,
|
||||
)
|
||||
|
||||
whitelist_library, whitelist_library_type, whitelist_users = setup_white_lists(
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
whitelist_users,
|
||||
library_mapping,
|
||||
user_mapping,
|
||||
)
|
||||
|
||||
return (
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
blacklist_users,
|
||||
whitelist_users,
|
||||
)
|
||||
|
||||
|
||||
def setup_black_lists(
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
blacklist_users,
|
||||
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 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 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)
|
||||
|
||||
return blacklist_library, blacklist_library_type, blacklist_users
|
||||
|
||||
|
||||
def setup_white_lists(
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
whitelist_users,
|
||||
library_mapping=None,
|
||||
user_mapping=None,
|
||||
):
|
||||
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 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 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 whitelist_library, whitelist_library_type, whitelist_users
|
||||
126
src/functions.py
126
src/functions.py
@@ -8,13 +8,13 @@ logfile = os.getenv("LOGFILE", "log.log")
|
||||
|
||||
|
||||
def logger(message: str, log_type=0):
|
||||
debug = str_to_bool(os.getenv("DEBUG", "True"))
|
||||
debug = str_to_bool(os.getenv("DEBUG", "False"))
|
||||
debug_level = os.getenv("DEBUG_LEVEL", "info").lower()
|
||||
|
||||
output = str(message)
|
||||
if log_type == 0:
|
||||
pass
|
||||
elif log_type == 1 and (debug and debug_level == "info"):
|
||||
elif log_type == 1 and (debug and debug_level in ("info", "debug")):
|
||||
output = f"[INFO]: {output}"
|
||||
elif log_type == 2:
|
||||
output = f"[ERROR]: {output}"
|
||||
@@ -39,6 +39,14 @@ def str_to_bool(value: any) -> bool:
|
||||
return str(value).lower() in ("y", "yes", "t", "true", "on", "1")
|
||||
|
||||
|
||||
# Search for nested element in list
|
||||
def contains_nested(element, lst):
|
||||
for i, item in enumerate(lst):
|
||||
if element in item:
|
||||
return i
|
||||
return None
|
||||
|
||||
|
||||
# Get mapped value
|
||||
def search_mapping(dictionary: dict, key_value: str):
|
||||
if key_value in dictionary.keys():
|
||||
@@ -55,124 +63,12 @@ def search_mapping(dictionary: dict, key_value: str):
|
||||
return None
|
||||
|
||||
|
||||
def check_skip_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
):
|
||||
skip_reason = None
|
||||
|
||||
if library_type.lower() in blacklist_library_type:
|
||||
skip_reason = "is blacklist_library_type"
|
||||
|
||||
if library_title.lower() in [x.lower() for x in blacklist_library]:
|
||||
skip_reason = "is blacklist_library"
|
||||
|
||||
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)
|
||||
workers = min(32, os.cpu_count() * 2)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=workers) as executor:
|
||||
for arg in args:
|
||||
|
||||
537
src/jellyfin.py
537
src/jellyfin.py
@@ -1,13 +1,66 @@
|
||||
import asyncio, aiohttp
|
||||
from src.functions import (
|
||||
logger,
|
||||
search_mapping,
|
||||
import asyncio, aiohttp, traceback
|
||||
from math import floor
|
||||
|
||||
from src.functions import logger, search_mapping, contains_nested
|
||||
from src.library import (
|
||||
check_skip_logic,
|
||||
generate_library_guids_dict,
|
||||
)
|
||||
from src.watched import (
|
||||
combine_watched_dicts,
|
||||
)
|
||||
|
||||
|
||||
def get_movie_guids(movie):
|
||||
if "ProviderIds" in movie:
|
||||
logger(
|
||||
f"Jellyfin: {movie['Name']} {movie['ProviderIds']} {movie['MediaSources']}",
|
||||
3,
|
||||
)
|
||||
else:
|
||||
logger(
|
||||
f"Jellyfin: {movie['Name']} {movie['MediaSources']['Path']}",
|
||||
3,
|
||||
)
|
||||
|
||||
# Create a dictionary for the movie with its title
|
||||
movie_guids = {"title": movie["Name"]}
|
||||
|
||||
# If the movie has provider IDs, add them to the dictionary
|
||||
if "ProviderIds" in movie:
|
||||
movie_guids.update({k.lower(): v for k, v in movie["ProviderIds"].items()})
|
||||
|
||||
# If the movie has media sources, add them to the dictionary
|
||||
if "MediaSources" in movie:
|
||||
movie_guids["locations"] = tuple(
|
||||
[x["Path"].split("/")[-1] for x in movie["MediaSources"]]
|
||||
)
|
||||
|
||||
movie_guids["status"] = {
|
||||
"completed": movie["UserData"]["Played"],
|
||||
# Convert ticks to milliseconds to match Plex
|
||||
"time": floor(movie["UserData"]["PlaybackPositionTicks"] / 10000),
|
||||
}
|
||||
|
||||
return movie_guids
|
||||
|
||||
|
||||
def get_episode_guids(episode):
|
||||
# Create a dictionary for the episode with its provider IDs and media sources
|
||||
episode_dict = {k.lower(): v for k, v in episode["ProviderIds"].items()}
|
||||
episode_dict["title"] = episode["Name"]
|
||||
episode_dict["locations"] = tuple(
|
||||
[x["Path"].split("/")[-1] for x in episode["MediaSources"]]
|
||||
)
|
||||
|
||||
episode_dict["status"] = {
|
||||
"completed": episode["UserData"]["Played"],
|
||||
"time": floor(episode["UserData"]["PlaybackPositionTicks"] / 10000),
|
||||
}
|
||||
|
||||
return episode_dict
|
||||
|
||||
|
||||
class Jellyfin:
|
||||
def __init__(self, baseurl, token):
|
||||
self.baseurl = baseurl
|
||||
@@ -38,21 +91,33 @@ class Jellyfin:
|
||||
async with session.get(
|
||||
self.baseurl + query, headers=headers
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
raise Exception(
|
||||
f"Query failed with status {response.status} {response.reason}"
|
||||
)
|
||||
results = await response.json()
|
||||
|
||||
elif query_type == "post":
|
||||
async with session.post(
|
||||
self.baseurl + query, headers=headers
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
raise Exception(
|
||||
f"Query failed with status {response.status} {response.reason}"
|
||||
)
|
||||
results = await response.json()
|
||||
|
||||
if not isinstance(results, list) and not isinstance(results, dict):
|
||||
raise Exception("Query result is not of type list or dict")
|
||||
|
||||
# append identifiers to results
|
||||
if identifiers:
|
||||
results["Identifiers"] = identifiers
|
||||
|
||||
return results
|
||||
|
||||
except Exception as e:
|
||||
logger(f"Jellyfin: Query failed {e}", 2)
|
||||
logger(f"Jellyfin: Query {query_type} {query}\nResults {results}\n{e}", 2)
|
||||
raise Exception(e)
|
||||
|
||||
async def get_users(self):
|
||||
@@ -63,7 +128,7 @@ class Jellyfin:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
response = await self.query(query_string, "get", session)
|
||||
|
||||
# If reponse is not empty
|
||||
# If response is not empty
|
||||
if response:
|
||||
for user in response:
|
||||
users[user["Name"]] = user["Id"]
|
||||
@@ -73,7 +138,7 @@ class Jellyfin:
|
||||
logger(f"Jellyfin: Get users failed {e}", 2)
|
||||
raise Exception(e)
|
||||
|
||||
async def get_user_watched(
|
||||
async def get_user_library_watched(
|
||||
self, user_name, user_id, library_type, library_id, library_title
|
||||
):
|
||||
try:
|
||||
@@ -85,72 +150,117 @@ class Jellyfin:
|
||||
f"Jellyfin: Generating watched for {user_name} in library {library_title}",
|
||||
0,
|
||||
)
|
||||
# Movies
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
# Movies
|
||||
if library_type == "Movie":
|
||||
user_watched[user_name][library_title] = []
|
||||
watched = await self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&Fields=ItemCounts,ProviderIds,MediaSources",
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&IncludeItemTypes=Movie&Recursive=True&Fields=ItemCounts,ProviderIds,MediaSources",
|
||||
"get",
|
||||
session,
|
||||
)
|
||||
|
||||
in_progress = await self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsResumable&IncludeItemTypes=Movie&Recursive=True&Fields=ItemCounts,ProviderIds,MediaSources",
|
||||
"get",
|
||||
session,
|
||||
)
|
||||
|
||||
for movie in watched["Items"]:
|
||||
if movie["UserData"]["Played"] is True:
|
||||
movie_guids = {}
|
||||
movie_guids["title"] = movie["Name"]
|
||||
if "ProviderIds" in movie:
|
||||
# Lowercase movie["ProviderIds"] keys
|
||||
movie_guids = {
|
||||
k.lower(): v
|
||||
for k, v in movie["ProviderIds"].items()
|
||||
}
|
||||
if "MediaSources" in movie:
|
||||
movie_guids["locations"] = tuple(
|
||||
[
|
||||
x["Path"].split("/")[-1]
|
||||
for x in movie["MediaSources"]
|
||||
]
|
||||
)
|
||||
if "MediaSources" in movie and movie["MediaSources"] != {}:
|
||||
logger(
|
||||
f"Jellyfin: Adding {movie['Name']} to {user_name} watched list",
|
||||
3,
|
||||
)
|
||||
|
||||
# Get the movie's GUIDs
|
||||
movie_guids = get_movie_guids(movie)
|
||||
|
||||
# Append the movie dictionary to the list for the given user and library
|
||||
user_watched[user_name][library_title].append(movie_guids)
|
||||
logger(
|
||||
f"Jellyfin: Added {movie_guids} to {user_name} watched list",
|
||||
3,
|
||||
)
|
||||
|
||||
# Get all partially watched movies greater than 1 minute
|
||||
for movie in in_progress["Items"]:
|
||||
if "MediaSources" in movie and movie["MediaSources"] != {}:
|
||||
if movie["UserData"]["PlaybackPositionTicks"] < 600000000:
|
||||
continue
|
||||
|
||||
logger(
|
||||
f"Jellyfin: Adding {movie['Name']} to {user_name} watched list",
|
||||
3,
|
||||
)
|
||||
|
||||
# Get the movie's GUIDs
|
||||
movie_guids = get_movie_guids(movie)
|
||||
|
||||
# Append the movie dictionary to the list for the given user and library
|
||||
user_watched[user_name][library_title].append(movie_guids)
|
||||
logger(
|
||||
f"Jellyfin: Added {movie_guids} to {user_name} watched list",
|
||||
3,
|
||||
)
|
||||
|
||||
# TV Shows
|
||||
if library_type == "Series":
|
||||
if library_type in ["Series", "Episode"]:
|
||||
# Initialize an empty dictionary for the given user and library
|
||||
user_watched[user_name][library_title] = {}
|
||||
|
||||
# Retrieve a list of watched TV shows
|
||||
watched_shows = await self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&isPlaceHolder=false&Fields=ProviderIds,Path,RecursiveItemCount",
|
||||
+ f"?ParentId={library_id}&isPlaceHolder=false&IncludeItemTypes=Series&Recursive=True&Fields=ProviderIds,Path,RecursiveItemCount",
|
||||
"get",
|
||||
session,
|
||||
)
|
||||
|
||||
# Filter the list of shows to only include those that have been partially or fully watched
|
||||
watched_shows_filtered = []
|
||||
for show in watched_shows["Items"]:
|
||||
if "PlayedPercentage" in show["UserData"]:
|
||||
if show["UserData"]["PlayedPercentage"] > 0:
|
||||
watched_shows_filtered.append(show)
|
||||
|
||||
# Create a list of tasks to retrieve the seasons of each watched show
|
||||
seasons_tasks = []
|
||||
for show in watched_shows_filtered:
|
||||
logger(
|
||||
f"Jellyfin: Adding {show['Name']} to {user_name} watched list",
|
||||
3,
|
||||
)
|
||||
show_guids = {
|
||||
k.lower(): v for k, v in show["ProviderIds"].items()
|
||||
}
|
||||
show_guids["title"] = show["Name"]
|
||||
show_guids["locations"] = tuple([show["Path"].split("/")[-1]])
|
||||
show_guids = frozenset(show_guids.items())
|
||||
identifiers = {"show_guids": show_guids, "show_id": show["Id"]}
|
||||
task = asyncio.ensure_future(
|
||||
show_identifiers = {
|
||||
"show_guids": show_guids,
|
||||
"show_id": show["Id"],
|
||||
}
|
||||
|
||||
season_task = asyncio.ensure_future(
|
||||
self.query(
|
||||
f"/Shows/{show['Id']}/Seasons"
|
||||
+ f"?userId={user_id}&isPlaceHolder=false&Fields=ProviderIds,RecursiveItemCount",
|
||||
"get",
|
||||
session,
|
||||
frozenset(identifiers.items()),
|
||||
frozenset(show_identifiers.items()),
|
||||
)
|
||||
)
|
||||
seasons_tasks.append(task)
|
||||
seasons_tasks.append(season_task)
|
||||
|
||||
# Retrieve the seasons for each watched show
|
||||
seasons_watched = await asyncio.gather(*seasons_tasks)
|
||||
seasons_watched_filtered = []
|
||||
|
||||
# Filter the list of seasons to only include those that have been partially or fully watched
|
||||
seasons_watched_filtered = []
|
||||
for seasons in seasons_watched:
|
||||
seasons_watched_filtered_dict = {}
|
||||
seasons_watched_filtered_dict["Identifiers"] = seasons[
|
||||
@@ -169,6 +279,7 @@ class Jellyfin:
|
||||
seasons_watched_filtered_dict
|
||||
)
|
||||
|
||||
# Create a list of tasks to retrieve the episodes of each watched season
|
||||
episodes_tasks = []
|
||||
for seasons in seasons_watched_filtered:
|
||||
if len(seasons["Items"]) > 0:
|
||||
@@ -176,69 +287,87 @@ class Jellyfin:
|
||||
season_identifiers = dict(seasons["Identifiers"])
|
||||
season_identifiers["season_id"] = season["Id"]
|
||||
season_identifiers["season_name"] = season["Name"]
|
||||
task = asyncio.ensure_future(
|
||||
watched_task = asyncio.ensure_future(
|
||||
self.query(
|
||||
f"/Shows/{season_identifiers['show_id']}/Episodes"
|
||||
+ f"?seasonId={season['Id']}&userId={user_id}&isPlaceHolder=false&isPlayed=true&Fields=ProviderIds,MediaSources",
|
||||
+ f"?seasonId={season['Id']}&userId={user_id}&isPlaceHolder=false&Filters=IsPlayed&Fields=ProviderIds,MediaSources",
|
||||
"get",
|
||||
session,
|
||||
frozenset(season_identifiers.items()),
|
||||
)
|
||||
)
|
||||
episodes_tasks.append(task)
|
||||
in_progress_task = asyncio.ensure_future(
|
||||
self.query(
|
||||
f"/Shows/{season_identifiers['show_id']}/Episodes"
|
||||
+ f"?seasonId={season['Id']}&userId={user_id}&isPlaceHolder=false&Filters=IsResumable&Fields=ProviderIds,MediaSources",
|
||||
"get",
|
||||
session,
|
||||
frozenset(season_identifiers.items()),
|
||||
)
|
||||
)
|
||||
episodes_tasks.append(watched_task)
|
||||
episodes_tasks.append(in_progress_task)
|
||||
|
||||
# Retrieve the episodes for each watched season
|
||||
watched_episodes = await asyncio.gather(*episodes_tasks)
|
||||
for episodes in watched_episodes:
|
||||
if len(episodes["Items"]) > 0:
|
||||
for episode in episodes["Items"]:
|
||||
if episode["UserData"]["Played"] is True:
|
||||
if (
|
||||
"ProviderIds" in episode
|
||||
or "MediaSources" in episode
|
||||
):
|
||||
episode_identifiers = dict(
|
||||
episodes["Identifiers"]
|
||||
)
|
||||
show_guids = episode_identifiers["show_guids"]
|
||||
if (
|
||||
show_guids
|
||||
not in user_watched[user_name][
|
||||
library_title
|
||||
]
|
||||
):
|
||||
user_watched[user_name][library_title][
|
||||
show_guids
|
||||
] = {}
|
||||
if (
|
||||
episode_identifiers["season_name"]
|
||||
not in user_watched[user_name][
|
||||
library_title
|
||||
][show_guids]
|
||||
):
|
||||
user_watched[user_name][library_title][
|
||||
show_guids
|
||||
][episode_identifiers["season_name"]] = []
|
||||
|
||||
episode_guids = {}
|
||||
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
|
||||
)
|
||||
# Iterate through the watched episodes
|
||||
for episodes in watched_episodes:
|
||||
# If the season has any watched episodes
|
||||
if len(episodes["Items"]) > 0:
|
||||
# Create a dictionary for the season with its identifier and episodes
|
||||
season_dict = {}
|
||||
season_dict["Identifiers"] = dict(episodes["Identifiers"])
|
||||
season_dict["Episodes"] = []
|
||||
for episode in episodes["Items"]:
|
||||
if (
|
||||
"MediaSources" in episode
|
||||
and episode["MediaSources"] is not {}
|
||||
):
|
||||
# If watched or watched more than a minute
|
||||
if (
|
||||
episode["UserData"]["Played"] == True
|
||||
or episode["UserData"]["PlaybackPositionTicks"]
|
||||
> 600000000
|
||||
):
|
||||
episode_dict = get_episode_guids(episode)
|
||||
# Add the episode dictionary to the season's list of episodes
|
||||
season_dict["Episodes"].append(episode_dict)
|
||||
|
||||
# Add the season dictionary to the show's list of seasons
|
||||
if (
|
||||
season_dict["Identifiers"]["show_guids"]
|
||||
not in user_watched[user_name][library_title]
|
||||
):
|
||||
user_watched[user_name][library_title][
|
||||
season_dict["Identifiers"]["show_guids"]
|
||||
] = {}
|
||||
|
||||
if (
|
||||
season_dict["Identifiers"]["season_name"]
|
||||
not in user_watched[user_name][library_title][
|
||||
season_dict["Identifiers"]["show_guids"]
|
||||
]
|
||||
):
|
||||
user_watched[user_name][library_title][
|
||||
season_dict["Identifiers"]["show_guids"]
|
||||
][season_dict["Identifiers"]["season_name"]] = []
|
||||
|
||||
user_watched[user_name][library_title][
|
||||
season_dict["Identifiers"]["show_guids"]
|
||||
][season_dict["Identifiers"]["season_name"]] = season_dict[
|
||||
"Episodes"
|
||||
]
|
||||
logger(
|
||||
f"Jellyfin: Added {season_dict['Episodes']} to {user_name} {season_dict['Identifiers']['show_guids']} watched list",
|
||||
1,
|
||||
)
|
||||
|
||||
logger(
|
||||
f"Jellyfin: Got watched for {user_name} in library {library_title}", 1
|
||||
)
|
||||
if library_title in user_watched[user_name]:
|
||||
logger(f"Jellyfin: {user_watched[user_name][library_title]}", 3)
|
||||
|
||||
return user_watched
|
||||
except Exception as e:
|
||||
@@ -246,7 +375,9 @@ class Jellyfin:
|
||||
f"Jellyfin: Failed to get watched for {user_name} in library {library_title}, Error: {e}",
|
||||
2,
|
||||
)
|
||||
raise Exception(e)
|
||||
|
||||
logger(traceback.format_exc(), 2)
|
||||
return {}
|
||||
|
||||
async def get_users_watched(
|
||||
self,
|
||||
@@ -276,7 +407,7 @@ class Jellyfin:
|
||||
task = asyncio.ensure_future(
|
||||
self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&limit=1",
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&Recursive=True&excludeItemTypes=Folder&limit=100",
|
||||
"get",
|
||||
session,
|
||||
identifiers=identifiers,
|
||||
@@ -294,11 +425,18 @@ class Jellyfin:
|
||||
|
||||
library_id = watched["Identifiers"]["library_id"]
|
||||
library_title = watched["Identifiers"]["library_title"]
|
||||
library_type = watched["Items"][0]["Type"]
|
||||
# Get all library types excluding "Folder"
|
||||
types = set(
|
||||
[
|
||||
x["Type"]
|
||||
for x in watched["Items"]
|
||||
if x["Type"] in ["Movie", "Series", "Episode"]
|
||||
]
|
||||
)
|
||||
|
||||
skip_reason = check_skip_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
types,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
@@ -308,20 +446,35 @@ class Jellyfin:
|
||||
|
||||
if skip_reason:
|
||||
logger(
|
||||
f"Jellyfin: Skipping library {library_title} {skip_reason}",
|
||||
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
|
||||
# If there are multiple types in library raise error
|
||||
if types is None or len(types) < 1:
|
||||
all_types = set([x["Type"] for x in watched["Items"]])
|
||||
logger(
|
||||
f"Jellyfin: Skipping Library {library_title} found types: {types}, all types: {all_types}",
|
||||
1,
|
||||
)
|
||||
)
|
||||
tasks_watched.append(task)
|
||||
continue
|
||||
|
||||
for library_type in types:
|
||||
# Get watched for user
|
||||
task = asyncio.ensure_future(
|
||||
self.get_user_library_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)
|
||||
@@ -342,7 +495,7 @@ class Jellyfin:
|
||||
|
||||
for user_name, user_id in users.items():
|
||||
watched.append(
|
||||
await self.get_users_watched(
|
||||
self.get_users_watched(
|
||||
user_name,
|
||||
user_id,
|
||||
blacklist_library,
|
||||
@@ -353,9 +506,10 @@ class Jellyfin:
|
||||
)
|
||||
)
|
||||
|
||||
watched = await asyncio.gather(*watched, return_exceptions=True)
|
||||
for user_watched in watched:
|
||||
user_watched_temp = combine_watched_dicts(user_watched)
|
||||
for user, user_watched_temp in user_watched_temp.items():
|
||||
user_watched_combine = combine_watched_dicts(user_watched)
|
||||
for user, user_watched_temp in user_watched_combine.items():
|
||||
if user not in users_watched:
|
||||
users_watched[user] = {}
|
||||
users_watched[user].update(user_watched_temp)
|
||||
@@ -386,24 +540,36 @@ class Jellyfin:
|
||||
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",
|
||||
+ f"?SortBy=SortName&SortOrder=Ascending&Recursive=True&ParentId={library_id}"
|
||||
+ "&isPlayed=false&Fields=ItemCounts,ProviderIds,MediaSources&IncludeItemTypes=Movie",
|
||||
"get",
|
||||
session,
|
||||
)
|
||||
for jellyfin_video in jellyfin_search["Items"]:
|
||||
movie_found = False
|
||||
movie_status = None
|
||||
|
||||
if "MediaSources" in jellyfin_video:
|
||||
for movie_location in jellyfin_video["MediaSources"]:
|
||||
if (
|
||||
movie_location["Path"].split("/")[-1]
|
||||
in videos_movies_ids["locations"]
|
||||
contains_nested(
|
||||
movie_location["Path"].split("/")[-1],
|
||||
videos_movies_ids["locations"],
|
||||
)
|
||||
is not None
|
||||
):
|
||||
movie_found = True
|
||||
for video in videos:
|
||||
if (
|
||||
contains_nested(
|
||||
movie_location["Path"].split("/")[-1],
|
||||
video["locations"],
|
||||
)
|
||||
is not None
|
||||
):
|
||||
movie_status = video["status"]
|
||||
break
|
||||
break
|
||||
|
||||
if not movie_found:
|
||||
if not movie_status:
|
||||
for (
|
||||
movie_provider_source,
|
||||
movie_provider_id,
|
||||
@@ -415,21 +581,37 @@ class Jellyfin:
|
||||
movie_provider_source.lower()
|
||||
]
|
||||
):
|
||||
movie_found = True
|
||||
for video in videos:
|
||||
if (
|
||||
movie_provider_id.lower()
|
||||
in video[movie_provider_source.lower()]
|
||||
):
|
||||
movie_status = video["status"]
|
||||
break
|
||||
break
|
||||
|
||||
if movie_found:
|
||||
if movie_status:
|
||||
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,
|
||||
)
|
||||
if movie_status["completed"]:
|
||||
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"Dryrun {msg}", 0)
|
||||
# TODO add support for partially watched movies
|
||||
msg = f"{jellyfin_video['Name']} as partially watched for {floor(movie_status['time'] / 60_000)} minutes for {user_name} in {library} for Jellyfin"
|
||||
if not dryrun:
|
||||
pass
|
||||
# logger(f"Marked {msg}", 0)
|
||||
else:
|
||||
pass
|
||||
# logger(f"Dryrun {msg}", 0)
|
||||
else:
|
||||
logger(
|
||||
f"Jellyfin: Skipping movie {jellyfin_video['Name']} as it is not in mark list for {user_name}",
|
||||
@@ -440,8 +622,8 @@ class Jellyfin:
|
||||
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",
|
||||
+ f"?SortBy=SortName&SortOrder=Ascending&Recursive=True&ParentId={library_id}"
|
||||
+ "&isPlayed=false&Fields=ItemCounts,ProviderIds,Path&IncludeItemTypes=Series",
|
||||
"get",
|
||||
session,
|
||||
)
|
||||
@@ -452,10 +634,27 @@ class Jellyfin:
|
||||
|
||||
if "Path" in jellyfin_show:
|
||||
if (
|
||||
jellyfin_show["Path"].split("/")[-1]
|
||||
in videos_shows_ids["locations"]
|
||||
contains_nested(
|
||||
jellyfin_show["Path"].split("/")[-1],
|
||||
videos_shows_ids["locations"],
|
||||
)
|
||||
is not None
|
||||
):
|
||||
show_found = True
|
||||
episode_videos = []
|
||||
|
||||
for show, seasons in videos.items():
|
||||
show = {k: v for k, v in show}
|
||||
if (
|
||||
contains_nested(
|
||||
jellyfin_show["Path"].split("/")[-1],
|
||||
show["locations"],
|
||||
)
|
||||
is not None
|
||||
):
|
||||
for season in seasons.values():
|
||||
for episode in season:
|
||||
episode_videos.append(episode)
|
||||
|
||||
if not show_found:
|
||||
for show_provider_source, show_provider_id in jellyfin_show[
|
||||
@@ -469,7 +668,16 @@ class Jellyfin:
|
||||
]
|
||||
):
|
||||
show_found = True
|
||||
break
|
||||
episode_videos = []
|
||||
for show, seasons in videos.items():
|
||||
show = {k: v for k, v in show}
|
||||
if (
|
||||
show_provider_id.lower()
|
||||
in show[show_provider_source.lower()]
|
||||
):
|
||||
for season in seasons.values():
|
||||
for episode in season:
|
||||
episode_videos.append(episode)
|
||||
|
||||
if show_found:
|
||||
logger(
|
||||
@@ -485,20 +693,34 @@ class Jellyfin:
|
||||
)
|
||||
|
||||
for jellyfin_episode in jellyfin_episodes["Items"]:
|
||||
episode_found = False
|
||||
episode_status = None
|
||||
|
||||
if "MediaSources" in jellyfin_episode:
|
||||
for episode_location in jellyfin_episode[
|
||||
"MediaSources"
|
||||
]:
|
||||
if (
|
||||
episode_location["Path"].split("/")[-1]
|
||||
in videos_episodes_ids["locations"]
|
||||
contains_nested(
|
||||
episode_location["Path"].split("/")[-1],
|
||||
videos_episodes_ids["locations"],
|
||||
)
|
||||
is not None
|
||||
):
|
||||
episode_found = True
|
||||
for episode in episode_videos:
|
||||
if (
|
||||
contains_nested(
|
||||
episode_location["Path"].split(
|
||||
"/"
|
||||
)[-1],
|
||||
episode["locations"],
|
||||
)
|
||||
is not None
|
||||
):
|
||||
episode_status = episode["status"]
|
||||
break
|
||||
break
|
||||
|
||||
if not episode_found:
|
||||
if not episode_status:
|
||||
for (
|
||||
episode_provider_source,
|
||||
episode_provider_id,
|
||||
@@ -513,33 +735,57 @@ class Jellyfin:
|
||||
episode_provider_source.lower()
|
||||
]
|
||||
):
|
||||
episode_found = True
|
||||
for episode in episode_videos:
|
||||
if (
|
||||
episode_provider_id.lower()
|
||||
in episode[
|
||||
episode_provider_source.lower()
|
||||
]
|
||||
):
|
||||
episode_status = episode[
|
||||
"status"
|
||||
]
|
||||
break
|
||||
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,
|
||||
if episode_status:
|
||||
if episode_status["completed"]:
|
||||
jellyfin_episode_id = jellyfin_episode["Id"]
|
||||
msg = (
|
||||
f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['IndexNumber']} {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"Dryrun {msg}", 0)
|
||||
# TODO add support for partially watched episodes
|
||||
jellyfin_episode_id = jellyfin_episode["Id"]
|
||||
msg = (
|
||||
f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['IndexNumber']} {jellyfin_episode['Name']}"
|
||||
+ f" as partially watched for {floor(episode_status['time'] / 60_000)} minutes for {user_name} in {library} for Jellyfin"
|
||||
)
|
||||
if not dryrun:
|
||||
pass
|
||||
# logger(f"Marked {msg}", 0)
|
||||
else:
|
||||
pass
|
||||
# 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,
|
||||
3,
|
||||
)
|
||||
else:
|
||||
logger(
|
||||
f"Jellyfin: Skipping show {jellyfin_show['Name']} as it is not in mark list for {user_name}",
|
||||
1,
|
||||
3,
|
||||
)
|
||||
|
||||
if (
|
||||
@@ -557,6 +803,7 @@ class Jellyfin:
|
||||
f"Jellyfin: Error updating watched for {user_name} in library {library}, {e}",
|
||||
2,
|
||||
)
|
||||
logger(traceback.format_exc(), 2)
|
||||
raise Exception(e)
|
||||
|
||||
async def update_watched(
|
||||
@@ -618,13 +865,13 @@ class Jellyfin:
|
||||
else:
|
||||
logger(
|
||||
f"Jellyfin: Library {library} or {library_other} not found in library list",
|
||||
2,
|
||||
1,
|
||||
)
|
||||
continue
|
||||
else:
|
||||
logger(
|
||||
f"Jellyfin: Library {library} not found in library list",
|
||||
2,
|
||||
1,
|
||||
)
|
||||
continue
|
||||
|
||||
|
||||
262
src/library.py
Normal file
262
src/library.py
Normal file
@@ -0,0 +1,262 @@
|
||||
from src.functions import (
|
||||
logger,
|
||||
search_mapping,
|
||||
)
|
||||
|
||||
|
||||
def check_skip_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping=None,
|
||||
):
|
||||
skip_reason = None
|
||||
library_other = None
|
||||
if library_mapping:
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
|
||||
skip_reason_black = check_blacklist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
library_other,
|
||||
)
|
||||
skip_reason_white = check_whitelist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
# Combine skip reasons
|
||||
if skip_reason_black:
|
||||
skip_reason = skip_reason_black
|
||||
|
||||
if skip_reason_white:
|
||||
if skip_reason:
|
||||
skip_reason = skip_reason + " and " + skip_reason_white
|
||||
else:
|
||||
skip_reason = skip_reason_white
|
||||
|
||||
return skip_reason
|
||||
|
||||
|
||||
def check_blacklist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
library_other=None,
|
||||
):
|
||||
skip_reason = None
|
||||
if isinstance(library_type, (list, tuple, set)):
|
||||
for library_type_item in library_type:
|
||||
if library_type_item.lower() in blacklist_library_type:
|
||||
skip_reason = f"{library_type_item} is in blacklist_library_type"
|
||||
else:
|
||||
if library_type.lower() in blacklist_library_type:
|
||||
skip_reason = f"{library_type} is in blacklist_library_type"
|
||||
|
||||
if library_title.lower() in [x.lower() for x in blacklist_library]:
|
||||
if skip_reason:
|
||||
skip_reason = (
|
||||
skip_reason + " and " + f"{library_title} is in blacklist_library"
|
||||
)
|
||||
else:
|
||||
skip_reason = f"{library_title} is in blacklist_library"
|
||||
|
||||
if library_other:
|
||||
if library_other.lower() in [x.lower() for x in blacklist_library]:
|
||||
if skip_reason:
|
||||
skip_reason = (
|
||||
skip_reason + " and " + f"{library_other} is in blacklist_library"
|
||||
)
|
||||
else:
|
||||
skip_reason = f"{library_other} is in blacklist_library"
|
||||
|
||||
return skip_reason
|
||||
|
||||
|
||||
def check_whitelist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
library_other=None,
|
||||
):
|
||||
skip_reason = None
|
||||
if len(whitelist_library_type) > 0:
|
||||
if isinstance(library_type, (list, tuple, set)):
|
||||
for library_type_item in library_type:
|
||||
if library_type_item.lower() not in whitelist_library_type:
|
||||
skip_reason = (
|
||||
f"{library_type_item} is not in whitelist_library_type"
|
||||
)
|
||||
else:
|
||||
if library_type.lower() not in whitelist_library_type:
|
||||
skip_reason = f"{library_type} is not in whitelist_library_type"
|
||||
|
||||
# if whitelist is not empty and library is not in whitelist
|
||||
if len(whitelist_library) > 0:
|
||||
if library_other:
|
||||
if library_title.lower() not in [
|
||||
x.lower() for x in whitelist_library
|
||||
] and library_other.lower() not in [x.lower() for x in whitelist_library]:
|
||||
if skip_reason:
|
||||
skip_reason = (
|
||||
skip_reason
|
||||
+ " and "
|
||||
+ f"{library_title} is not in whitelist_library"
|
||||
)
|
||||
else:
|
||||
skip_reason = f"{library_title} is not in whitelist_library"
|
||||
else:
|
||||
if library_title.lower() not in [x.lower() for x in whitelist_library]:
|
||||
if skip_reason:
|
||||
skip_reason = (
|
||||
skip_reason
|
||||
+ " and "
|
||||
+ f"{library_title} is not in whitelist_library"
|
||||
)
|
||||
else:
|
||||
skip_reason = f"{library_title} is not in whitelist_library"
|
||||
|
||||
return skip_reason
|
||||
|
||||
|
||||
def show_title_dict(user_list: dict):
|
||||
try:
|
||||
show_output_dict = {}
|
||||
show_output_dict["locations"] = []
|
||||
show_counter = 0 # Initialize a counter for the current show position
|
||||
|
||||
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()] = [None] * show_counter
|
||||
if provider_key.lower() == "locations":
|
||||
show_output_dict[provider_key.lower()].append(provider_value)
|
||||
else:
|
||||
show_output_dict[provider_key.lower()].append(
|
||||
provider_value.lower()
|
||||
)
|
||||
|
||||
show_counter += 1
|
||||
for key in show_output_dict:
|
||||
if len(show_output_dict[key]) < show_counter:
|
||||
show_output_dict[key].append(None)
|
||||
|
||||
return show_output_dict
|
||||
except Exception:
|
||||
logger("Generating show_output_dict failed, skipping", 1)
|
||||
return {}
|
||||
|
||||
|
||||
def episode_title_dict(user_list: dict):
|
||||
try:
|
||||
episode_output_dict = {}
|
||||
episode_output_dict["completed"] = []
|
||||
episode_output_dict["time"] = []
|
||||
episode_output_dict["locations"] = []
|
||||
episode_counter = 0 # Initialize a counter for the current episode position
|
||||
|
||||
# Iterate through the shows, seasons, and episodes in user_list
|
||||
for show in user_list:
|
||||
for season in user_list[show]:
|
||||
for episode in user_list[show][season]:
|
||||
# Iterate through the keys and values in each episode
|
||||
for episode_key, episode_value in episode.items():
|
||||
# If the key is not "status", add the key to episode_output_dict if it doesn't exist
|
||||
if episode_key != "status":
|
||||
if episode_key.lower() not in episode_output_dict:
|
||||
# Initialize the list with None values up to the current episode position
|
||||
episode_output_dict[episode_key.lower()] = [
|
||||
None
|
||||
] * episode_counter
|
||||
|
||||
# If the key is "locations", append each location to the list
|
||||
if episode_key == "locations":
|
||||
episode_output_dict[episode_key.lower()].append(
|
||||
episode_value
|
||||
)
|
||||
|
||||
# If the key is "status", append the "completed" and "time" values
|
||||
elif episode_key == "status":
|
||||
episode_output_dict["completed"].append(
|
||||
episode_value["completed"]
|
||||
)
|
||||
episode_output_dict["time"].append(episode_value["time"])
|
||||
|
||||
# For other keys, append the value to the list
|
||||
else:
|
||||
episode_output_dict[episode_key.lower()].append(
|
||||
episode_value.lower()
|
||||
)
|
||||
|
||||
# Increment the episode_counter
|
||||
episode_counter += 1
|
||||
|
||||
# Extend the lists in episode_output_dict with None values to match the current episode_counter
|
||||
for key in episode_output_dict:
|
||||
if len(episode_output_dict[key]) < episode_counter:
|
||||
episode_output_dict[key].append(None)
|
||||
|
||||
return episode_output_dict
|
||||
except Exception:
|
||||
logger("Generating episode_output_dict failed, skipping", 1)
|
||||
return {}
|
||||
|
||||
|
||||
def movies_title_dict(user_list: dict):
|
||||
try:
|
||||
movies_output_dict = {}
|
||||
movies_output_dict["completed"] = []
|
||||
movies_output_dict["time"] = []
|
||||
movies_output_dict["locations"] = []
|
||||
movie_counter = 0 # Initialize a counter for the current movie position
|
||||
|
||||
for movie in user_list:
|
||||
for movie_key, movie_value in movie.items():
|
||||
if movie_key != "status":
|
||||
if movie_key.lower() not in movies_output_dict:
|
||||
movies_output_dict[movie_key.lower()] = []
|
||||
|
||||
if movie_key == "locations":
|
||||
movies_output_dict[movie_key.lower()].append(movie_value)
|
||||
elif movie_key == "status":
|
||||
movies_output_dict["completed"].append(movie_value["completed"])
|
||||
movies_output_dict["time"].append(movie_value["time"])
|
||||
else:
|
||||
movies_output_dict[movie_key.lower()].append(movie_value.lower())
|
||||
|
||||
movie_counter += 1
|
||||
for key in movies_output_dict:
|
||||
if len(movies_output_dict[key]) < movie_counter:
|
||||
movies_output_dict[key].append(None)
|
||||
|
||||
return movies_output_dict
|
||||
except Exception:
|
||||
logger("Generating movies_output_dict failed, skipping", 1)
|
||||
return {}
|
||||
|
||||
|
||||
def generate_library_guids_dict(user_list: dict):
|
||||
# Handle the case where user_list is empty or does not contain the expected keys and values
|
||||
if not user_list:
|
||||
return {}, {}, {}
|
||||
|
||||
show_output_dict = show_title_dict(user_list)
|
||||
episode_output_dict = episode_title_dict(user_list)
|
||||
movies_output_dict = movies_title_dict(user_list)
|
||||
|
||||
return show_output_dict, episode_output_dict, movies_output_dict
|
||||
574
src/main.py
574
src/main.py
@@ -1,423 +1,52 @@
|
||||
import copy, os, traceback, json, asyncio
|
||||
import 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.users import (
|
||||
generate_user_list,
|
||||
combine_user_lists,
|
||||
filter_user_lists,
|
||||
generate_server_users,
|
||||
)
|
||||
from src.watched import (
|
||||
cleanup_watched,
|
||||
)
|
||||
from src.black_white import setup_black_white_lists
|
||||
|
||||
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
|
||||
):
|
||||
server_1_users = generate_user_list(server_1)
|
||||
server_2_users = generate_user_list(server_2)
|
||||
|
||||
# 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
|
||||
|
||||
users = combine_user_lists(server_1_users, server_2_users, user_mapping)
|
||||
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]
|
||||
|
||||
users_filtered = filter_user_lists(users, blacklist_users, whitelist_users)
|
||||
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
|
||||
output_server_1_users = generate_server_users(server_1, users_filtered)
|
||||
output_server_2_users = generate_server_users(server_2, users_filtered)
|
||||
|
||||
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:
|
||||
# Check if users is none or empty
|
||||
if output_server_1_users is None or len(output_server_1_users) == 0:
|
||||
raise Exception(
|
||||
f"No users found for server 1, users found {users} filtered users {users_filtered}"
|
||||
f"No users found for server 1 {server_1[0]}, users found {users}, filtered users {users_filtered}, server 1 users {server_1[1].users}"
|
||||
)
|
||||
|
||||
if len(output_server_2_users) == 0:
|
||||
if output_server_2_users is None or len(output_server_2_users) == 0:
|
||||
raise Exception(
|
||||
f"No users found for server 2, users found {users} filtered users {users_filtered}"
|
||||
f"No users found for server 2 {server_2[0]}, users found {users} filtered users {users_filtered}, server 2 users {server_2[1].users}"
|
||||
)
|
||||
|
||||
logger(f"Server 1 users: {output_server_1_users}", 1)
|
||||
@@ -500,16 +129,116 @@ def generate_server_connections():
|
||||
)
|
||||
|
||||
for i, baseurl in enumerate(jellyfin_baseurl):
|
||||
baseurl = baseurl.strip()
|
||||
if baseurl[-1] == "/":
|
||||
baseurl = baseurl[:-1]
|
||||
servers.append(
|
||||
(
|
||||
"jellyfin",
|
||||
Jellyfin(baseurl=baseurl.strip(), token=jellyfin_token[i].strip()),
|
||||
Jellyfin(baseurl=baseurl, token=jellyfin_token[i].strip()),
|
||||
)
|
||||
)
|
||||
|
||||
return servers
|
||||
|
||||
|
||||
def get_server_watched(
|
||||
server_connection: list,
|
||||
users: dict,
|
||||
blacklist_library: list,
|
||||
whitelist_library: list,
|
||||
blacklist_library_type: list,
|
||||
whitelist_library_type: list,
|
||||
library_mapping: dict,
|
||||
):
|
||||
if server_connection[0] == "plex":
|
||||
return server_connection[1].get_watched(
|
||||
users,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
elif server_connection[0] == "jellyfin":
|
||||
return asyncio.run(
|
||||
server_connection[1].get_watched(
|
||||
users,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def update_server_watched(
|
||||
server_connection: list,
|
||||
server_watched_filtered: dict,
|
||||
user_mapping: dict,
|
||||
library_mapping: dict,
|
||||
dryrun: bool,
|
||||
):
|
||||
if server_connection[0] == "plex":
|
||||
server_connection[1].update_watched(
|
||||
server_watched_filtered, user_mapping, library_mapping, dryrun
|
||||
)
|
||||
elif server_connection[0] == "jellyfin":
|
||||
asyncio.run(
|
||||
server_connection[1].update_watched(
|
||||
server_watched_filtered, user_mapping, library_mapping, dryrun
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def should_sync_server(server_1_type, server_2_type):
|
||||
sync_from_plex_to_jellyfin = str_to_bool(
|
||||
os.getenv("SYNC_FROM_PLEX_TO_JELLYFIN", "True")
|
||||
)
|
||||
sync_from_jelly_to_plex = str_to_bool(
|
||||
os.getenv("SYNC_FROM_JELLYFIN_TO_PLEX", "True")
|
||||
)
|
||||
sync_from_plex_to_plex = str_to_bool(os.getenv("SYNC_FROM_PLEX_TO_PLEX", "True"))
|
||||
sync_from_jelly_to_jellyfin = str_to_bool(
|
||||
os.getenv("SYNC_FROM_JELLYFIN_TO_JELLYFIN", "True")
|
||||
)
|
||||
|
||||
if (
|
||||
server_1_type == "plex"
|
||||
and server_2_type == "plex"
|
||||
and not sync_from_plex_to_plex
|
||||
):
|
||||
logger("Sync between plex and plex is disabled", 1)
|
||||
return False
|
||||
|
||||
if (
|
||||
server_1_type == "plex"
|
||||
and server_2_type == "jellyfin"
|
||||
and not sync_from_jelly_to_plex
|
||||
):
|
||||
logger("Sync from jellyfin to plex disabled", 1)
|
||||
return False
|
||||
|
||||
if (
|
||||
server_1_type == "jellyfin"
|
||||
and server_2_type == "jellyfin"
|
||||
and not sync_from_jelly_to_jellyfin
|
||||
):
|
||||
logger("Sync between jellyfin and jellyfin is disabled", 1)
|
||||
return False
|
||||
|
||||
if (
|
||||
server_1_type == "jellyfin"
|
||||
and server_2_type == "plex"
|
||||
and not sync_from_plex_to_jellyfin
|
||||
):
|
||||
logger("Sync from plex to jellyfin is disabled", 1)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def main_loop():
|
||||
logfile = os.getenv("LOGFILE", "log.log")
|
||||
# Delete logfile if it exists
|
||||
@@ -567,10 +296,6 @@ def main_loop():
|
||||
|
||||
# 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(
|
||||
@@ -578,7 +303,8 @@ def main_loop():
|
||||
)
|
||||
|
||||
logger("Creating watched lists", 1)
|
||||
server_1_watched = server_1_connection.get_watched(
|
||||
server_1_watched = get_server_watched(
|
||||
server_1,
|
||||
server_1_users,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
@@ -587,15 +313,14 @@ def main_loop():
|
||||
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,
|
||||
)
|
||||
server_2_watched = get_server_watched(
|
||||
server_2,
|
||||
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)
|
||||
@@ -620,17 +345,27 @@ def main_loop():
|
||||
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
|
||||
if should_sync_server(server_1[0], server_2[0]):
|
||||
update_server_watched(
|
||||
server_1,
|
||||
server_2_watched_filtered,
|
||||
user_mapping,
|
||||
library_mapping,
|
||||
dryrun,
|
||||
)
|
||||
|
||||
if should_sync_server(server_2[0], server_1[0]):
|
||||
update_server_watched(
|
||||
server_2,
|
||||
server_1_watched_filtered,
|
||||
user_mapping,
|
||||
library_mapping,
|
||||
dryrun,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
run_only_once = str_to_bool(os.getenv("RUN_ONLY_ONCE", "False"))
|
||||
sleep_duration = float(os.getenv("SLEEP_DURATION", "3600"))
|
||||
times = []
|
||||
while True:
|
||||
@@ -643,6 +378,9 @@ def main():
|
||||
if len(times) > 0:
|
||||
logger(f"Average time: {sum(times) / len(times)}", 0)
|
||||
|
||||
if run_only_once:
|
||||
break
|
||||
|
||||
logger(f"Looping in {sleep_duration}")
|
||||
sleep(sleep_duration)
|
||||
|
||||
@@ -654,6 +392,10 @@ def main():
|
||||
logger(error, log_type=2)
|
||||
|
||||
logger(traceback.format_exc(), 2)
|
||||
|
||||
if run_only_once:
|
||||
break
|
||||
|
||||
logger(f"Retrying in {sleep_duration}", log_type=0)
|
||||
sleep(sleep_duration)
|
||||
|
||||
|
||||
487
src/plex.py
487
src/plex.py
@@ -1,5 +1,6 @@
|
||||
import re, requests
|
||||
import re, requests, os, traceback
|
||||
from urllib3.poolmanager import PoolManager
|
||||
from math import floor
|
||||
|
||||
from plexapi.server import PlexServer
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
@@ -7,21 +8,123 @@ from plexapi.myplex import MyPlexAccount
|
||||
from src.functions import (
|
||||
logger,
|
||||
search_mapping,
|
||||
future_thread_executor,
|
||||
contains_nested,
|
||||
)
|
||||
from src.library import (
|
||||
check_skip_logic,
|
||||
generate_library_guids_dict,
|
||||
future_thread_executor,
|
||||
)
|
||||
|
||||
|
||||
# Bypass hostname validation for ssl. Taken from https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186
|
||||
class HostNameIgnoringAdapter(requests.adapters.HTTPAdapter):
|
||||
def init_poolmanager(self, connections, maxsize, block=..., **pool_kwargs):
|
||||
self.poolmanager = PoolManager(num_pools=connections,
|
||||
maxsize=maxsize,
|
||||
block=block,
|
||||
assert_hostname=False,
|
||||
**pool_kwargs)
|
||||
self.poolmanager = PoolManager(
|
||||
num_pools=connections,
|
||||
maxsize=maxsize,
|
||||
block=block,
|
||||
assert_hostname=False,
|
||||
**pool_kwargs,
|
||||
)
|
||||
|
||||
def get_user_watched(user, user_plex, library):
|
||||
|
||||
def get_movie_guids(video, completed=True):
|
||||
logger(f"Plex: {video.title} {video.guids} {video.locations}", 3)
|
||||
|
||||
movie_guids = {}
|
||||
try:
|
||||
for guid in video.guids:
|
||||
# Extract source and id from guid.id
|
||||
m = re.match(r"(.*)://(.*)", guid.id)
|
||||
guid_source, guid_id = m.group(1).lower(), m.group(2)
|
||||
movie_guids[guid_source] = guid_id
|
||||
except Exception:
|
||||
logger(f"Plex: Failed to get guids for {video.title}, Using location only", 1)
|
||||
|
||||
movie_guids["title"] = video.title
|
||||
movie_guids["locations"] = tuple([x.split("/")[-1] for x in video.locations])
|
||||
|
||||
movie_guids["status"] = {
|
||||
"completed": completed,
|
||||
"time": video.viewOffset,
|
||||
}
|
||||
|
||||
return movie_guids
|
||||
|
||||
|
||||
def get_episode_guids(episode, show, completed=True):
|
||||
episode_guids_temp = {}
|
||||
try:
|
||||
for guid in episode.guids:
|
||||
# Extract after :// from guid.id
|
||||
m = re.match(r"(.*)://(.*)", guid.id)
|
||||
guid_source, guid_id = m.group(1).lower(), m.group(2)
|
||||
episode_guids_temp[guid_source] = guid_id
|
||||
except Exception:
|
||||
logger(
|
||||
f"Plex: Failed to get guids for {episode.title} in {show.title}, Using location only",
|
||||
1,
|
||||
)
|
||||
|
||||
episode_guids_temp["title"] = episode.title
|
||||
episode_guids_temp["locations"] = tuple(
|
||||
[x.split("/")[-1] for x in episode.locations]
|
||||
)
|
||||
|
||||
episode_guids_temp["status"] = {
|
||||
"completed": completed,
|
||||
"time": episode.viewOffset,
|
||||
}
|
||||
|
||||
return episode_guids_temp
|
||||
|
||||
|
||||
def get_user_library_watched_show(show):
|
||||
try:
|
||||
show_guids = {}
|
||||
try:
|
||||
for show_guid in show.guids:
|
||||
# Extract source and id from guid.id
|
||||
m = re.match(r"(.*)://(.*)", show_guid.id)
|
||||
show_guid_source, show_guid_id = m.group(1).lower(), m.group(2)
|
||||
show_guids[show_guid_source] = show_guid_id
|
||||
except Exception:
|
||||
logger(
|
||||
f"Plex: Failed to get guids for {show.title}, Using location only", 1
|
||||
)
|
||||
|
||||
show_guids["title"] = show.title
|
||||
show_guids["locations"] = tuple([x.split("/")[-1] for x in show.locations])
|
||||
show_guids = frozenset(show_guids.items())
|
||||
|
||||
# Get all watched episodes for show
|
||||
episode_guids = {}
|
||||
watched = show.watched()
|
||||
|
||||
for episode in show.episodes():
|
||||
if episode in watched:
|
||||
if episode.parentTitle not in episode_guids:
|
||||
episode_guids[episode.parentTitle] = []
|
||||
|
||||
episode_guids[episode.parentTitle].append(
|
||||
get_episode_guids(episode, show, completed=True)
|
||||
)
|
||||
elif episode.viewOffset > 0:
|
||||
if episode.parentTitle not in episode_guids:
|
||||
episode_guids[episode.parentTitle] = []
|
||||
|
||||
episode_guids[episode.parentTitle].append(
|
||||
get_episode_guids(episode, show, completed=False)
|
||||
)
|
||||
|
||||
return show_guids, episode_guids
|
||||
|
||||
except Exception:
|
||||
return {}, {}
|
||||
|
||||
|
||||
def get_user_library_watched(user, user_plex, library):
|
||||
try:
|
||||
user_name = user.title.lower()
|
||||
user_watched = {}
|
||||
@@ -32,76 +135,61 @@ def get_user_watched(user, user_plex, library):
|
||||
0,
|
||||
)
|
||||
|
||||
library_videos = user_plex.library.section(library.title)
|
||||
|
||||
if library.type == "movie":
|
||||
user_watched[user_name][library.title] = []
|
||||
|
||||
library_videos = user_plex.library.section(library.title)
|
||||
# Get all watched movies
|
||||
for video in library_videos.search(unwatched=False):
|
||||
movie_guids = {}
|
||||
for guid in video.guids:
|
||||
guid_source = re.search(r"(.*)://", guid.id).group(1).lower()
|
||||
guid_id = re.search(r"://(.*)", guid.id).group(1)
|
||||
movie_guids[guid_source] = guid_id
|
||||
logger(f"Plex: Adding {video.title} to {user_name} watched list", 3)
|
||||
|
||||
movie_guids["title"] = video.title
|
||||
movie_guids["locations"] = tuple(
|
||||
[x.split("/")[-1] for x in video.locations]
|
||||
)
|
||||
movie_guids = get_movie_guids(video, completed=True)
|
||||
|
||||
user_watched[user_name][library.title].append(movie_guids)
|
||||
|
||||
# Get all partially watched movies greater than 1 minute
|
||||
for video in library_videos.search(inProgress=True):
|
||||
if video.viewOffset < 60000:
|
||||
continue
|
||||
|
||||
logger(f"Plex: Adding {video.title} to {user_name} watched list", 3)
|
||||
|
||||
movie_guids = get_movie_guids(video, completed=False)
|
||||
|
||||
user_watched[user_name][library.title].append(movie_guids)
|
||||
|
||||
elif library.type == "show":
|
||||
user_watched[user_name][library.title] = {}
|
||||
|
||||
library_videos = user_plex.library.section(library.title)
|
||||
# Parallelize show processing
|
||||
args = []
|
||||
|
||||
# Get all watched shows
|
||||
for show in library_videos.search(unwatched=False):
|
||||
show_guids = {}
|
||||
for show_guid in show.guids:
|
||||
# Extract after :// from guid.id
|
||||
show_guid_source = (
|
||||
re.search(r"(.*)://", show_guid.id).group(1).lower()
|
||||
args.append([get_user_library_watched_show, show])
|
||||
|
||||
# Get all partially watched shows
|
||||
for show in library_videos.search(inProgress=True):
|
||||
args.append([get_user_library_watched_show, show])
|
||||
|
||||
for show_guids, episode_guids in future_thread_executor(
|
||||
args, workers=min(os.cpu_count(), 4)
|
||||
):
|
||||
if show_guids and episode_guids:
|
||||
# append show, season, episode
|
||||
if show_guids not in user_watched[user_name][library.title]:
|
||||
user_watched[user_name][library.title][show_guids] = {}
|
||||
|
||||
user_watched[user_name][library.title][show_guids] = episode_guids
|
||||
logger(
|
||||
f"Plex: Added {episode_guids} to {user_name} {show_guids} watched list",
|
||||
3,
|
||||
)
|
||||
show_guid_id = re.search(r"://(.*)", show_guid.id).group(1)
|
||||
show_guids[show_guid_source] = show_guid_id
|
||||
|
||||
show_guids["title"] = show.title
|
||||
show_guids["locations"] = tuple(
|
||||
[x.split("/")[-1] for x in show.locations]
|
||||
)
|
||||
show_guids = frozenset(show_guids.items())
|
||||
|
||||
for season in show.seasons():
|
||||
episode_guids = []
|
||||
for episode in season.episodes():
|
||||
if episode.viewCount > 0:
|
||||
episode_guids_temp = {}
|
||||
for guid in episode.guids:
|
||||
# Extract after :// from guid.id
|
||||
guid_source = (
|
||||
re.search(r"(.*)://", guid.id).group(1).lower()
|
||||
)
|
||||
guid_id = re.search(r"://(.*)", guid.id).group(1)
|
||||
episode_guids_temp[guid_source] = guid_id
|
||||
|
||||
episode_guids_temp["locations"] = tuple(
|
||||
[x.split("/")[-1] for x in episode.locations]
|
||||
)
|
||||
episode_guids.append(episode_guids_temp)
|
||||
|
||||
if episode_guids:
|
||||
# append show, season, episode
|
||||
if show_guids not in user_watched[user_name][library.title]:
|
||||
user_watched[user_name][library.title][show_guids] = {}
|
||||
if (
|
||||
season.title
|
||||
not in user_watched[user_name][library.title][show_guids]
|
||||
):
|
||||
user_watched[user_name][library.title][show_guids][
|
||||
season.title
|
||||
] = {}
|
||||
user_watched[user_name][library.title][show_guids][
|
||||
season.title
|
||||
] = episode_guids
|
||||
logger(f"Plex: Got watched for {user_name} in library {library.title}", 1)
|
||||
if library.title in user_watched[user_name]:
|
||||
logger(f"Plex: {user_watched[user_name][library.title]}", 3)
|
||||
|
||||
return user_watched
|
||||
except Exception as e:
|
||||
@@ -109,7 +197,83 @@ def get_user_watched(user, user_plex, library):
|
||||
f"Plex: Failed to get watched for {user_name} in library {library.title}, Error: {e}",
|
||||
2,
|
||||
)
|
||||
raise Exception(e)
|
||||
return {}
|
||||
|
||||
|
||||
def find_video(plex_search, video_ids, videos=None):
|
||||
try:
|
||||
for location in plex_search.locations:
|
||||
if (
|
||||
contains_nested(location.split("/")[-1], video_ids["locations"])
|
||||
is not None
|
||||
):
|
||||
episode_videos = []
|
||||
if videos:
|
||||
for show, seasons in videos.items():
|
||||
show = {k: v for k, v in show}
|
||||
if (
|
||||
contains_nested(location.split("/")[-1], show["locations"])
|
||||
is not None
|
||||
):
|
||||
for season in seasons.values():
|
||||
for episode in season:
|
||||
episode_videos.append(episode)
|
||||
|
||||
return True, episode_videos
|
||||
|
||||
for guid in plex_search.guids:
|
||||
guid_source = re.search(r"(.*)://", guid.id).group(1).lower()
|
||||
guid_id = re.search(r"://(.*)", guid.id).group(1)
|
||||
|
||||
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
|
||||
if guid_source in video_ids.keys():
|
||||
if guid_id in video_ids[guid_source]:
|
||||
episode_videos = []
|
||||
if videos:
|
||||
for show, seasons in videos.items():
|
||||
show = {k: v for k, v in show}
|
||||
if guid_source in show["ids"].keys():
|
||||
if guid_id in show["ids"][guid_source]:
|
||||
for season in seasons:
|
||||
for episode in season:
|
||||
episode_videos.append(episode)
|
||||
|
||||
return True, episode_videos
|
||||
|
||||
return False, []
|
||||
except Exception:
|
||||
return False, []
|
||||
|
||||
|
||||
def get_video_status(plex_search, video_ids, videos):
|
||||
try:
|
||||
for location in plex_search.locations:
|
||||
if (
|
||||
contains_nested(location.split("/")[-1], video_ids["locations"])
|
||||
is not None
|
||||
):
|
||||
for video in videos:
|
||||
if (
|
||||
contains_nested(location.split("/")[-1], video["locations"])
|
||||
is not None
|
||||
):
|
||||
return video["status"]
|
||||
|
||||
for guid in plex_search.guids:
|
||||
guid_source = re.search(r"(.*)://", guid.id).group(1).lower()
|
||||
guid_id = re.search(r"://(.*)", guid.id).group(1)
|
||||
|
||||
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
|
||||
if guid_source in video_ids.keys():
|
||||
if guid_id in video_ids[guid_source]:
|
||||
for video in videos:
|
||||
if guid_source in video["ids"].keys():
|
||||
if guid_id in video["ids"][guid_source]:
|
||||
return video["status"]
|
||||
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def update_user_watched(user, user_plex, library, videos, dryrun):
|
||||
@@ -128,32 +292,24 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
|
||||
library_videos = user_plex.library.section(library)
|
||||
if videos_movies_ids:
|
||||
for movies_search in library_videos.search(unwatched=True):
|
||||
movie_found = False
|
||||
for movie_location in movies_search.locations:
|
||||
if movie_location.split("/")[-1] in videos_movies_ids["locations"]:
|
||||
movie_found = True
|
||||
break
|
||||
|
||||
if not movie_found:
|
||||
for movie_guid in movies_search.guids:
|
||||
movie_guid_source = (
|
||||
re.search(r"(.*)://", movie_guid.id).group(1).lower()
|
||||
)
|
||||
movie_guid_id = re.search(r"://(.*)", movie_guid.id).group(1)
|
||||
|
||||
# If movie provider source and movie provider id are in videos_movie_ids exactly, then the movie is in the list
|
||||
if movie_guid_source in videos_movies_ids.keys():
|
||||
if movie_guid_id in videos_movies_ids[movie_guid_source]:
|
||||
movie_found = True
|
||||
break
|
||||
|
||||
if movie_found:
|
||||
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
movies_search.markWatched()
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
video_status = get_video_status(
|
||||
movies_search, videos_movies_ids, videos
|
||||
)
|
||||
if video_status:
|
||||
if video_status["completed"]:
|
||||
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
movies_search.markWatched()
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
elif video_status["time"] > 60_000:
|
||||
msg = f"{movies_search.title} as partially watched for {floor(video_status['time'] / 60_000)} minutes for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
movies_search.updateProgress(video_status["time"])
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
else:
|
||||
logger(
|
||||
f"Plex: Skipping movie {movies_search.title} as it is not in mark list for {user.title}",
|
||||
@@ -162,73 +318,38 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
|
||||
|
||||
if videos_shows_ids and videos_episodes_ids:
|
||||
for show_search in library_videos.search(unwatched=True):
|
||||
show_found = False
|
||||
for show_location in show_search.locations:
|
||||
if show_location.split("/")[-1] in videos_shows_ids["locations"]:
|
||||
show_found = True
|
||||
break
|
||||
|
||||
if not show_found:
|
||||
for show_guid in show_search.guids:
|
||||
show_guid_source = (
|
||||
re.search(r"(.*)://", show_guid.id).group(1).lower()
|
||||
)
|
||||
show_guid_id = re.search(r"://(.*)", show_guid.id).group(1)
|
||||
|
||||
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
|
||||
if show_guid_source in videos_shows_ids.keys():
|
||||
if show_guid_id in videos_shows_ids[show_guid_source]:
|
||||
show_found = True
|
||||
break
|
||||
|
||||
show_found, episode_videos = find_video(
|
||||
show_search, videos_shows_ids, videos
|
||||
)
|
||||
if show_found:
|
||||
for episode_search in show_search.episodes():
|
||||
episode_found = False
|
||||
|
||||
for episode_location in episode_search.locations:
|
||||
if (
|
||||
episode_location.split("/")[-1]
|
||||
in videos_episodes_ids["locations"]
|
||||
):
|
||||
episode_found = True
|
||||
break
|
||||
|
||||
if not episode_found:
|
||||
for episode_guid in episode_search.guids:
|
||||
episode_guid_source = (
|
||||
re.search(r"(.*)://", episode_guid.id)
|
||||
.group(1)
|
||||
.lower()
|
||||
)
|
||||
episode_guid_id = re.search(
|
||||
r"://(.*)", episode_guid.id
|
||||
).group(1)
|
||||
|
||||
# If episode provider source and episode provider id are in videos_episodes_ids exactly, then the episode is in the list
|
||||
if episode_guid_source in videos_episodes_ids.keys():
|
||||
if (
|
||||
episode_guid_id
|
||||
in videos_episodes_ids[episode_guid_source]
|
||||
):
|
||||
episode_found = True
|
||||
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()
|
||||
video_status = get_video_status(
|
||||
episode_search, videos_episodes_ids, episode_videos
|
||||
)
|
||||
if video_status:
|
||||
if video_status["completed"]:
|
||||
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"Dryrun {msg}", 0)
|
||||
msg = f"{show_search.title} {episode_search.title} as partially watched for {floor(video_status['time'] / 60_000)} minutes for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
episode_search.updateProgress(video_status["time"])
|
||||
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,
|
||||
3,
|
||||
)
|
||||
else:
|
||||
logger(
|
||||
f"Plex: Skipping show {show_search.title} as it is not in mark list for {user.title}",
|
||||
1,
|
||||
3,
|
||||
)
|
||||
|
||||
if not videos_movies_ids and not videos_shows_ids and not videos_episodes_ids:
|
||||
@@ -242,7 +363,7 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
|
||||
f"Plex: Failed to update watched for {user.title} in library {library}, Error: {e}",
|
||||
2,
|
||||
)
|
||||
raise Exception(e)
|
||||
logger(traceback.format_exc(), 2)
|
||||
|
||||
|
||||
# class plex accept base url and token and username and password but default with none
|
||||
@@ -255,6 +376,7 @@ class Plex:
|
||||
password=None,
|
||||
servername=None,
|
||||
ssl_bypass=False,
|
||||
session=None,
|
||||
):
|
||||
self.baseurl = baseurl
|
||||
self.token = token
|
||||
@@ -262,21 +384,20 @@ class Plex:
|
||||
self.password = password
|
||||
self.servername = servername
|
||||
self.ssl_bypass = ssl_bypass
|
||||
self.plex = self.login(self.baseurl, self.token, ssl_bypass)
|
||||
if ssl_bypass:
|
||||
# Session for ssl bypass
|
||||
session = requests.Session()
|
||||
# By pass ssl hostname check https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186
|
||||
session.mount("https://", HostNameIgnoringAdapter())
|
||||
self.session = session
|
||||
self.plex = self.login(self.baseurl, self.token)
|
||||
self.admin_user = self.plex.myPlexAccount()
|
||||
self.users = self.get_users()
|
||||
|
||||
def login(self, baseurl, token, ssl_bypass=False):
|
||||
def login(self, baseurl, token):
|
||||
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)
|
||||
plex = PlexServer(baseurl, token, session=self.session)
|
||||
elif self.username and self.password and self.servername:
|
||||
# Login via plex account
|
||||
account = MyPlexAccount(self.username, self.password)
|
||||
@@ -323,9 +444,19 @@ class Plex:
|
||||
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
|
||||
)
|
||||
token = user.get_token(self.plex.machineIdentifier)
|
||||
if token:
|
||||
user_plex = self.login(
|
||||
self.plex._baseurl,
|
||||
token,
|
||||
)
|
||||
else:
|
||||
logger(
|
||||
f"Plex: Failed to get token for {user.title}, skipping",
|
||||
2,
|
||||
)
|
||||
users_watched[user.title] = {}
|
||||
continue
|
||||
|
||||
libraries = user_plex.library.sections()
|
||||
|
||||
@@ -345,11 +476,11 @@ class Plex:
|
||||
|
||||
if skip_reason:
|
||||
logger(
|
||||
f"Plex: Skipping library {library_title} {skip_reason}", 1
|
||||
f"Plex: Skipping library {library_title}: {skip_reason}", 1
|
||||
)
|
||||
continue
|
||||
|
||||
args.append([get_user_watched, user, user_plex, library])
|
||||
args.append([get_user_library_watched, user, user_plex, library])
|
||||
|
||||
for user_watched in future_thread_executor(args):
|
||||
for user, user_watched_temp in user_watched.items():
|
||||
@@ -388,9 +519,26 @@ class Plex:
|
||||
if self.admin_user == user:
|
||||
user_plex = self.plex
|
||||
else:
|
||||
user_plex = PlexServer(
|
||||
self.plex._baseurl, user.get_token(self.plex.machineIdentifier)
|
||||
)
|
||||
if isinstance(user, str):
|
||||
logger(
|
||||
f"Plex: {user} is not a plex object, attempting to get object for user",
|
||||
4,
|
||||
)
|
||||
user = self.plex.myPlexAccount().user(user)
|
||||
|
||||
token = user.get_token(self.plex.machineIdentifier)
|
||||
if token:
|
||||
user_plex = PlexServer(
|
||||
self.plex._baseurl,
|
||||
token,
|
||||
session=self.session,
|
||||
)
|
||||
else:
|
||||
logger(
|
||||
f"Plex: Failed to get token for {user.title}, skipping",
|
||||
2,
|
||||
)
|
||||
continue
|
||||
|
||||
for library, videos in libraries.items():
|
||||
library_other = None
|
||||
@@ -415,12 +563,13 @@ class Plex:
|
||||
else:
|
||||
logger(
|
||||
f"Plex: Library {library} or {library_other} not found in library list",
|
||||
2,
|
||||
1,
|
||||
)
|
||||
continue
|
||||
else:
|
||||
logger(
|
||||
f"Plex: Library {library} not found in library list", 2
|
||||
f"Plex: Library {library} not found in library list",
|
||||
1,
|
||||
)
|
||||
continue
|
||||
|
||||
|
||||
83
src/users.py
Normal file
83
src/users.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from src.functions import (
|
||||
logger,
|
||||
search_mapping,
|
||||
)
|
||||
|
||||
|
||||
def generate_user_list(server):
|
||||
# generate list of users from server 1 and server 2
|
||||
server_type = server[0]
|
||||
server_connection = server[1]
|
||||
|
||||
server_users = []
|
||||
if server_type == "plex":
|
||||
server_users = [x.title.lower() for x in server_connection.users]
|
||||
elif server_type == "jellyfin":
|
||||
server_users = [key.lower() for key in server_connection.users.keys()]
|
||||
|
||||
return server_users
|
||||
|
||||
|
||||
def combine_user_lists(server_1_users, server_2_users, user_mapping):
|
||||
# combined list of overlapping users from plex and jellyfin
|
||||
users = {}
|
||||
|
||||
for server_1_user in server_1_users:
|
||||
if user_mapping:
|
||||
mapped_user = search_mapping(user_mapping, server_1_user)
|
||||
if mapped_user in server_2_users:
|
||||
users[server_1_user] = 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:
|
||||
mapped_user = search_mapping(user_mapping, server_2_user)
|
||||
if mapped_user in server_1_users:
|
||||
users[mapped_user] = server_2_user
|
||||
continue
|
||||
|
||||
if server_2_user in server_1_users:
|
||||
users[server_2_user] = server_2_user
|
||||
|
||||
return users
|
||||
|
||||
|
||||
def filter_user_lists(users, blacklist_users, whitelist_users):
|
||||
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]
|
||||
|
||||
return users_filtered
|
||||
|
||||
|
||||
def generate_server_users(server, users):
|
||||
server_users = None
|
||||
|
||||
if server[0] == "plex":
|
||||
server_users = []
|
||||
for plex_user in server[1].users:
|
||||
if (
|
||||
plex_user.title.lower() in users.keys()
|
||||
or plex_user.title.lower() in users.values()
|
||||
):
|
||||
server_users.append(plex_user)
|
||||
elif server[0] == "jellyfin":
|
||||
server_users = {}
|
||||
for jellyfin_user, jellyfin_id in server[1].users.items():
|
||||
if (
|
||||
jellyfin_user.lower() in users.keys()
|
||||
or jellyfin_user.lower() in users.values()
|
||||
):
|
||||
server_users[jellyfin_user] = jellyfin_id
|
||||
|
||||
return server_users
|
||||
240
src/watched.py
Normal file
240
src/watched.py
Normal file
@@ -0,0 +1,240 @@
|
||||
import copy
|
||||
|
||||
from src.functions import logger, search_mapping, contains_nested
|
||||
|
||||
from src.library import generate_library_guids_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():
|
||||
if subkey in combined_dict[key]:
|
||||
# If the subkey already exists in the combined dictionary,
|
||||
# check if the values are different and raise an exception if they are
|
||||
if combined_dict[key][subkey] != subvalue:
|
||||
raise ValueError(
|
||||
f"Conflicting values for subkey '{subkey}' under key '{key}'"
|
||||
)
|
||||
else:
|
||||
# If the subkey does not exist in the combined dictionary, add it
|
||||
combined_dict[key][subkey] = subvalue
|
||||
|
||||
return combined_dict
|
||||
|
||||
|
||||
def check_remove_entry(video, library, video_index, library_watched_list_2):
|
||||
if video_index is not None:
|
||||
if (
|
||||
library_watched_list_2["completed"][video_index]
|
||||
== video["status"]["completed"]
|
||||
) and (library_watched_list_2["time"][video_index] == video["status"]["time"]):
|
||||
logger(
|
||||
f"Removing {video['title']} from {library} due to exact match",
|
||||
3,
|
||||
)
|
||||
return True
|
||||
elif (
|
||||
library_watched_list_2["completed"][video_index] == True
|
||||
and video["status"]["completed"] == False
|
||||
):
|
||||
logger(
|
||||
f"Removing {video['title']} from {library} due to being complete in one library and not the other",
|
||||
3,
|
||||
)
|
||||
return True
|
||||
elif (
|
||||
library_watched_list_2["completed"][video_index] == False
|
||||
and video["status"]["completed"] == False
|
||||
) and (video["status"]["time"] < library_watched_list_2["time"][video_index]):
|
||||
logger(
|
||||
f"Removing {video['title']} from {library} due to more time watched in one library than the other",
|
||||
3,
|
||||
)
|
||||
return True
|
||||
elif (
|
||||
library_watched_list_2["completed"][video_index] == True
|
||||
and video["status"]["completed"] == True
|
||||
):
|
||||
logger(
|
||||
f"Removing {video['title']} from {library} due to being complete in both libraries",
|
||||
3,
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
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 watched_list_1 that are in watched_list_2
|
||||
for user_1 in watched_list_1:
|
||||
user_other = None
|
||||
if user_mapping:
|
||||
user_other = search_mapping(user_mapping, user_1)
|
||||
user_2 = get_other(watched_list_2, user_1, user_other)
|
||||
if user_2 is None:
|
||||
continue
|
||||
|
||||
for library_1 in watched_list_1[user_1]:
|
||||
library_other = None
|
||||
if library_mapping:
|
||||
library_other = search_mapping(library_mapping, library_1)
|
||||
library_2 = get_other(watched_list_2[user_2], library_1, library_other)
|
||||
if library_2 is None:
|
||||
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_index = get_movie_index_in_dict(
|
||||
movie, movies_watched_list_2_keys_dict
|
||||
)
|
||||
if movie_index is not None:
|
||||
if check_remove_entry(
|
||||
movie,
|
||||
library_1,
|
||||
movie_index,
|
||||
movies_watched_list_2_keys_dict,
|
||||
):
|
||||
modified_watched_list_1[user_1][library_1].remove(movie)
|
||||
|
||||
# TV Shows
|
||||
elif isinstance(watched_list_1[user_1][library_1], dict):
|
||||
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_index = get_episode_index_in_dict(
|
||||
episode, episode_watched_list_2_keys_dict
|
||||
)
|
||||
if episode_index is not None:
|
||||
if check_remove_entry(
|
||||
episode,
|
||||
library_1,
|
||||
episode_index,
|
||||
episode_watched_list_2_keys_dict,
|
||||
):
|
||||
modified_watched_list_1[user_1][library_1][
|
||||
show_key_1
|
||||
][season].remove(episode)
|
||||
|
||||
# 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]
|
||||
|
||||
# Remove empty shows
|
||||
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']} because it is empty",
|
||||
3,
|
||||
)
|
||||
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 get_other(watched_list, object_1, object_2):
|
||||
if object_1 in watched_list:
|
||||
return object_1
|
||||
elif object_2 in watched_list:
|
||||
return object_2
|
||||
else:
|
||||
logger(f"{object_1} and {object_2} not found in watched list 2", 1)
|
||||
return None
|
||||
|
||||
|
||||
def get_movie_index_in_dict(movie, movies_watched_list_2_keys_dict):
|
||||
# Iterate through the keys and values of the movie dictionary
|
||||
for movie_key, movie_value in movie.items():
|
||||
# If the key is "locations", check if the "locations" key is present in the movies_watched_list_2_keys_dict dictionary
|
||||
if movie_key == "locations":
|
||||
if "locations" in movies_watched_list_2_keys_dict.keys():
|
||||
# Iterate through the locations in the movie dictionary
|
||||
for location in movie_value:
|
||||
# If the location is in the movies_watched_list_2_keys_dict dictionary, return index of the key
|
||||
return contains_nested(
|
||||
location, movies_watched_list_2_keys_dict["locations"]
|
||||
)
|
||||
|
||||
# If the key is not "locations", check if the movie_key is present in the movies_watched_list_2_keys_dict dictionary
|
||||
else:
|
||||
if movie_key in movies_watched_list_2_keys_dict.keys():
|
||||
# If the movie_value is in the movies_watched_list_2_keys_dict dictionary, return True
|
||||
if movie_value in movies_watched_list_2_keys_dict[movie_key]:
|
||||
return movies_watched_list_2_keys_dict[movie_key].index(movie_value)
|
||||
|
||||
# If the loop completes without finding a match, return False
|
||||
return None
|
||||
|
||||
|
||||
def get_episode_index_in_dict(episode, episode_watched_list_2_keys_dict):
|
||||
# Iterate through the keys and values of the episode dictionary
|
||||
for episode_key, episode_value in episode.items():
|
||||
if episode_key in episode_watched_list_2_keys_dict.keys():
|
||||
if episode_key == "locations":
|
||||
# Iterate through the locations in the episode dictionary
|
||||
for location in episode_value:
|
||||
# If the location is in the episode_watched_list_2_keys_dict dictionary, return index of the key
|
||||
return contains_nested(
|
||||
location, episode_watched_list_2_keys_dict["locations"]
|
||||
)
|
||||
|
||||
else:
|
||||
# If the episode_value is in the episode_watched_list_2_keys_dict dictionary, return True
|
||||
if episode_value in episode_watched_list_2_keys_dict[episode_key]:
|
||||
return episode_watched_list_2_keys_dict[episode_key].index(
|
||||
episode_value
|
||||
)
|
||||
|
||||
# If the loop completes without finding a match, return False
|
||||
return None
|
||||
78
test/test_black_white.py
Normal file
78
test/test_black_white.py
Normal 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.black_white 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"]
|
||||
317
test/test_library.py
Normal file
317
test/test_library.py
Normal file
@@ -0,0 +1,317 @@
|
||||
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.functions import (
|
||||
search_mapping,
|
||||
)
|
||||
|
||||
from src.library import (
|
||||
check_skip_logic,
|
||||
check_blacklist_logic,
|
||||
check_whitelist_logic,
|
||||
show_title_dict,
|
||||
episode_title_dict,
|
||||
movies_title_dict,
|
||||
generate_library_guids_dict,
|
||||
)
|
||||
|
||||
blacklist_library = ["TV Shows"]
|
||||
whitelist_library = ["Movies"]
|
||||
blacklist_library_type = ["episodes"]
|
||||
whitelist_library_type = ["movies"]
|
||||
library_mapping = {"Shows": "TV Shows", "Movie": "Movies"}
|
||||
|
||||
show_list = {
|
||||
frozenset(
|
||||
{
|
||||
("locations", ("The Last of Us",)),
|
||||
("tmdb", "100088"),
|
||||
("imdb", "tt3581920"),
|
||||
("tvdb", "392256"),
|
||||
("title", "The Last of Us"),
|
||||
}
|
||||
): {
|
||||
"Season 1": [
|
||||
{
|
||||
"imdb": "tt11957006",
|
||||
"tmdb": "2181581",
|
||||
"tvdb": "8444132",
|
||||
"locations": (
|
||||
(
|
||||
"The Last of Us - S01E01 - When You're Lost in the Darkness WEBDL-1080p.mkv",
|
||||
)
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
movie_list = [
|
||||
{
|
||||
"title": "Coco",
|
||||
"imdb": "tt2380307",
|
||||
"tmdb": "354912",
|
||||
"locations": [("Coco (2017) Remux-2160p.mkv", "Coco (2017) Remux-1080p.mkv")],
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
|
||||
show_titles = {
|
||||
"imdb": ["tt3581920"],
|
||||
"locations": [("The Last of Us",)],
|
||||
"tmdb": ["100088"],
|
||||
"tvdb": ["392256"],
|
||||
}
|
||||
episode_titles = {
|
||||
"imdb": ["tt11957006"],
|
||||
"locations": [
|
||||
("The Last of Us - S01E01 - When You're Lost in the Darkness WEBDL-1080p.mkv",)
|
||||
],
|
||||
"tmdb": ["2181581"],
|
||||
"tvdb": ["8444132"],
|
||||
"completed": [True],
|
||||
"time": [0],
|
||||
}
|
||||
movie_titles = {
|
||||
"imdb": ["tt2380307"],
|
||||
"locations": [
|
||||
[
|
||||
(
|
||||
"Coco (2017) Remux-2160p.mkv",
|
||||
"Coco (2017) Remux-1080p.mkv",
|
||||
)
|
||||
]
|
||||
],
|
||||
"title": ["coco"],
|
||||
"tmdb": ["354912"],
|
||||
"completed": [True],
|
||||
"time": [0],
|
||||
}
|
||||
|
||||
|
||||
def test_check_skip_logic():
|
||||
# Failes
|
||||
library_title = "Test"
|
||||
library_type = "movies"
|
||||
skip_reason = check_skip_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
|
||||
assert skip_reason == "Test is not in whitelist_library"
|
||||
|
||||
library_title = "Shows"
|
||||
library_type = "episodes"
|
||||
skip_reason = check_skip_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
|
||||
assert (
|
||||
skip_reason
|
||||
== "episodes is in blacklist_library_type and TV Shows is in blacklist_library and "
|
||||
+ "episodes is not in whitelist_library_type and Shows is not in whitelist_library"
|
||||
)
|
||||
|
||||
# Passes
|
||||
library_title = "Movie"
|
||||
library_type = "movies"
|
||||
skip_reason = check_skip_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
|
||||
assert skip_reason is None
|
||||
|
||||
|
||||
def test_check_blacklist_logic():
|
||||
# Fails
|
||||
library_title = "Shows"
|
||||
library_type = "episodes"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_blacklist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert (
|
||||
skip_reason
|
||||
== "episodes is in blacklist_library_type and TV Shows is in blacklist_library"
|
||||
)
|
||||
|
||||
library_title = "TV Shows"
|
||||
library_type = "episodes"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_blacklist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert (
|
||||
skip_reason
|
||||
== "episodes is in blacklist_library_type and TV Shows is in blacklist_library"
|
||||
)
|
||||
|
||||
# Passes
|
||||
library_title = "Movie"
|
||||
library_type = "movies"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_blacklist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert skip_reason is None
|
||||
|
||||
library_title = "Movies"
|
||||
library_type = "movies"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_blacklist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
blacklist_library,
|
||||
blacklist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert skip_reason is None
|
||||
|
||||
|
||||
def test_check_whitelist_logic():
|
||||
# Fails
|
||||
library_title = "Shows"
|
||||
library_type = "episodes"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_whitelist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert (
|
||||
skip_reason
|
||||
== "episodes is not in whitelist_library_type and Shows is not in whitelist_library"
|
||||
)
|
||||
|
||||
library_title = "TV Shows"
|
||||
library_type = "episodes"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_whitelist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert (
|
||||
skip_reason
|
||||
== "episodes is not in whitelist_library_type and TV Shows is not in whitelist_library"
|
||||
)
|
||||
|
||||
# Passes
|
||||
library_title = "Movie"
|
||||
library_type = "movies"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_whitelist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert skip_reason is None
|
||||
|
||||
library_title = "Movies"
|
||||
library_type = "movies"
|
||||
library_other = search_mapping(library_mapping, library_title)
|
||||
skip_reason = check_whitelist_logic(
|
||||
library_title,
|
||||
library_type,
|
||||
whitelist_library,
|
||||
whitelist_library_type,
|
||||
library_other,
|
||||
)
|
||||
|
||||
assert skip_reason is None
|
||||
|
||||
|
||||
def test_show_title_dict():
|
||||
show_titles_dict = show_title_dict(show_list)
|
||||
|
||||
assert show_titles_dict == show_titles
|
||||
|
||||
|
||||
def test_episode_title_dict():
|
||||
episode_titles_dict = episode_title_dict(show_list)
|
||||
|
||||
assert episode_titles_dict == episode_titles
|
||||
|
||||
|
||||
def test_movies_title_dict():
|
||||
movies_titles_dict = movies_title_dict(movie_list)
|
||||
|
||||
assert movies_titles_dict == movie_titles
|
||||
|
||||
|
||||
def test_generate_library_guids_dict():
|
||||
# Test with shows
|
||||
(
|
||||
show_titles_dict,
|
||||
episode_titles_dict,
|
||||
movies_titles_dict,
|
||||
) = generate_library_guids_dict(show_list)
|
||||
|
||||
assert show_titles_dict == show_titles
|
||||
assert episode_titles_dict == episode_titles
|
||||
assert movies_titles_dict == {}
|
||||
|
||||
# Test with movies
|
||||
(
|
||||
show_titles_dict,
|
||||
episode_titles_dict,
|
||||
movies_titles_dict,
|
||||
) = generate_library_guids_dict(movie_list)
|
||||
|
||||
assert show_titles_dict == {}
|
||||
assert episode_titles_dict == {}
|
||||
assert movies_titles_dict == movie_titles
|
||||
@@ -1,78 +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"]
|
||||
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.black_white 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"]
|
||||
|
||||
39
test/test_users.py
Normal file
39
test/test_users.py
Normal file
@@ -0,0 +1,39 @@
|
||||
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.users import (
|
||||
combine_user_lists,
|
||||
filter_user_lists,
|
||||
)
|
||||
|
||||
|
||||
def test_combine_user_lists():
|
||||
server_1_users = ["test", "test3", "luigi311"]
|
||||
server_2_users = ["luigi311", "test2", "test3"]
|
||||
user_mapping = {"test2": "test"}
|
||||
|
||||
combined = combine_user_lists(server_1_users, server_2_users, user_mapping)
|
||||
|
||||
assert combined == {"luigi311": "luigi311", "test": "test2", "test3": "test3"}
|
||||
|
||||
|
||||
def test_filter_user_lists():
|
||||
users = {"luigi311": "luigi311", "test": "test2", "test3": "test3"}
|
||||
blacklist_users = ["test3"]
|
||||
whitelist_users = ["test", "luigi311"]
|
||||
|
||||
filtered = filter_user_lists(users, blacklist_users, whitelist_users)
|
||||
|
||||
assert filtered == {"test": "test2", "luigi311": "luigi311"}
|
||||
@@ -1,301 +1,487 @@
|
||||
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
|
||||
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.watched import cleanup_watched, combine_watched_dicts
|
||||
|
||||
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",
|
||||
"title": "Extreme Aggressor",
|
||||
"locations": (
|
||||
"Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"imdb": "tt0550487",
|
||||
"tmdb": "282861",
|
||||
"tvdb": "300385",
|
||||
"title": "Compulsion",
|
||||
"locations": ("Criminal Minds S01E02 Compulsion WEBDL-720p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
},
|
||||
frozenset({("title", "Test"), ("locations", ("Test",))}): {
|
||||
"Season 1": [
|
||||
{
|
||||
"title": "S01E01",
|
||||
"locations": ("Test S01E01.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "S01E02",
|
||||
"locations": ("Test S01E02.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "S01E04",
|
||||
"locations": ("Test S01E04.mkv",),
|
||||
"status": {"completed": False, "time": 5},
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
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",
|
||||
"title": "Compulsion",
|
||||
"locations": ("Criminal Minds S01E02 Compulsion WEBDL-720p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"imdb": "tt0550498",
|
||||
"tmdb": "282865",
|
||||
"tvdb": "300474",
|
||||
"title": "Won't Get Fooled Again",
|
||||
"locations": (
|
||||
"Criminal Minds S01E03 Won't Get Fooled Again WEBDL-720p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
},
|
||||
frozenset({("title", "Test"), ("locations", ("Test",))}): {
|
||||
"Season 1": [
|
||||
{
|
||||
"title": "S01E02",
|
||||
"locations": ("Test S01E02.mkv",),
|
||||
"status": {"completed": False, "time": 10},
|
||||
},
|
||||
{
|
||||
"title": "S01E03",
|
||||
"locations": ("Test S01E03.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "S01E04",
|
||||
"locations": ("Test S01E04.mkv",),
|
||||
"status": {"completed": False, "time": 10},
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
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",
|
||||
"title": "Extreme Aggressor",
|
||||
"locations": (
|
||||
"Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
},
|
||||
frozenset({("title", "Test"), ("locations", ("Test",))}): {
|
||||
"Season 1": [
|
||||
{
|
||||
"title": "S01E01",
|
||||
"locations": ("Test S01E01.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "S01E02",
|
||||
"locations": ("Test S01E02.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
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",
|
||||
"title": "Won't Get Fooled Again",
|
||||
"locations": (
|
||||
"Criminal Minds S01E03 Won't Get Fooled Again WEBDL-720p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
},
|
||||
frozenset({("title", "Test"), ("locations", ("Test",))}): {
|
||||
"Season 1": [
|
||||
{
|
||||
"title": "S01E03",
|
||||
"locations": ("Test S01E03.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "S01E04",
|
||||
"locations": ("Test S01E04.mkv",),
|
||||
"status": {"completed": False, "time": 10},
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
movies_watched_list_1 = [
|
||||
{
|
||||
"imdb": "tt2380307",
|
||||
"tmdb": "354912",
|
||||
"title": "Coco",
|
||||
"locations": ("Coco (2017) Remux-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"tmdbcollection": "448150",
|
||||
"imdb": "tt1431045",
|
||||
"tmdb": "293660",
|
||||
"title": "Deadpool",
|
||||
"locations": ("Deadpool (2016) Remux-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
|
||||
movies_watched_list_2 = [
|
||||
{
|
||||
"imdb": "tt2380307",
|
||||
"tmdb": "354912",
|
||||
"title": "Coco",
|
||||
"locations": ("Coco (2017) Remux-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"imdb": "tt0384793",
|
||||
"tmdb": "9788",
|
||||
"tvdb": "9103",
|
||||
"title": "Accepted",
|
||||
"locations": ("Accepted (2006) Remux-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
expected_movie_watched_list_1 = [
|
||||
{
|
||||
"tmdbcollection": "448150",
|
||||
"imdb": "tt1431045",
|
||||
"tmdb": "293660",
|
||||
"title": "Deadpool",
|
||||
"locations": ("Deadpool (2016) Remux-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
|
||||
expected_movie_watched_list_2 = [
|
||||
{
|
||||
"imdb": "tt0384793",
|
||||
"tmdb": "9788",
|
||||
"tvdb": "9103",
|
||||
"title": "Accepted",
|
||||
"locations": ("Accepted (2006) Remux-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
|
||||
# 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",
|
||||
"title": "Extreme Aggressor",
|
||||
"locations": (
|
||||
"Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def test_combine_watched_dicts():
|
||||
input_watched = [
|
||||
{
|
||||
"test3": {
|
||||
"Anime Movies": [
|
||||
{
|
||||
"title": "Ponyo",
|
||||
"tmdb": "12429",
|
||||
"imdb": "tt0876563",
|
||||
"locations": ("Ponyo (2008) Bluray-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "Spirited Away",
|
||||
"tmdb": "129",
|
||||
"imdb": "tt0245429",
|
||||
"locations": ("Spirited Away (2001) Bluray-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "Castle in the Sky",
|
||||
"tmdb": "10515",
|
||||
"imdb": "tt0092067",
|
||||
"locations": ("Castle in the Sky (1986) Bluray-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
]
|
||||
}
|
||||
},
|
||||
{"test3": {"Anime Shows": {}}},
|
||||
{"test3": {"Cartoon Shows": {}}},
|
||||
{
|
||||
"test3": {
|
||||
"Shows": {
|
||||
frozenset(
|
||||
{
|
||||
("tmdb", "64464"),
|
||||
("tvdb", "301824"),
|
||||
("tvrage", "45210"),
|
||||
("title", "11.22.63"),
|
||||
("locations", ("11.22.63",)),
|
||||
("imdb", "tt2879552"),
|
||||
}
|
||||
): {
|
||||
"Season 1": [
|
||||
{
|
||||
"imdb": "tt4460418",
|
||||
"title": "The Rabbit Hole",
|
||||
"locations": (
|
||||
"11.22.63 S01E01 The Rabbit Hole Bluray-1080p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{"test3": {"Subbed Anime": {}}},
|
||||
]
|
||||
expected = {
|
||||
"test3": {
|
||||
"Anime Movies": [
|
||||
{
|
||||
"title": "Ponyo",
|
||||
"tmdb": "12429",
|
||||
"imdb": "tt0876563",
|
||||
"locations": ("Ponyo (2008) Bluray-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "Spirited Away",
|
||||
"tmdb": "129",
|
||||
"imdb": "tt0245429",
|
||||
"locations": ("Spirited Away (2001) Bluray-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
{
|
||||
"title": "Castle in the Sky",
|
||||
"tmdb": "10515",
|
||||
"imdb": "tt0092067",
|
||||
"locations": ("Castle in the Sky (1986) Bluray-1080p.mkv",),
|
||||
"status": {"completed": True, "time": 0},
|
||||
},
|
||||
],
|
||||
"Anime Shows": {},
|
||||
"Cartoon Shows": {},
|
||||
"Shows": {
|
||||
frozenset(
|
||||
{
|
||||
("tmdb", "64464"),
|
||||
("tvdb", "301824"),
|
||||
("tvrage", "45210"),
|
||||
("title", "11.22.63"),
|
||||
("locations", ("11.22.63",)),
|
||||
("imdb", "tt2879552"),
|
||||
}
|
||||
): {
|
||||
"Season 1": [
|
||||
{
|
||||
"imdb": "tt4460418",
|
||||
"title": "The Rabbit Hole",
|
||||
"locations": (
|
||||
"11.22.63 S01E01 The Rabbit Hole Bluray-1080p.mkv",
|
||||
),
|
||||
"status": {"completed": True, "time": 0},
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"Subbed Anime": {},
|
||||
}
|
||||
}
|
||||
|
||||
assert combine_watched_dicts(input_watched) == expected
|
||||
Reference in New Issue
Block a user