Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
adogecheems committed Sep 16, 2024
1 parent 523a9f4 commit 3eaff16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
14 changes: 4 additions & 10 deletions anisearch/AniSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ def set_timefmt(self, timefmt: str) -> None:
Args:
- timefmt: Time format string
"""
try:
time.strftime(timefmt, time.localtime())
self._timefmt = timefmt
except ValueError:
raise ValueError(f"Invalid time format: {timefmt}")
time.strftime(timefmt, time.localtime())
self._timefmt = timefmt

def reset(self) -> None:
"""Reset the search object."""
Expand Down Expand Up @@ -113,11 +110,7 @@ def size_format(self, unit: str = 'MB') -> None:
if not self.if_selected:
raise ValueError("No item selected. Please use select() method first.")

try:
self.anime.size_format(unit)
except Exception as e:
log.error(f"Size format conversion failed: {str(e)}")
raise
self.anime.size_format(unit)

def save_csv(self, filename: str) -> None:
"""
Expand Down Expand Up @@ -145,6 +138,7 @@ def save_csv(self, filename: str) -> None:
log.error(f"Failed to save CSV: {str(e)}")
raise


if __name__ == "__main__":
import doctest

Expand Down
3 changes: 2 additions & 1 deletion anisearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import logging

LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
Expand Down Expand Up @@ -26,4 +27,4 @@ def setup_logger(name: str = "global", level: int = logging.DEBUG) -> logging.Lo

log = setup_logger()

eval("from .AniSearch import AniSearch")
AniSearch = importlib.import_module('.AniSearch', package=__name__).AniSearch
37 changes: 14 additions & 23 deletions anisearch/plugins/Anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
'TIB': 1099511627776
}


class Anime:
def __init__(self, time: str, title: str, size: str, magnet: str):
"""
Expand All @@ -43,18 +42,12 @@ def size_format(self, unit: str = 'MB') -> None:
Args:
unit (str, optional): The target unit. Defaults to 'MB'.
Raises:
ValueError: If the size string is invalid or an invalid storage unit is provided.
"""
try:
value, pre_unit = self.extract_value_and_unit(self.size)
value, pre_unit = self.extract_value_and_unit(self.size)
if pre_unit is not None and value is not None:
if pre_unit.upper() != unit.upper():
value = self.convert_byte(value, pre_unit, unit)
self.size = f"{value}{unit}"
except ValueError as e:
log.critical(f"Size format error: {e}")
raise

@staticmethod
def convert_byte(value: float, from_unit: str, to_unit: str) -> float:
Expand All @@ -68,18 +61,15 @@ def convert_byte(value: float, from_unit: str, to_unit: str) -> float:
Returns:
float: The converted value.
Raises:
ValueError: If an invalid storage unit is provided.
"""
try:
from_factor = conversion_factors[from_unit.upper()]
to_factor = conversion_factors[to_unit.upper()]
return round(value * (from_factor / to_factor), 2)
except KeyError as e:
invalid_unit = e.args[0] if e.args else 'unknown'
raise ValueError(f"Convert: invalid storage unit '{invalid_unit}'") from e

return round(value * (from_factor / to_factor), 2)
log.error(f"Convert: invalid storage unit '{invalid_unit}'")
return None

@staticmethod
def extract_value_and_unit(size: str) -> Tuple[float, str]:
Expand All @@ -91,9 +81,6 @@ def extract_value_and_unit(size: str) -> Tuple[float, str]:
Returns:
Tuple[float, str]: The extracted value and unit.
Raises:
ValueError: If the size string is invalid.
"""
match = size_pattern.match(size)

Expand All @@ -102,7 +89,8 @@ def extract_value_and_unit(size: str) -> Tuple[float, str]:
unit = match.group(2)
return value, unit
else:
raise ValueError(f"Extract: invalid size '{size}'")
log.error(f"Extract: invalid size '{size}'")
return None, None

def __eq__(self, value: object) -> bool:
"""
Expand All @@ -118,9 +106,12 @@ def __eq__(self, value: object) -> bool:
return False

try:
return magnet_hash_pattern.search(self.magnet).group(1).lower() == magnet_hash_pattern.search(value.magnet).group(1).lower()
return (
magnet_hash_pattern.search(self.magnet).group(1).lower() ==
magnet_hash_pattern.search(value.magnet).group(1).lower()
)
except AttributeError:
log.critical("Magnet hash extraction failed.")
log.error("Magnet hash extraction failed.")
return False

def __ne__(self, other) -> bool:
Expand All @@ -145,9 +136,9 @@ def __str__(self) -> str:
try:
hash_value = magnet_hash_pattern.search(self.magnet).group(1)
except AttributeError:
log.critical("Magnet hash extraction failed.")
log.error("Magnet hash extraction failed.")
hash_value = "unknown"
return f"Anime'{self.title}' with hash {hash_value}"
return f"Anime '{self.title}' with hash {hash_value}"

def __repr__(self) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='Anisearch-lib',
version='1.3.3',
version='1.3.4',
packages=find_packages(exclude=['tests*']),
install_requires=requirements,
entry_points={
Expand Down

0 comments on commit 3eaff16

Please sign in to comment.