Skip to content

Commit

Permalink
refactor: parse category optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Oct 26, 2024
1 parent b533f64 commit dbf35bc
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions radiofeed/feedparser/feed_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def _handle_update(
feed: Feed,
) -> None:
categories_dct = get_categories()

try:
with transaction.atomic():
self._podcast_update(
Expand All @@ -134,8 +135,8 @@ def _handle_update(
active=not (feed.complete),
etag=self._parse_etag(response),
modified=self._parse_modified(response),
keywords=self._parse_keywords(feed, categories_dct),
extracted_text=self._tokenize_content(feed),
keywords=" ".join(self._parse_keywords(feed, categories_dct)),
frequency=scheduler.schedule(feed),
**feed.model_dump(
exclude={
Expand All @@ -149,7 +150,9 @@ def _handle_update(
self._podcast.categories.set(
self._parse_categories(feed, categories_dct)
)

self._episode_updates(feed)

self._logger.success("Feed updated")
except DataError as exc:
raise InvalidDataError from exc
Expand Down Expand Up @@ -239,21 +242,19 @@ def _podcast_update(self, **fields) -> None:
**fields,
)

def _parse_keywords(
self, feed: Feed, categories_dct: dict[str, Category]
) -> Iterator[str]:
categories_dct = get_categories()

for value in feed.categories:
if value not in categories_dct:
yield value
def _parse_keywords(self, feed: Feed, categories_dct: dict[str, Category]) -> str:
return " ".join(
[value for value in feed.categories if value not in categories_dct]
)

def _parse_categories(
self, feed: Feed, categories_dct: dict[str, Category]
) -> Iterator[Category]:
for value in feed.categories:
if value in categories_dct:
yield categories_dct[value]
) -> list[Category]:
return [
categories_dct[value]
for value in feed.categories
if value in categories_dct
]

def _tokenize_content(self, feed: Feed) -> str:
text = " ".join(
Expand Down

0 comments on commit dbf35bc

Please sign in to comment.