Skip to content

Commit

Permalink
remove single ticks from img download urls
Browse files Browse the repository at this point in the history
merged cover url logic into 1 place
  • Loading branch information
Jake Lunn committed Oct 22, 2023
1 parent 15b8f5e commit 7a23cc6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
14 changes: 4 additions & 10 deletions modules/local_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import List
from dataclasses import dataclass
from argparse import Namespace
from modules.onepacenet import get_arcs, get_image, extract_title
from modules.onepacenet import get_arcs, get_image, extract_title, MediaType

REGEX = re.compile(
r"\[(One Pace)\]\[(?P<Chapters>.+?)\]\s(?P<Arc>.+?)(?P<Episode>\d{1,2})?\s\[(?P<Quality>\d{3,4}p)\]\[(?P<Hash>.+?)\]\.mkv"
Expand Down Expand Up @@ -44,8 +44,7 @@ def __init__(
arc_title: str,
source_file: FileInfo,
target_directory: str,
target_file=None,
cover_url=None,
target_file=None
):
self.title = title
self.episode_number = episode_number
Expand All @@ -54,9 +53,6 @@ def __init__(
self.source_file = source_file
self.target_directory = target_directory
file_name = f"One.Pace.{arc_title}.E{episode_number:02d}.{resolution}.mkv"
self.cover_url = (
cover_url or f"/images/episodes/cover-{title.replace(' ', '-')}.jpg".lower()
)
self.target_file = target_file or FileInfo(
name=file_name,
full_name=os.path.join(target_directory, file_name),
Expand All @@ -75,7 +71,7 @@ def copy_cover_to_destination(self):
file_full_name = os.path.join(self.target_directory, file_name)
if os.path.exists(file_full_name):
return
image_data = get_image(self.cover_url)
image_data = get_image(MediaType.EPISODE, self.title)
with open(file_full_name, "wb") as file:
file.write(image_data)
print(f"Copied cover for {self.title} from onepace.net to target directory")
Expand All @@ -100,7 +96,6 @@ def __extract_group_value_from_match(match, group_name, optional: bool = False)
group_str = group_str.strip()
return group_str


def search_directory(directory) -> List[FileInfo]:
"""Search a directory for One Pace episodes"""
files = []
Expand Down Expand Up @@ -167,10 +162,9 @@ def copy_arc_cover_to_destination(target_directory: str, arc):
file_name = f"Season{part:02d}.jpg"
target_directory = os.path.join(target_directory, f"Season {part:02d}")
file_full_name = os.path.join(target_directory, file_name)
url = f"/images/arcs/cover-{title.replace(' ', '-')}-arc.jpg".lower()
if os.path.exists(file_full_name):
return # Already exists
image_data = get_image(url)
image_data = get_image(MediaType.ARC, title)
with open(file_full_name, "wb") as file:
file.write(image_data)
print(f"Copied cover for {title} from onepace.net to target directory")
Expand Down
16 changes: 15 additions & 1 deletion modules/onepacenet.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
"""Module for calling the onepace.net API to retrieve episode information"""
import re
import requests
from enum import Enum

BUILD_ID_REGEX = re.compile(r"\"buildId\".+?\"(?P<BuildId>.+?)\"")

MediaType = Enum('MediaType', ['ARC', 'EPISODE'])

class TranslationNotFoundException(Exception):
"""Exception for when a translation is not found"""

def __generate_cover_url(media_type: MediaType, title: str):
"""Generate a cover url given a title"""
titlef = title
titlef = titlef.replace(' ', '-')
titlef = titlef.replace('\'', '')
media_area = 'episodes'
if media_type == MediaType.ARC:
titlef = titlef + "-arc"
media_area = 'arcs'
cover_url = f"/images/{media_area}/cover-{titlef}.jpg".lower()
return cover_url

def get_build() -> str:
"""Get the Build Id from onepace.net"""
Expand All @@ -27,8 +40,9 @@ def get_arcs() -> list:
return arcs


def get_image(url: str) -> bytes:
def get_image(media_type: MediaType, title: str) -> bytes:
"""Download Image from the One Pace Servers"""
url = __generate_cover_url(media_type, title)
response = requests.get(
f"https://onepace.net/_next/image?url={url}&w=828&q=75", timeout=30
)
Expand Down

0 comments on commit 7a23cc6

Please sign in to comment.