def create_trakt_account(cls, account, username): try: return True, TraktAccount.create( account=account, username=username ) except (apsw.ConstraintError, peewee.IntegrityError): return False, TraktAccount.get( account=account )
def on_trakt_refresh_rejected(cls, username): log.debug('[Trakt.tv] Token refresh for %r has been rejected', username) # Find matching trakt account account = (TraktAccount.select().where( TraktAccount.username == username)).first() if not account: log.warn('[Trakt.tv] Unable to find account with the username: %r', username) return False # Delete OAuth credential TraktOAuthCredentialManager.delete(account=account.id) log.info('[Trakt.tv] Token cleared for %r', account) return True
def on_trakt_refresh(cls, username, authorization): log.debug('[Trakt.tv] Token has been refreshed for %r', username) # Retrieve trakt account matching this `authorization` with Trakt.configuration.http(retry=True).oauth( token=authorization.get('access_token')): settings = Trakt['users/settings'].get(validate_token=False) if not settings: log.warn('[Trakt.tv] Unable to retrieve account details for token') return False # Retrieve trakt account username from `settings` s_username = settings.get('user', {}).get('username') if not s_username: log.warn('[Trakt.tv] Unable to retrieve username for token') return False if s_username != username: log.warn('[Trakt.tv] Token mismatch (%r != %r)', s_username, username) return False # Find matching trakt account trakt_account = (TraktAccount.select().where( TraktAccount.username == username)).first() if not trakt_account: log.warn('[Trakt.tv] Unable to find account with the username: %r', username) return False # Update OAuth credential TraktAccountManager.update.from_dict( trakt_account, {'authorization': { 'oauth': authorization }}, settings=settings) log.info('[Trakt.tv] Token updated for %r', trakt_account) return True
def on_token_refreshed(cls, authorization): log.debug('Authentication - PIN authorization refreshed') # Retrieve trakt account matching this `authorization` with Trakt.configuration.http(retry=True).oauth( token=authorization.get('access_token')): settings = Trakt['users/settings'].get() if not settings: log.warn( 'Authentication - Unable to retrieve account details for authorization' ) return # Retrieve trakt account username from `settings` username = settings.get('user', {}).get('username') if not username: log.warn( 'Authentication - Unable to retrieve username for authorization' ) return None # Find matching trakt account trakt_account = (TraktAccount.select().where( TraktAccount.username == username)).first() if not trakt_account: log.warn( 'Authentication - Unable to find TraktAccount with the username %r', username) # Update oauth credential TraktAccountManager.update.from_dict( trakt_account, {'authorization': { 'oauth': authorization }}) log.info('Authentication - Updated OAuth credential for %r', trakt_account)
def create_trakt_account(cls, account, username): try: return True, TraktAccount.create(account=account, username=username) except (apsw.ConstraintError, peewee.IntegrityError): return False, TraktAccount.get(account=account)