Skip to content

Commit

Permalink
Allow configuring TMDB token
Browse files Browse the repository at this point in the history
Add 'configure' subcommand, with ability to set a TMDB token. Adjust
configuration parsing for dealing with the token.
  • Loading branch information
gevhaz committed Nov 26, 2023
1 parent ef1022d commit e3994c7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ $ pip3 install seasonwatch-<the version you want to install>-py3-none-any.whl

## Usage

### Initial configuration

In order to use Seasonwatch, you need to have a TMDB API Read Access Token.
Getting one is free, and you get it by registering on
<https://www.themoviedb.org> and then creating an API key according to the
[documentation](https://developer.themoviedb.org/docs). Creating an API key also
gives you an API Read Access Token. Once you have it, manually edit your
configuration file (usually `~/.config/seasonwatchrc`) or running:

```command
$ seasonwatch configure
TMDB API Read Access Token: <your token, then Enter>
Testing token...
Token is valid!
Write new config file, losing all comments? (Y/n): <Y>
Successfully set TMDB token!
```

### Adding new shows or artists

Start by adding a TV show or an artist. For adding a TV show, run:
Expand Down
50 changes: 47 additions & 3 deletions seasonwatch/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from configparser import ConfigParser

import gi
import requests
from prettytable.prettytable import SINGLE_BORDER
from requests.exceptions import HTTPError

gi.require_version("Notify", "0.7")

Expand Down Expand Up @@ -35,6 +37,51 @@ def main() -> int:
(Constants.CONFIG_PATH).touch()

args = Cli.parse()

config = ConfigParser()
config.read(Constants.CONFIG_PATH)
if not config.has_section("Tokens"):
config.add_section("Tokens")
# Appending should preserve comments that the user has written.
with open(Constants.CONFIG_PATH, mode="a") as fp:
config.write(fp)

tmdb_token: str | None = config.get("Tokens", "tmdb_token", fallback=None)
if tmdb_token is None and args.subparser_name != "configure":
print("You need to set a TMDb token. Run 'seasonwatch configure'")
sys.exit(1)

if args.subparser_name == "configure":
new_tmdb_token = input("TMDB API Read Access Token: ")
print("Testing token...")
try:
requests.get(
f"{Constants.API_BASE_URL}/movie/11",
headers={
"accept": "application/json",
"Authorization": f"Bearer {new_tmdb_token}",
},
).raise_for_status()
except HTTPError as e:
if e.response is not None and e.response.status_code == 401:
print("Invalid token. Please correct it.", file=sys.stderr)
else:
print(f"Error testing token: '{e}'", file=sys.stderr)
sys.exit(1)
print("Token is valid!")
config.set(section="Tokens", option="tmdb_token", value=new_tmdb_token)
overwrite_config = input("Write new config file, losing all comments? (Y/n): ")
if overwrite_config == "n" or overwrite_config == "N":
print(
f"Please manually update '{Constants.CONFIG_PATH}' with 'tmdb_token' "
"under [Tokens]"
)
sys.exit(0)
with open(Constants.CONFIG_PATH, mode="w") as fp:
config.write(fp)
print("Successfully set TMDB token!")
sys.exit(0)

if args.subparser_name == "tv":
if args.add:
Configure.add_series()
Expand All @@ -53,9 +100,6 @@ def main() -> int:
ia: IMDbHTTPAccessSystem = Cinemagoer(accessSystem="https")
watcher = MediaWatcher()

config = ConfigParser()
config.read(Constants.CONFIG_PATH)

try:
watcher.check_for_new_seasons(ia)
except SeasonwatchException as e:
Expand Down
5 changes: 5 additions & 0 deletions seasonwatch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def parse() -> Namespace:
help="Subcommand for working with TV shows",
)

subparsers.add_parser(
"configure",
help="Configure Seasonwatch for use",
)

tv.add_argument(
"-r",
"--remove",
Expand Down
2 changes: 1 addition & 1 deletion seasonwatch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class Configure:
"""Class for modifying the artists and shows to check."""
"""Class for modifying the shows to check."""

@staticmethod
def step_up_series() -> None:
Expand Down
4 changes: 4 additions & 0 deletions seasonwatch/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from pathlib import Path
from typing import Final


class Constants:
Expand All @@ -22,3 +23,6 @@ class Constants:

SERIES_TABLE = "series"
MOVIES_TABLE = "movies"

API_VERSION: Final = 3
API_BASE_URL: Final = f"https://api.themoviedb.org/{API_VERSION}"

0 comments on commit e3994c7

Please sign in to comment.