-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathTautulliSyncHelper.py
134 lines (109 loc) · 4.22 KB
/
TautulliSyncHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import collections
import configparser
import coloredlogs
import json
import logging
import os
import re
import requests
import sys
from time import sleep
from guessit import guessit
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
import Libs.anilist as anilist
import Libs.plexmodule as plexmodule
# Logger settings
logger = logging.getLogger('PlexAniSync')
coloredlogs.install(fmt='%(asctime)s %(message)s', logger=logger)
# Enable this if you want to also log all messages coming from imported libraries
# coloredlogs.install(level='DEBUG')
## Settings section ##
def read_settings(settings_file):
if not os.path.isfile(settings_file):
logger.critical(
'[CONFIG] Settings file file not found: %s' % (settings_file))
sys.exit()
settings = configparser.ConfigParser()
settings.read(settings_file)
return settings
if len(sys.argv) > 2:
settings_file = sys.argv[1]
logger.warning(
'Found settings file parameter and using: %s' %
(settings_file))
else:
settings_file = 'settings.ini'
settings = read_settings(settings_file)
anilist_settings = settings['ANILIST']
plex_settings = settings['PLEX']
ANILIST_SKIP_UPDATE = anilist_settings['skip_list_update'].lower()
ANILIST_ACCESS_TOKEN = anilist_settings['access_token'].strip()
mapping_file = 'custom_mappings.ini'
custom_mappings = []
def read_custom_mappings(mapping_file):
if not os.path.isfile(mapping_file):
logger.info(
'[MAPPING] Custom map file not found: %s' % (mapping_file))
else:
logger.info('[MAPPING] Custom map file found: %s' % (mapping_file))
file = open(mapping_file, "r")
for line in file:
try:
mappingSplit = line.split('^')
series_title = mappingSplit[0]
season = mappingSplit[1]
anime_id = int(mappingSplit[2])
logger.info(
"[MAPPING] Adding custom mapping | title: %s | season: %s | anilist id: %s" %
(series_title, season, anime_id))
mapping = anilist.anilist_custom_mapping(
series_title, season, anime_id)
custom_mappings.append(mapping)
except BaseException:
logger.error(
'[MAPPING] Invalid entry found for line: %s' %
(line))
## Startup section ##
def start():
if len(sys.argv) < 2:
logger.error(
'No show title specified in arguments so cancelling updating')
sys.exit()
else:
# If we have custom settings file parameter use different arg index to
# keep legacy method intact
if len(sys.argv) > 2:
show_title = sys.argv[2]
elif len(sys.argv) == 2:
show_title = sys.argv[1]
logger.info('Updating single show: %s' % (show_title))
if ANILIST_SKIP_UPDATE == 'true':
logger.warning(
'AniList skip list update enabled in settings, will match but NOT update your list')
# Wait a few a seconds to make sure Plex has processed watched states
sleep(5.0)
# Anilist
anilist_username = anilist_settings['username']
anilist.custom_mappings = custom_mappings
anilist.ANILIST_ACCESS_TOKEN = ANILIST_ACCESS_TOKEN
anilist.ANILIST_SKIP_UPDATE = ANILIST_SKIP_UPDATE
anilist_series = anilist.process_user_list(anilist_username)
# Plex
if anilist_series is None:
logger.error(
'Unable to retrieve AniList list, check your username and access token')
elif not anilist_series:
logger.error('No items found on your AniList list to process')
else:
plexmodule.plex_settings = plex_settings
plex_anime_series = plexmodule.get_anime_shows_filter(show_title)
plex_series_watched = plexmodule.get_watched_shows(plex_anime_series)
anilist.match_to_plex(
anilist_series,
plex_anime_series,
plex_series_watched)
logger.info('Plex to AniList sync finished')
if __name__ == '__main__':
read_custom_mappings(mapping_file)
start()