Skip to content

Commit

Permalink
Add .env.example and dynamic project metadata extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Kajiih committed Oct 12, 2024
1 parent 67a82f8 commit 69f4bea
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 19 deletions.
30 changes: 30 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# === Configuration for MusicBrainz2Notion === #
# This file contains environment variables needed to run the application.
# Replace the placeholders with your actual values. Rename this file to `.env`
# or export these variables into your environment.

# === Notion API Token === #
# The API token for accessing your Notion workspace. This token must be generated
# by creating an integration in your Notion account and granting it the necessary permissions.
# It has to be a Notion API token, not a token_v2 from a browser's cookie
# Note: Keep this token secure and do not share it publicly.
NOTION_TOKEN=secret_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# === Notion Database IDs === #
# The IDs of the Notion databases used by the application. These IDs correspond
# to the specific databases where information related to artists, releases, and recordings
# will be stored or retrieved.
# To Find the database id with the link of the database:
# https://www.notion.so/<this_is_the_database_id>?v=<view_id>&pvs=4

# The ID for the database where artist information is stored/managed.
ARTIST_DB_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# The ID for the database where release information is stored/managed.
RELEASE_DB_ID=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

# The ID for the database where recording information is stored/managed.
RECORDING_DB_ID=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

# Note: Ensure these database IDs match the databases in your Notion workspace
# where the application is expected to read from or write to.
14 changes: 12 additions & 2 deletions .todo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Features:
☐ `alt/option + D` toggle Done
☐ `alt/option + C` toggle Cancelled
☐ `alt/option + S` toggle Start
☐ Add CLI
☐ Add config
see https://www.reddit.com/r/rust/comments/ec741e/config_files_vs_env_variables_vs_dotenv/
"In an ideal world, you use a config file (format irrelevant really, though yaml is probably the most common in modern tools - for better and worse) and maintain the ability to override via env vars and via cli flags (typically in that order).

That's how most people will expect an application to behave, anyway."

Bugs:

Expand All @@ -11,6 +17,10 @@ Code Improvement:
Tests:

Final:
☐ Update `pyproject.toml`: keywords, classifiers, etc
☐ Update `pyproject.toml`
☐ Keywords, classifiers, etc
☐ Add entry points
see: https://hatch.pypa.io/1.9/config/metadata/#entry-points
☐ Update `README.md`
☐ Disable library logging. See: https://github.com/Delgan/loguru/issues/349 and https://loguru.readthedocs.io/en/stable/api/logger.html#loguru._logger.Logger.disable
☐ Disable library logging
see: https://github.com/Delgan/loguru/issues/349 and https://loguru.readthedocs.io/en/stable/api/logger.html#loguru._logger.Logger.disable
10 changes: 7 additions & 3 deletions dev/scripts/concat_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from pathlib import Path

source_directory = "src"
output_file = "dev/_concatenated_src.py"
output_file = "dev/_concatenated_src.txt"
exclude_patterns = [
"__pycache__",
"*.pyc",
"*.md",
"*.json",
"py.typed",
"__init__.py",
# "__about__.py",
]


