Skip to content

Commit

Permalink
πŸš€ Add track shuffle option to album posters (#21)
Browse files Browse the repository at this point in the history
- Added shuffle to `get_album`
- Fixed `ImagePathValidator` by resolving path
- Added `conf.py` for easier config access
- Cleaned up Beatprints CLI by removing unnecessary code
- Fixed issue with cover image not showing up on the poster when it should
  • Loading branch information
TrueMyst committed Dec 25, 2024
1 parent d79644b commit 997b0c3
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 224 deletions.
19 changes: 10 additions & 9 deletions BeatPrints/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ def scannable(id: str, darktheme: bool = False, is_album: bool = False) -> Image
return scan_code.resize(S_SPOTIFY_CODE, Image.Resampling.BICUBIC)


def cover(image_url: str, path: Optional[str]) -> Image.Image:
def cover(image_url: str, image_path: Optional[str]) -> Image.Image:
"""
Fetches and processes an image from a URL or local path.
Args:
image_url (str): The image URL.
path (Optional[str]): The local image path.
image_path (Optional[str]): The local image path.
Returns:
Image.Image: The processed image.
Expand All @@ -171,16 +171,17 @@ def cover(image_url: str, path: Optional[str]) -> Image.Image:
FileNotFoundError: If the local image path does not exist.
"""

if image_url:
img = Image.open(BytesIO(requests.get(image_url).content))
else:
image_path = Path(str(path)).expanduser().resolve()
if image_path:
path = Path(image_path).expanduser().resolve()

if image_path.exists():
img = crop(image_path)
else:
if not path.exists():
raise FileNotFoundError(f"The specified path '{path}' does not exist.")

img = crop(path)

else:
img = Image.open(BytesIO(requests.get(image_url).content))

# Apply the magic filter and resize the image for the cover
return magicify(img.resize(S_COVER))

Expand Down
8 changes: 3 additions & 5 deletions BeatPrints/poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import os
import random

from pathlib import Path
from typing import Optional, Union
Expand Down Expand Up @@ -56,8 +55,7 @@ def _add_common_text(
color,
write.font("Bold"),
S_HEADING,
)
# Add artist name
) # Add artist name
write.text(
draw,
C_ARTIST,
Expand Down Expand Up @@ -186,8 +184,8 @@ def album(
self._add_common_text(draw, metadata, color)

# Shuffle tracks and optionally index them
tracks = metadata.tracks[:]
random.shuffle(tracks)
tracks = metadata.tracks

if indexing:
tracks = [f"{i + 1}. {track}" for i, track in enumerate(tracks)]

Expand Down
10 changes: 9 additions & 1 deletion BeatPrints/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Provides functionality related to interacting with the Spotify API.
"""

import random
import requests
import datetime

Expand Down Expand Up @@ -173,13 +174,16 @@ def get_track(self, query: str, limit: int = 6) -> List[TrackMetadata]:

return tracklist

def get_album(self, query: str, limit: int = 6) -> List[AlbumMetadata]:
def get_album(
self, query: str, limit: int = 6, shuffle: bool = True
) -> List[AlbumMetadata]:
"""
Searches for albums based on a query and retrieves their metadata, including track listing.
Args:
query (str): The search query for the album (e.g. album name - artist).
limit (int, optional): Maximum number of albums to retrieve. Defaults to 6.
shuffle (bool, optional): Shuffle the tracks in the list. Defaults to False.
Returns:
List[AlbumMetadata]: A list of album metadata with track listings.
Expand Down Expand Up @@ -215,6 +219,10 @@ def get_album(self, query: str, limit: int = 6) -> List[AlbumMetadata]:
for track in album_details.get("tracks", {}).get("items", [])
]

# Shuffle tracks if true
if shuffle:
random.shuffle(tracks)

# Create AlbumMetadata object with formatted data
metadata = {
"name": album["name"],
Expand Down
1 change: 1 addition & 0 deletions cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .conf import *
from .exutils import *
from .validate import *
20 changes: 20 additions & 0 deletions cli/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import toml
import platform

# Determine the config path based on the platform
config_dir = (
os.getenv("APPDATA")
if platform.system() == "Windows"
else os.path.expanduser("~/.config")
)

config_path = os.path.join(str(config_dir), "BeatPrints", "config.toml")

with open(config_path) as config:
config = toml.load(config)

POSTERS_DIR = config["general"]["output_directory"]
SEARCH_LIMIT = config["general"]["search_limit"]
CLIENT_ID = config["credentials"]["client_id"]
CLIENT_SECRET = config["credentials"]["client_secret"]
Loading

0 comments on commit 997b0c3

Please sign in to comment.