def get_trakt_id(title, item_type, year=None): required_type = 'show' if item_type == 'episode' else 'movie' global trakt_cache if not trakt_cache: trakt_cache = read_json(TRAKT_CACHE_PATH) or {'movie': {}, 'show': {}} trakt_id = trakt_cache[required_type].get(title) if trakt_id: return trakt_id logger.debug(f'Searching trakt: Title: "{title}", Year: {year}') results = search(title, [required_type], year) if results is None: # Connection error return 0 # Dont store in cache elif results == [] or results[0]['score'] < 5: # Weak or no match msg = f'Trakt search yielded no results for the {required_type}, {title}' msg += f", Year: {year}" * bool(year) logger.warning(msg) notify(msg) trakt_id = -1 else: trakt_id = results[0][required_type]['ids']['trakt'] trakt_cache[required_type][title] = trakt_id logger.debug(f'Trakt ID: {trakt_id}') write_json(trakt_cache, TRAKT_CACHE_PATH) return trakt_id
def __init__(self, manual=False): self.backlog = read_json(self.BACKLOG_PATH) or [] self.clear_interval = config["backlog"]["clear_interval"].get( confuse.Number()) self.expiry = config["backlog"]["expiry"].get(confuse.Number()) self.timer_enabled = not manual if self.timer_enabled: self._make_timer() self.clear()
def try_migrate_old_file(self): token = read_json(self.OLD_PATH) if not token: # we really don't have the token return None # we got the token from old file- move to new self.OLD_PATH.unlink() self.data = token["token"] return token["token"]
def get_token(**kwargs): token = kwargs.get("token") or read_json(PLEX_TOKEN_PATH).get("token") if not token: logger.info("Retrieving plex token") token = plex_token_auth(kwargs["login"], kwargs["password"]) if token: write_json({"token": token}, PLEX_TOKEN_PATH) logger.info(f"Saved plex token to {PLEX_TOKEN_PATH}") return token
def get_access_token(): global token_data if not token_data: token_data = read_json(TRAKT_TOKEN_PATH) if not token_data: logger.info("Access token not found in config. " "Initiating device authentication.") token_data = device_auth() write_json(token_data, TRAKT_TOKEN_PATH) elif token_data['created_at'] + token_data['expires_in'] - \ time.time() < 86400: logger.info("Access token about to expire. Refreshing.") token_data = refresh_token(token_data) write_json(token_data, TRAKT_TOKEN_PATH) if not token_data: logger.error("Unable to get access token. " f"Try deleting {TRAKT_TOKEN_PATH!s} and retry.") notify("Failed to authorize application.", stdout=True) sys.exit(1) return token_data['access_token']
def get_token(): global token_data token_data = read_json(PLEX_TOKEN_PATH) or {} if not token_data: logger.info("Retrieving plex token") login = input("Plex login ID: ") pwd = getpass() resp = plex_token_auth(login, pwd) if resp: token_data = {"token": resp.json()["user"]["authToken"]} write_json(token_data, PLEX_TOKEN_PATH) logger.info(f"Saved plex token to {PLEX_TOKEN_PATH}") elif resp is not None: print(resp.json().get("error", resp.text)) return None else: logger.error("Unable to get access token. " f"Try deleting {PLEX_TOKEN_PATH!s} and retry.") return None return token_data['token']
def get_trakt_id(title, item_type, year=None): required_type = 'show' if item_type == 'episode' else 'movie' global trakt_cache if not trakt_cache: trakt_cache = read_json(TRAKT_CACHE_PATH) or {'movie': {}, 'show': {}} key = f"{title}{year or ''}" trakt_id = trakt_cache[required_type].get(key) if trakt_id: return trakt_id logger.debug( f'Searching trakt: Title: "{title}"{year and f", Year: {year}" or ""}') results = search(title, [required_type], year) if results == [] and year is not None: # no match, possibly a mismatch in year metadata msg = ( f'Trakt search yielded no results for the {required_type}, {title}, ' f'Year: {year}. Retrying search without filtering by year.') logger.warning(msg) notify(msg, category="trakt") results = search(title, [required_type]) # retry without 'year' if results is None: # Connection error return 0 # Dont store in cache elif results == [] or results[0]['score'] < 5: # Weak or no match msg = f'Trakt search yielded no results for the {required_type}, {title}' msg += f", Year: {year}" * bool(year) logger.warning(msg) notify(msg, category="trakt") trakt_id = -1 else: trakt_id = results[0][required_type]['ids']['trakt'] trakt_cache[required_type][key] = trakt_id logger.debug(f'Trakt ID: {trakt_id}') write_json(trakt_cache, TRAKT_CACHE_PATH) return trakt_id
import time import sys import webbrowser from datetime import datetime as dt from trakt_scrobbler import logger, trakt_key_holder from trakt_scrobbler.app_dirs import DATA_DIR from trakt_scrobbler.notifier import notify from trakt_scrobbler.utils import safe_request, read_json, write_json CLIENT_ID = trakt_key_holder.get_id() CLIENT_SECRET = trakt_key_holder.get_secret() API_URL = "https://api.trakt.tv" TRAKT_CACHE_PATH = DATA_DIR / 'trakt_cache.json' TRAKT_TOKEN_PATH = DATA_DIR / 'trakt_token.json' trakt_cache = read_json(TRAKT_CACHE_PATH) or {'movie': {}, 'show': {}} token_data = read_json(TRAKT_TOKEN_PATH) def get_device_code(): code_request_params = { "url": API_URL + "/oauth/device/code", "headers": { "Content-Type": "application/json" }, "json": { "client_id": CLIENT_ID } } code_resp = safe_request('post', code_request_params) return code_resp.json() if code_resp else None
from getpass import getpass import confuse from trakt_scrobbler import logger from trakt_scrobbler.app_dirs import DATA_DIR from trakt_scrobbler.player_monitors.monitor import WebInterfaceMon from trakt_scrobbler.utils import read_json, safe_request, write_json PLEX_TOKEN_PATH = DATA_DIR / "plex_token.json" token_data = read_json(PLEX_TOKEN_PATH) or {} def plex_token_auth(login, password): auth_params = { "url": "https://plex.tv/users/sign_in.json", "data": { "user[login]": login, "user[password]": password }, "headers": { "X-Plex-Client-Identifier": "com.iamkroot.trakt_scrobbler", "X-Plex-Product": "Trakt Scrobbler", "Accept": "application/json", }, } return safe_request("post", auth_params) def get_token(): global token_data if not token_data:
def token_data(self): if not self._token_data: self._token_data = read_json(self.TRAKT_TOKEN_PATH) return self._token_data