Expand Down Expand Up @@ -80,7 +81,10 @@ def concatenate_files(source_dir: str, output_file: str, exclude_patterns: list[
relative_path = file_path.relative_to(source_path)

# Skip files or directories that match the exclude patterns
if any(file_path.match(pattern) or relative_path.match(pattern) for pattern in exclude_patterns):
if any(
file_path.match(pattern) or relative_path.match(pattern)
for pattern in exclude_patterns
):
continue

# Read the entire content of the file
Expand All @@ -92,7 +96,7 @@ def concatenate_files(source_dir: str, output_file: str, exclude_patterns: list[
continue

# Write the relative path as a header
outfile.write(f"\n# %% === {relative_path} === #\n\n")
outfile.write(f"\n{relative_path} content:\n\n")

# Write the original file content to the output file
outfile.write(file_content)
Expand Down
14 changes: 14 additions & 0 deletions src/musicbrainz2notion/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
"""About MusicBrainz2Notion."""

import pathlib
import tomllib

_PROJECT_ROOT = pathlib.Path(__file__).resolve().parent.parent.parent

# Construct the path to pyproject.toml relative to the project root
_PYPROJECT_PATH = _PROJECT_ROOT / "pyproject.toml"

with _PYPROJECT_PATH.open("rb") as fp:
_PYPROJECT_DATA = tomllib.load(fp)

__version__ = "0.1.0"
__app_name__ = _PYPROJECT_DATA["project"]["name"]
__author__ = _PYPROJECT_DATA["project"]["authors"][0]["name"]
__email__ = _PYPROJECT_DATA["project"]["authors"][0]["email"]
12 changes: 7 additions & 5 deletions src/musicbrainz2notion/musibrainz_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class IncludeOption(StrEnum):
"""
Represents the options for including additional information in API responses.
These options are used with various API calls to retrieve more detailed or related
data about the main entity being queried. Note that this list may be incomplete
and can be expanded based on the API's capabilities.
These options are used with various API calls to retrieve more detailed or
related data about the main entity being queried. Note that this list may be
incomplete and can be expanded based on the API's capabilities.
"""

ALIASES = "aliases"
Expand Down Expand Up @@ -73,7 +73,8 @@ class ReleaseType(StrEnum):
"""
Represents different types of music releases.
This enum helps filter release entities in the API by their type (e.g., albums, singles, live performances).
This enum helps filter release entities in the API by their type (e.g.,
albums, singles, live performances).
"""

ALBUM = "album"
Expand All @@ -97,7 +98,8 @@ class ReleaseStatus(StrEnum):
"""
Represents the status of a release.
This enum is used to filter and categorize releases based on their publication status.
This enum is used to filter and categorize releases based on their
publication status.
"""

OFFICIAL = "official"
Expand Down
33 changes: 24 additions & 9 deletions src/musicbrainz2notion/musicbrainz2notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

import logging
import os
from enum import StrEnum

import musicbrainzngs
from dotenv import load_dotenv
from loguru import logger
from utils import InterceptHandler

from musicbrainz2notion.__about__ import __app_name__, __email__, __version__
from musicbrainz2notion.utils import InterceptHandler


# === Enums === #
class EnvVar(StrEnum):
"""Environment variable keys used in the application."""

NOTION_TOKEN = "NOTION_TOKEN" # noqa: S105
ARTIST_DB_ID = "ARTIST_DB_ID"
RELEASE_DB_ID = "RELEASE_DB_ID"
RECORDING_DB_ID = "RECORDING_DB_ID"


# === Constants === #
ARTIST_DB_ID = ""
RELEASE_DB_ID = ""
RECORDING_DB_ID = ""
load_dotenv()
# TODO: Add CLI for setting environment variables

APP_NAME = "MusicBrainz2Notion"
APP_VERSION = "0.0.1"
APP_CONTACT = "[email protected]"
ARTIST_DB_ID = os.getenv(EnvVar.ARTIST_DB_ID)
RELEASE_DB_ID = os.getenv(EnvVar.RELEASE_DB_ID)
RECORDING_DB_ID = os.getenv(EnvVar.RECORDING_DB_ID)

MB_API_RATE_LIMIT_INTERVAL = 1 # Seconds
MB_API_REQUEST_PER_INTERVAL = 10

NOTION_TOKEN = os.environ["NOTION_TOKEN"] # API token, no token_v2 from cookie
NOTION_TOKEN = os.getenv(EnvVar.NOTION_TOKEN)

# Set up logging with Loguru
logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True)


musicbrainzngs.set_useragent(APP_NAME, APP_VERSION, APP_CONTACT)
musicbrainzngs.set_useragent(__app_name__, __version__, __email__)
musicbrainzngs.set_rate_limit(MB_API_RATE_LIMIT_INTERVAL, MB_API_REQUEST_PER_INTERVAL)
logger.info("MusicBrainz client initialized.")

0 comments on commit 69f4bea

Please sign in to comment.