mutator and embed fix
This commit is contained in:
parent
92e9e63e53
commit
d6e6043194
@ -132,7 +132,7 @@ class TaskCog(commands.Cog):
|
||||
successful_track = await self.mark_tracked_item(api, sub, item, channel.id, blocked)
|
||||
|
||||
if successful_track and not blocked:
|
||||
await channel.send(embed=await item.to_embed(sub, feed, api.session))
|
||||
await channel.send(embed=await mutated_item.to_embed(sub, feed, api.session))
|
||||
|
||||
def filter_item(self, _filter: dict, item: RSSItem) -> bool:
|
||||
"""
|
||||
|
57
src/feed.py
57
src/feed.py
@ -1,7 +1,8 @@
|
||||
|
||||
import json
|
||||
import copy
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
@ -47,25 +48,29 @@ class RSSItem:
|
||||
return cls(guid, link, title, description, pub_date, image_url)
|
||||
|
||||
def create_mutated_copy(self, mutators):
|
||||
pass
|
||||
item_copy = copy.copy(self)
|
||||
|
||||
def apply_mutation(item, attr: str, mutator: dict[str, str]):
|
||||
val = mutator["value"]
|
||||
try:
|
||||
mutator = mutator_registry.get_mutator(val)
|
||||
except ValueError as err:
|
||||
log.error(err)
|
||||
|
||||
setattr(item, attr, mutator.mutate(getattr(item, attr)))
|
||||
|
||||
for field in ("title", "description"):
|
||||
for mutator in mutators[field]:
|
||||
apply_mutation(item_copy, field, mutator)
|
||||
|
||||
return item_copy
|
||||
|
||||
async def to_embed(self, sub, feed, session):
|
||||
|
||||
# Replace HTML with Markdown, and shorten text.
|
||||
title = shorten(markdownify(self.title, strip=["img", "a"]), 256)
|
||||
desc = shorten(markdownify(self.description, strip=["img"]), 4096)
|
||||
|
||||
author = ""
|
||||
author_url = ""
|
||||
|
||||
icon_url = ""
|
||||
thumb_url = ""
|
||||
|
||||
# Replace HTML with Markdown, and shorten text.
|
||||
# author = shorten(self.source.name, 256)
|
||||
|
||||
# validate urls
|
||||
# author_url = self.source.url if validators.url(self.source.url) else None
|
||||
# icon_url = self.source.icon_url if validators.url(self.source.icon_url) else None
|
||||
author = shorten(feed.title, 256)
|
||||
|
||||
# Combined length validation
|
||||
# Can't exceed combined 6000 characters, [400 Bad Request] if failed.
|
||||
@ -76,20 +81,17 @@ class RSSItem:
|
||||
embed = Embed(
|
||||
title=title,
|
||||
description=desc,
|
||||
# timestamp=self.published,
|
||||
timestamp=self.pub_date,
|
||||
url=self.link if validators.url(self.link) else None,
|
||||
colour=Colour.from_str("#" + sub.embed_colour)
|
||||
)
|
||||
|
||||
log.debug("has image url without search: '%s'", self.image_url)
|
||||
|
||||
if sub.article_fetch_image:
|
||||
embed.set_image(url=self.image_url or await self.get_thumbnail_url(session))
|
||||
embed.set_thumbnail(url=feed.image_href if validators.url(feed.image_href) else None)
|
||||
|
||||
# embed.set_image(url=thumb_url)
|
||||
# embed.set_author(url=author_url, name=author)
|
||||
# embed.set_footer(text=self.author)
|
||||
embed.set_author(name=author, url=feed.link)
|
||||
embed.set_footer(text=sub.name)
|
||||
|
||||
return embed
|
||||
|
||||
@ -107,7 +109,7 @@ class RSSItem:
|
||||
The thumbnail URL, or None if not found.
|
||||
"""
|
||||
|
||||
# log.debug("Fetching thumbnail for article: %s", self)
|
||||
log.debug("Fetching thumbnail for article: %s", self.guid)
|
||||
|
||||
try:
|
||||
async with session.get(self.link, timeout=15) as response:
|
||||
@ -131,9 +133,12 @@ class RSSFeed:
|
||||
description: str
|
||||
link: str
|
||||
lang: str
|
||||
last_build_date = datetime
|
||||
last_build_date: datetime
|
||||
image_href: str
|
||||
items: list[RSSItem] = field(default_factory=list)
|
||||
items: list[RSSItem] = None
|
||||
|
||||
def __post_init__(self):
|
||||
self.items = []
|
||||
|
||||
def add_item(self, item: RSSItem):
|
||||
if not isinstance(item, RSSItem):
|
||||
@ -151,7 +156,7 @@ class RSSFeed:
|
||||
last_build_date = pf.feed.get('updated_parsed', None)
|
||||
last_build_date = datetime(*last_build_date[0:-2] if last_build_date else None)
|
||||
|
||||
image_href = pf.get("image", {}).get("href")
|
||||
image_href = pf.feed.get("image", {}).get("href")
|
||||
|
||||
feed = cls(title, description, link, language, last_build_date, image_href)
|
||||
|
||||
@ -209,7 +214,7 @@ class Subscription(DjangoDataModel):
|
||||
item["creation_datetime"] = datetime.strptime(item["creation_datetime"], "%Y-%m-%dT%H:%M:%S.%f%z")
|
||||
item["mutators"] = {
|
||||
"title": item.pop("article_title_mutators"),
|
||||
"desc": item.pop("article_desc_mutators")
|
||||
"description": item.pop("article_desc_mutators")
|
||||
}
|
||||
|
||||
return item
|
||||
|
Loading…
x
Reference in New Issue
Block a user