Skip to content

Commit

Permalink
refactor: remove arrow as much as possible, use datetime,date if poss…
Browse files Browse the repository at this point in the history
…ible

convert types for hashtable queries
  • Loading branch information
datawhores committed Nov 29, 2024
1 parent 1659210 commit 84fa781
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cyberdrop_dl/scraper/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def is_outside_date_range(scrape_item: ScrapeItem, before: arrow, after: arrow)
item_date = scrape_item.completed_at or scrape_item.created_at
if not item_date:
return False
if (after and arrow.get(item_date) < after) or (before and arrow.get(item_date) > before):
if (after and arrow.get(item_date).date() < after) or (before and arrow.get(item_date).date() > before):
skip = True

return skip
Expand Down
7 changes: 5 additions & 2 deletions cyberdrop_dl/scraper/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from dataclasses import Field
from pathlib import Path
from typing import TYPE_CHECKING
from datetime import date,datetime

import aiofiles
import arrow

from yarl import URL

from cyberdrop_dl import __version__ as current_version
Expand Down Expand Up @@ -184,8 +186,9 @@ async def load_failed_links(self) -> None:

async def load_all_links(self) -> None:
"""Loads all links from database."""
after=self.manager.parsed_args.cli_only_args.completed_after or arrow.get(0)
before=self.manager.parsed_args.cli_only_args.completed_before or arrow.now().shift(days=1)
after=self.manager.parsed_args.cli_only_args.completed_after or date(1970, 1, 1)

before=self.manager.parsed_args.cli_only_args.completed_before or datetime.now().date()
entries = await self.manager.db_manager.history_table.get_all_items(
after,
before,
Expand Down
14 changes: 5 additions & 9 deletions cyberdrop_dl/utils/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from argparse import _ArgumentGroup as ArgGroup
from pathlib import Path
from typing import Self
from datetime import date


import arrow
from pydantic import BaseModel, Field, ValidationError, computed_field, field_validator, model_validator

Check failure on line 9 in cyberdrop_dl/utils/args.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

cyberdrop_dl/utils/args.py:9:73: F401 `pydantic.field_validator` imported but unused

from cyberdrop_dl import __version__
Expand All @@ -20,9 +21,9 @@ def _check_mutually_exclusive(group: set, msg: str) -> None:

class CommandLineOnlyArgs(BaseModel):
links: list[HttpURL] = Field([], description="link(s) to content to download (passing multiple links is supported)")
appdata_folder: Path | None = Field(None, description="AppData folder path")
completed_after: str | None = Field(None, description="only download completed downloads at or after this date")
completed_before: str | None = Field(None, description="only download completed downloads at or before this date")
appdata_folder: Path | None = Field(None,description="AppData folder path")
completed_after: date| None = Field(None,description="only download completed downloads at or after this date")
completed_before: date | None = Field(None,description="only download completed downloads at or before this date")
config: str | None = Field(None, description="name of config to load")
config_file: Path | None = Field(None, description="path to the CDL settings.yaml file to load")
download: bool = Field(False, description="skips UI, start download inmediatly")
Expand All @@ -44,11 +45,6 @@ def retry_any(self) -> bool:
def multiconfig(self) -> bool:
return self.config and self.config.casefold() == "all"

@field_validator("completed_after", "completed_before", mode="after")
@staticmethod
def arrow_date(value: int) -> arrow.Arrow | None:
return None if not value else arrow.get(value)

@model_validator(mode="after")
def mutually_exclusive(self) -> Self:
group1 = {self.retry_all, self.retry_failed, self.retry_maintenance}
Expand Down
17 changes: 9 additions & 8 deletions cyberdrop_dl/utils/database/tables/hash_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

from cyberdrop_dl.utils.database.table_definitions import create_files, create_hash, create_temp_hash
from cyberdrop_dl.utils.logger import log
from datetime import datetime

if TYPE_CHECKING:

Check failure on line 13 in cyberdrop_dl/utils/database/tables/hash_table.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

cyberdrop_dl/utils/database/tables/hash_table.py:1:1: I001 Import block is un-sorted or un-formatted
import aiosqlite
from yarl import URL
import arrow

console = Console()

Expand Down Expand Up @@ -55,9 +55,10 @@ async def transer_old_hash_table(self):
referer = old_result[5]
hash_type = "md5"
file_date = (
Path(folder, dl_name).stat().st_mtime
int(Path(folder, dl_name).stat().st_mtime)
if Path(folder, dl_name).exists()
else int(arrow.now().float_timestamp)
else int( int(datetime.now().timestamp())
)
)
await cursor.execute(
"INSERT OR IGNORE INTO files (folder, download_filename, original_filename, file_size, referer,date) VALUES (?,?,?,?,?,?);",
Expand All @@ -84,7 +85,7 @@ async def get_file_hash_exists(self, full_path: Path | str, hash_type: str) -> s
# Extract folder, filename, and size from the full path
path = Path(full_path).absolute()
folder = str(path.parent)
filename = path.name
filename = str(path.name)

# Connect to the database
cursor = await self.db_conn.cursor()
Expand Down Expand Up @@ -154,7 +155,7 @@ async def insert_or_update_hash_db(
async def insert_or_update_hashes(self, hash_value, hash_type, file):
try:
full_path = Path(file).absolute()
download_filename = full_path.name
download_filename = str(full_path.name)
folder = str(full_path.parent)
cursor = await self.db_conn.cursor()

Expand Down Expand Up @@ -186,9 +187,9 @@ async def insert_or_update_file(self, original_filename, referer, file):
try:
referer = str(referer)
full_path = Path(file).absolute()
file_size = full_path.stat().st_size
file_date = full_path.stat().st_mtime
download_filename = full_path.name
file_size = int(full_path.stat().st_size)
file_date = int(full_path.stat().st_mtime)
download_filename = str(full_path.name)
folder = str(full_path.parent)

cursor = await self.db_conn.cursor()
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/utils/database/tables/history_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async def get_all_items(self, after: datetime, before: datetime) -> Iterable[Row
FROM media
WHERE COALESCE(completed_at, '1970-01-01') BETWEEN ? AND ?
ORDER BY completed_at DESC;""",
(after.format("YYYY-MM-DD"), before.format("YYYY-MM-DD")),
(after.strftime("%Y-%m-%d"), before.strftime("%Y-%m-%d")),
)
return await result.fetchall()

Expand Down

0 comments on commit 84fa781

Please sign in to comment.