Skip to content

Commit

Permalink
Better obfuscation during demo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Aug 1, 2023
1 parent f3e7f60 commit 7ddf2ba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
18 changes: 9 additions & 9 deletions calibre-plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .libby import LibbyClient
from .libby.client import LibbyMediaTypes
from .magazine_download_utils import extract_asin, extract_isbn
from .utils import PluginColors, PluginIcons
from .utils import PluginColors, PluginIcons, obfuscate_date, obfuscate_name

# noinspection PyUnreachableCode
if False:
Expand Down Expand Up @@ -62,11 +62,11 @@ def get_media_title(


def truncate_for_display(text, text_length=30):
if DEMO_MODE:
return "*" * min(len(text), text_length)
if len(text) <= text_length:
return text
return text[:text_length] + "…"
return text if not DEMO_MODE else obfuscate_name(text)
return (
text[:text_length] if not DEMO_MODE else obfuscate_name(text[:text_length])
) + "…"


LOAN_TYPE_TRANSLATION = {"ebook": _("ebook"), "magazine": _("magazine")} # not used
Expand Down Expand Up @@ -300,13 +300,13 @@ def data(self, index, role):
return dt_value.isoformat()
if DEMO_MODE:
return format_date(
dt_value.replace(month=12, day=31),
obfuscate_date(dt_value, day=31, month=12),
tweaks["gui_timestamp_display_format"],
)
return format_date(dt_value, tweaks["gui_timestamp_display_format"])
if col == 3:
if DEMO_MODE:
return "*" * len(card["advantageKey"])
return obfuscate_name(card["advantageKey"])
return card["advantageKey"]
if col == 4:
loan_format = LibbyClient.get_loan_format(
Expand Down Expand Up @@ -467,15 +467,15 @@ def data(self, index, role):
return placed_or_expire_dt.isoformat()
if DEMO_MODE:
return format_date(
placed_or_expire_dt.replace(month=1, day=1),
obfuscate_date(placed_or_expire_dt, month=1, day=1),
tweaks["gui_timestamp_display_format"],
)
return format_date(
placed_or_expire_dt, tweaks["gui_timestamp_display_format"]
)
if col == 3:
if DEMO_MODE:
return "*" * len(card["advantageKey"])
return obfuscate_name(card["advantageKey"])
return card["advantageKey"]
if col == 4:
hold_format = LibbyClient.get_loan_format(
Expand Down
29 changes: 29 additions & 0 deletions calibre-plugin/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import random
from collections import namedtuple
from datetime import datetime
from enum import Enum
from typing import Dict

Expand All @@ -12,6 +14,33 @@
OD_IDENTIFIER = "odid"


def obfuscate_date(dt: datetime, day=None, month=None, year=None):
return dt.replace(day=day or 1, month=month or 1, year=year or datetime.now().year)


def obfuscate_name(name: str, offset=5, min_word_len=1, max_word_len=8):
obfuscated = []
for n in name.split(" "):
min_n = max(min_word_len, len(n) - offset)
max_n = min(max_word_len, len(n) + offset)
if min_n == max_n:
min_n = min_word_len
max_n = max_word_len
choices = range(min_n, max_n, 1 if max_n > min_n else -1)
obfuscated.append(
"*" * random.choice(choices or range(min_word_len, max_word_len))
)
return " ".join(obfuscated)


def obfuscate_int(value: int, offset=5, min_value=0, max_val=30):
return random.choice(
range(
max(min_value, min(value, max_val) - offset), min(max_val, value + offset)
)
)


def generate_od_identifier(media: Dict, library: Dict) -> str:
"""
Generates the OverDrive Link identifier.
Expand Down

0 comments on commit 7ddf2ba

Please sign in to comment.