Skip to content

Question: How to run auth automatically with no user interaction? #78

@nbn22385

Description

@nbn22385

Hello,

I have a script that runs as a daily cron job to send me a notification of any new episodes airing that day. Currently, when running the script on the command line, each time I must visit the authorization URL to retrieve a code, then input the code, and the script resumes.

After reading the Getting Started docs, I believe setting store=True would eliminate the need for this to be done each time my script runs. I would like to know how to have it run without user interaction. I understand the tokens expire every 24 hours, but I think the refresh token logic is already baked into the library.

Here is the script with sensitive values removed:

from trakt import init
from trakt.calendar import MyShowCalendar
import trakt.core
import datetime

def get_today_shows():
    calendar = MyShowCalendar(days=1)
    if not calendar:
        print("No shows airing today.")
        return

    for item in calendar:
        print(f"{item.show} - {item.title} (S{item.season:02}E{item.episode:02})")

if __name__ == '__main__':
    trakt.core.APPLICATION_ID = 'APPLICATION_ID_REMOVED'
    trakt.core.AUTH_METHOD = trakt.core.OAUTH_AUTH

    init('USERNAME_REMOVED', client_id='CLIENT_ID_REMOVED', client_secret='CLIENT_SECRET_REMOVED', store=True)
    
    # Is there a shorter init that can be called after the initial authorization?
    # init('USERNAME_REMOVED', store=True)

    print(trakt.core.CLIENT_ID) # prints None
    print(trakt.core.CLIENT_SECRET) # prints None
    print(trakt.core.OAUTH_TOKEN) # prints None

    get_today_shows()

I can see the ~/.pytrakt.json file is created with valid values, but when I print them in the script (print(trakt.core.XYZ)) the values resolve to None so I'm not sure they are being loaded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions