Skip to content

Commit

Permalink
my.hackernews.harmonic: use orjson + add __hash__ for Saved object
Browse files Browse the repository at this point in the history
plus some minor cleanup
  • Loading branch information
karlicoss committed Nov 7, 2023
1 parent 4ac3bbb commit 19353e9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions my/hackernews/harmonic.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""
[[https://play.google.com/store/apps/details?id=com.simon.harmonichackernews][Harmonic]] app for Hackernews
"""
REQUIRES = ['lxml']
REQUIRES = ['lxml', 'orjson']

from dataclasses import dataclass
from datetime import datetime, timezone
import json
import orjson
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional, Sequence, TypedDict, cast

from lxml import etree
from more_itertools import unique_everseen, one
from more_itertools import one

from my.core import (
Paths,
Expand All @@ -21,15 +21,16 @@
make_logger,
stat,
)
from my.core.common import unique_everseen
import my.config
from .common import hackernews_link, SavedBase

from my.config import harmonic as user_config

logger = make_logger(__name__)


@dataclass
class harmonic(user_config):
class harmonic(my.config.harmonic):
export_path: Paths


Expand Down Expand Up @@ -76,6 +77,10 @@ def title(self) -> str:
def hackernews_link(self) -> str:
return hackernews_link(self.uid)

def __hash__(self) -> int:
# meh. but seems like the easiest and fastest way to hash a dict?
return hash(orjson.dumps(self.raw))


_PREFIX = 'com.simon.harmonichackernews.KEY_SHARED_PREFERENCES'

Expand All @@ -95,7 +100,7 @@ def _saved() -> Iterator[Res[Saved]]:
cached: Dict[str, Cached] = {}
for sid in cached_ids:
res = one(cast(List[Any], tr.xpath(f'//*[@name="{_PREFIX}_CACHED_STORY{sid}"]')))
j = json.loads(res.text)
j = orjson.loads(res.text)
cached[sid] = j

res = one(cast(List[Any], tr.xpath(f'//*[@name="{_PREFIX}_BOOKMARKS"]')))
Expand All @@ -112,7 +117,7 @@ def _saved() -> Iterator[Res[Saved]]:


def saved() -> Iterator[Res[Saved]]:
yield from unique_everseen(_saved())
yield from unique_everseen(_saved)


def stats() -> Stats:
Expand Down

0 comments on commit 19353e9

Please sign in to comment.