jensb89 / Netflix-to-Trakt-Import

Synchronize/Import the viewing history from Netflix to trakt.tv
97 stars 21 forks source link

Move local configuration to a separate file that can be ignored by git #41

Closed ricky closed 1 year ago

ricky commented 1 year ago

Motivations:

  1. Be able to run tests and commit changes without stashing modifications to config.py.
  2. Prevent the accidental publication of API keys to :octocat:.

I kept the same interface as the existing config object to make this easier to review. We could take it a step further and use the structured object directly. e.g.:

# Setup logging
logging.basicConfig(**config.Logging)

# Connect to TMDB
tmdb = TMDb()
tmdb.api_key = config.TMDB.api_key
tmdb.language = config.TMDB.language
tmdb.debug = config.TMDB.debug

# Setup Trakt
traktIO = TraktIO(**config.Trakt)
# etc...

If you want to create a config.ini from your existing config.py before merging these changes, you can use this script:

import config
import configparser

outputFilename="config.ini"

newConfig = configparser.ConfigParser()
newConfig.optionxform=str

newConfig['Logging'] = {
    'filename': config.LOG_FILENAME,
    'level': config.LOG_LEVEL,
}

newConfig['Netflix'] = {
    'viewing_history_filename': config.VIEWING_HISTORY_FILENAME,
    'viewing_history_datetime_format': config.CSV_DATETIME_FORMAT.replace('%', '%%'),
    'viewing_history_delimiter': config.CSV_DELIMITER,
}

newConfig['TMDB'] = {
    'api_key': config.TMDB_API_KEY,
    'language': config.TMDB_LANGUAGE,
    'debug': config.TMDB_DEBUG,
    'strict': config.TMDB_SYNC_STRICT,
    'episode_language_search': config.TMDB_EPISODE_LANGUAGE_SEARCH,
}

newConfig['Trakt'] = {
    'id': config.TRAKT_API_CLIENT_ID,
    'secret': config.TRAKT_API_CLIENT_SECRET,
    'dry_run': config.TRAKT_API_DRY_RUN,
    'page_size': config.TRAKT_API_SYNC_PAGE_SIZE,
}

print(f"Writing {outputFilename}...")
with open(outputFilename, "w") as conf:
    newConfig.write(conf)

print(f"Done. Review {outputFilename} for correctness.")