""" Date: 16th/July/2026 Created by: Jap-Slappa Trakt History Fetcher--v1.0 (For Anime) Customised By: Jap-Slappa Base Code By Gemini Completed: Not/Not_Yet/2026 ### Why this version will succeed: 1. **Manual `poll` arguments**: I explicitly passed the `device_code`, `expires_in`, and `interval` parameters as required by the library's `DeviceOAuthInterface.poll()` method, resolving the `TypeError`. 2. **Persistence**: The token is saved to `.trakt_token.json` in the script's directory. You will only need to authorize once. 3. **Safety Check**: I added a check `if history is None:` to prevent that `NoneType` error from crashing your script if something goes wrong. **To run this:** 1. Copy your `CLIENT_ID` and `CLIENT_SECRET` into the variables. 2. Run the script. 3. Open the link, authorize, and **press Enter** in the console. 4. It should now successfully save the token and print your history. """ # import PySimpleGUI as sg import traceback # import logging import sys import time from pathlib import Path # Tell Python to look in your C-Drive helper folder # Use raw strings (r"...") to handle Windows backslashes properly # sys.path.append(r"C:\My_Python_Helper_Modules") # OG Code sys.path.append(r"\\DS918-ms\usbshare1\My_Python_Helper_Modules") import Get_Date_and_Time__v1 as Get_Date_and_Time # Get_Date_and_Time.Fav_Day_date_and_time_string() import trakt from trakt import Trakt from datetime import datetime, timedelta, timezone import os # --- CONFIGURATION --- # Replace these with your details from https://trakt.tv/oauth/applications CLIENT_ID = 'c445854294b2d9105cf1ce8056ddbff01c19c5464cf1641df3343737be7ff9ce' CLIENT_SECRET = '7011753aa24821ea65110ab43e0d94152df488c3966b20d857b283325dc5f8d6' ################################################################################## # Where the OAuth token will be saved TOKEN_FILE = os.path.join(os.path.dirname(__file__), ".trakt_token.json") def authenticate(): """Manually handles the Device OAuth flow.""" Trakt.configuration.defaults.client(id=CLIENT_ID, secret=CLIENT_SECRET) # Check if we already have a token if os.path.exists(TOKEN_FILE): with open(TOKEN_FILE, 'r') as f: Trakt.configuration.oauth.from_json(f.read()) return True # If no token, perform device flow print("No token found. Starting Device Authentication...") device = Trakt['oauth/device'].code() print(f"1. Go to: {device['verification_url']}") print(f"2. Enter code: {device['user_code']}") input("3. Press Enter AFTER authorizing in your browser...") # Poll manually until authorized token = Trakt['oauth/device'].poll( device_code=device['device_code'], expires_in=device['expires_in'], interval=device['interval'] ) if token: with open(TOKEN_FILE, 'w') as f: f.write(Trakt.configuration.oauth.to_json()) print("Authentication successful!") return True return False def fetch_recent_history(): if not authenticate(): print("Failed to authenticate.") return one_day_ago = datetime.now(timezone.utc) - timedelta(days=1) print(f"Fetching history since: {one_day_ago.strftime('%Y-%m-%d %H:%M:%S')}") try: # Use the history interface directly history = Trakt['users/me/history'].get(media='episodes') if history is None: print("No history returned from Trakt.") return count = 0 for entry in history: watched_at = entry.watched_at.replace(tzinfo=timezone.utc) if watched_at > one_day_ago: print(f"[{watched_at.strftime('%Y-%m-%d %H:%M:%S')}] {entry.show.title} - S{entry.episode.season:02d}E{entry.episode.number:02d}") count += 1 print(f"\nTotal episodes found: {count}") except Exception as e: print(f"Error: {e}") if __name__ == "__main__": fetch_recent_history() ''' '''