save method for content
Some checks are pending
Build and Push Docker Image / build (push) Waiting to run
Some checks are pending
Build and Push Docker Image / build (push) Waiting to run
This commit is contained in:
parent
08295dfea6
commit
d86fc0eb71
@ -5,7 +5,7 @@ import asyncio
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from time import perf_counter
|
from time import perf_counter
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass, asdict
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from textwrap import shorten
|
from textwrap import shorten
|
||||||
|
|
||||||
@ -323,6 +323,7 @@ class Subscription(DjangoDataModel):
|
|||||||
for content in contents:
|
for content in contents:
|
||||||
log.debug(f"filtering: '{content.item_title}'")
|
log.debug(f"filtering: '{content.item_title}'")
|
||||||
if any(content_filter.matches(content) for content_filter in self.filters):
|
if any(content_filter.matches(content) for content_filter in self.filters):
|
||||||
|
content.blocked = True
|
||||||
invalid_contents.append(content)
|
invalid_contents.append(content)
|
||||||
else:
|
else:
|
||||||
valid_contents.append(content)
|
valid_contents.append(content)
|
||||||
@ -340,14 +341,16 @@ class Content(DjangoDataModel):
|
|||||||
item_url: str
|
item_url: str
|
||||||
item_title: str
|
item_title: str
|
||||||
item_description: str
|
item_description: str
|
||||||
|
item_content_hash: str
|
||||||
item_image_url: str | None
|
item_image_url: str | None
|
||||||
item_thumbnail_url: str | None
|
item_thumbnail_url: str | None
|
||||||
item_published: datetime | None
|
item_published: datetime | None
|
||||||
item_author: str
|
item_author: str
|
||||||
item_author_url: str
|
item_author_url: str | None
|
||||||
item_feed_title: str
|
item_feed_title: str
|
||||||
item_feed_url: str
|
item_feed_url: str
|
||||||
_subscription: Subscription | None = None
|
_subscription: Subscription | None = None
|
||||||
|
blocked: bool = False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parser(item: dict) -> dict:
|
def parser(item: dict) -> dict:
|
||||||
@ -355,6 +358,24 @@ class Content(DjangoDataModel):
|
|||||||
item["subscription_id"] = item.pop("subscription")
|
item["subscription_id"] = item.pop("subscription")
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
async def save(self, client: httpx.AsyncClient, base_url: str, headers: dict):
|
||||||
|
log.debug(f"saving content {self.item_content_hash}")
|
||||||
|
|
||||||
|
data = asdict(self)
|
||||||
|
data.pop("id")
|
||||||
|
data["subscription"] = data.pop("subscription_id")
|
||||||
|
item_published = data.pop("item_published")
|
||||||
|
data["item_published"] = item_published.strftime("%Y-%m-%d") if item_published else None
|
||||||
|
data.pop("_subscription")
|
||||||
|
|
||||||
|
response = await client.post(
|
||||||
|
url=base_url + "content/",
|
||||||
|
headers=headers,
|
||||||
|
data=data
|
||||||
|
)
|
||||||
|
log.debug(response.text)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def from_raw_rss(cls, rss: str, subscription: Subscription, client: httpx.AsyncClient):
|
async def from_raw_rss(cls, rss: str, subscription: Subscription, client: httpx.AsyncClient):
|
||||||
style = subscription.message_style
|
style = subscription.message_style
|
||||||
@ -362,9 +383,8 @@ class Content(DjangoDataModel):
|
|||||||
contents = []
|
contents = []
|
||||||
|
|
||||||
async def create_content(entry: feedparser.FeedParserDict):
|
async def create_content(entry: feedparser.FeedParserDict):
|
||||||
# content_hash = hashlib.new("sha256")
|
content_hash = hashlib.new("sha256")
|
||||||
# content_hash.update(entry.get("description", "").encode())
|
content_hash.update(entry.get("description", "").encode())
|
||||||
# content_hash.hexdigest()
|
|
||||||
|
|
||||||
item_url = entry.get("link", "")
|
item_url = entry.get("link", "")
|
||||||
item_image_url = entry.get("media_thumbnail", [{}])[0].get("url")
|
item_image_url = entry.get("media_thumbnail", [{}])[0].get("url")
|
||||||
@ -382,11 +402,12 @@ class Content(DjangoDataModel):
|
|||||||
"item_url": item_url,
|
"item_url": item_url,
|
||||||
"item_title": entry.get("title", ""),
|
"item_title": entry.get("title", ""),
|
||||||
"item_description": entry.get("description", ""),
|
"item_description": entry.get("description", ""),
|
||||||
|
"item_content_hash": content_hash.hexdigest(),
|
||||||
"item_image_url": item_image_url,
|
"item_image_url": item_image_url,
|
||||||
"item_thumbnail_url": parsed_rss.feed.image.href or None,
|
"item_thumbnail_url": parsed_rss.feed.image.href or None,
|
||||||
"item_published": published,
|
"item_published": published,
|
||||||
"item_author": entry.get("author", ""),
|
"item_author": entry.get("author", ""),
|
||||||
"item_author_url": entry.get("author_detail", {}).get("href", ""),
|
"item_author_url": entry.get("author_detail", {}).get("href"),
|
||||||
"item_feed_title": parsed_rss.get("feed", {}).get("title"),
|
"item_feed_title": parsed_rss.get("feed", {}).get("title"),
|
||||||
"item_feed_url": parsed_rss.get("feed", {}).get("link")
|
"item_feed_url": parsed_rss.get("feed", {}).get("link")
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user