|
| 1 | +from logging import Logger |
| 2 | +from apscheduler.schedulers.blocking import BlockingScheduler |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from common import utils |
| 5 | + |
| 6 | +from service.ServiceBase import ServiceBase |
| 7 | + |
| 8 | +from api.plex import PlexAPI, PlexCollection |
| 9 | +from api.emby import EmbyAPI, EmbyPlaylist |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class PlexCollectionConfig: |
| 13 | + library_name: str |
| 14 | + collection_name: str |
| 15 | + target_servers: list[str] = field(default_factory=list) |
| 16 | + |
| 17 | +class PlaylistSync(ServiceBase): |
| 18 | + def __init__(self, ansi_code: str, plex_api: PlexAPI, emby_api: EmbyAPI, config, logger: Logger, scheduler: BlockingScheduler): |
| 19 | + super().__init__(ansi_code, self.__module__, config, logger, scheduler) |
| 20 | + |
| 21 | + self.plex_api = plex_api |
| 22 | + self.emby_api = emby_api |
| 23 | + self.plex_collection_configs: list[PlexCollectionConfig] = [] |
| 24 | + |
| 25 | + try: |
| 26 | + for plex_collection in config['plex_collection_sync']: |
| 27 | + if 'library' in plex_collection and 'collection_name' in plex_collection and 'target_servers' in plex_collection: |
| 28 | + library_name = plex_collection['library'] |
| 29 | + collection_name = plex_collection['collection_name'] |
| 30 | + target_servers: list[str] = [] |
| 31 | + |
| 32 | + target_server_names = plex_collection['target_servers'].split(',') |
| 33 | + for target_server_name in target_server_names: |
| 34 | + supported_target_server_name = self.__server_supported(target_server_name) |
| 35 | + if supported_target_server_name != '': |
| 36 | + target_servers.append(supported_target_server_name) |
| 37 | + |
| 38 | + if library_name != '' and collection_name != '' and len(target_servers) > 0: |
| 39 | + if plex_api.get_valid() is True and plex_api.get_collection_valid(library_name, collection_name) is False: |
| 40 | + self.log_warning('{} {} {} not found on server'.format(utils.get_formatted_plex(), utils.get_tag('library', library_name), utils.get_tag('collection', collection_name))) |
| 41 | + |
| 42 | + self.plex_collection_configs.append(PlexCollectionConfig(library_name, collection_name, target_servers)) |
| 43 | + |
| 44 | + except Exception as e: |
| 45 | + self.log_error('Read config {}'.format(utils.get_tag('error', e))) |
| 46 | + |
| 47 | + def __server_supported(self, server_type: str) -> str: |
| 48 | + lower_name = server_type.lower() |
| 49 | + if (lower_name == 'plex' or lower_name == 'emby'): |
| 50 | + return lower_name |
| 51 | + return '' |
| 52 | + |
| 53 | + def __sync_emby_playlist(self, emby_item_ids: list[str], emby_playlist: EmbyPlaylist): |
| 54 | + # Check if any items were added to the playlist |
| 55 | + added_items: list[str] = [] |
| 56 | + for emby_item_id in emby_item_ids: |
| 57 | + item_id_found = False |
| 58 | + for item in emby_playlist.items: |
| 59 | + if emby_item_id == item.id: |
| 60 | + item_id_found = True |
| 61 | + break |
| 62 | + if item_id_found is False: |
| 63 | + added_items.append(emby_item_id) |
| 64 | + |
| 65 | + # Check if any items were deleted out of the playlist |
| 66 | + deleted_items: list[str] = [] |
| 67 | + for item in emby_playlist.items: |
| 68 | + if item.id not in emby_item_ids: |
| 69 | + deleted_items.append(item.id) |
| 70 | + |
| 71 | + # Check if the order of the playlist items changed |
| 72 | + playlist_changed = False |
| 73 | + if len(emby_item_ids) == len(emby_playlist.items): |
| 74 | + playlist_index = 0 |
| 75 | + for item_id in emby_item_ids: |
| 76 | + if item_id != emby_playlist.items[playlist_index].id: |
| 77 | + playlist_changed = True |
| 78 | + break |
| 79 | + playlist_index += 1 |
| 80 | + else: |
| 81 | + playlist_changed = True |
| 82 | + |
| 83 | + if playlist_changed is True or len(added_items) > 0 or len(deleted_items) > 0: |
| 84 | + self.log_info('Syncing {} {} to {}'.format(utils.get_formatted_plex(), utils.get_tag('collection', emby_playlist.name), utils.get_formatted_emby())) |
| 85 | + |
| 86 | + if len(added_items) > 0: |
| 87 | + self.emby_api.add_playlist_items(emby_playlist.id, added_items) |
| 88 | + |
| 89 | + if len(deleted_items) > 0: |
| 90 | + self.emby_api.remove_playlist_items(emby_playlist.id, deleted_items) |
| 91 | + |
| 92 | + current_index = 1 |
| 93 | + for item_id in emby_item_ids: |
| 94 | + self.emby_api.set_move_playlist_item_to_index(emby_playlist.id, item_id, current_index) |
| 95 | + current_index += 1 |
| 96 | + |
| 97 | + def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollection): |
| 98 | + emby_item_ids: list[str] = [] |
| 99 | + for plex_item in plex_collection.items: |
| 100 | + emby_item_id = self.emby_api.get_item_id_from_path(plex_item.path) |
| 101 | + if emby_item_id != self.emby_api.get_invalid_item_id(): |
| 102 | + emby_item_ids.append(emby_item_id) |
| 103 | + else: |
| 104 | + self.log_warning('{} sync {} {} item not found {}'.format(utils.get_emby_ansi_code(), utils.get_plex_ansi_code(), utils.get_tag('collection', plex_collection.name), utils.get_tag('item', plex_item.title))) |
| 105 | + |
| 106 | + emby_playlist_id = self.emby_api.get_playlist_id(plex_collection.name) |
| 107 | + if emby_playlist_id == self.emby_api.get_invalid_item_id(): |
| 108 | + self.emby_api.create_playlist(plex_collection.name, emby_item_ids) |
| 109 | + self.log_info('Syncing {} {} to {}'.format(utils.get_formatted_plex(), utils.get_tag('collection', plex_collection.name), utils.get_formatted_emby())) |
| 110 | + else: |
| 111 | + emby_playlist:EmbyPlaylist = self.emby_api.get_playlist_items(emby_playlist_id) |
| 112 | + if emby_playlist is not None: |
| 113 | + self.__sync_emby_playlist(emby_item_ids, emby_playlist) |
| 114 | + |
| 115 | + def __sync_plex_collection(self, collection_config: PlexCollectionConfig): |
| 116 | + collection: PlexCollection = self.plex_api.get_collection(collection_config.library_name, collection_config.collection_name) |
| 117 | + if collection != self.plex_api.get_invalid_type(): |
| 118 | + for target_server in collection_config.target_servers: |
| 119 | + if target_server == 'emby' and self.emby_api.get_valid() is True: |
| 120 | + self.__sync_emby_playlist_with_plex_collection(collection) |
| 121 | + |
| 122 | + def __sync_playlists(self): |
| 123 | + if len(self.plex_collection_configs) > 0: |
| 124 | + plex_valid = self.plex_api.get_valid() |
| 125 | + emby_valid = self.emby_api.get_valid() |
| 126 | + if plex_valid is True and emby_valid is True: |
| 127 | + for plex_collection_config in self.plex_collection_configs: |
| 128 | + self.__sync_plex_collection(plex_collection_config) |
| 129 | + else: |
| 130 | + if plex_valid is False: |
| 131 | + self.log_warning(self.plex_api.get_connection_error_log()) |
| 132 | + if emby_valid is False: |
| 133 | + self.log_warning(self.emby_api.get_connection_error_log()) |
| 134 | + |
| 135 | + def init_scheduler_jobs(self): |
| 136 | + if self.cron is not None: |
| 137 | + self.log_service_enabled() |
| 138 | + self.scheduler.add_job(self.__sync_playlists, trigger='cron', hour=self.cron.hours, minute=self.cron.minutes) |
| 139 | + else: |
| 140 | + self.log_warning('Enabled but will not Run. Cron is not valid!') |
0 commit comments