Skip to content

Commit

Permalink
Merge pull request #2 from stickman33/main
Browse files Browse the repository at this point in the history
Add video files support
  • Loading branch information
aigoncharov authored Sep 25, 2024
2 parents 5779a3c + 24ff9e9 commit ae573b5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
26 changes: 18 additions & 8 deletions telegram_to_rss/generate_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@ def generate_feed(feed_render_dir: Path, feed: Feed):
title = message_text[:100]
ET.SubElement(rss_item_el, "title").text = title

images = ""
media_content = ""
media_download_failure = False

# processing mediafiles
for media_path in feed_entry.media:
if media_path == "FAIL":
media_download_failure = True
else:
media_url = "{}/static/{}".format(base_url, media_path)
images += '<br /><img src="{}"/>'.format(media_url)
content = feed_entry.message.replace("\n", "<br />") + images

# checking file type
if media_path.endswith(('.jpg', '.png', '.gif')):
media_content += '<br /><img src="{}" alt="media"/>'.format(media_url)
elif media_path.endswith('.mp4'):
media_content += (
'<br /><video controls poster="{}" style="max-width:100%;">'
'<source src="{}" type="video/mp4">'
'Your browser does not support the video tag.</video>'
).format(media_url, media_url)

# creating feed with text and media
content = feed_entry.message.replace("\n", "<br />") + media_content
if feed_entry.has_unsupported_media:
content += "<br /><strong>This message has unsupported attachment. Open Telegram to view it.</strong>"
if media_download_failure:
content += "<br /><strong>Downloading some of the media for this message failed. Open Telegram to view it.</strong>"
ET.SubElement(rss_item_el, "description").text = content

ET.SubElement(rss_item_el, "description").text = content
ET.SubElement(rss_item_el, "pubDate").text = feed_entry.date.isoformat()
ET.SubElement(rss_item_el, "link", {"href": feed_entry_url}).text = (
feed_entry_url
)
ET.SubElement(rss_item_el, "link", {"href": feed_entry_url}).text = feed_entry_url

final_feed_file = feed_render_dir.joinpath("{}.xml".format(feed.id))

Expand All @@ -78,7 +89,6 @@ def generate_feed(feed_render_dir: Path, feed: Feed):

logging.info("generate_feed -> done %s %s", feed.name, feed.id)


async def update_feeds_cache(feed_render_dir: str):
feeds = await Feed.all().prefetch_related(
Prefetch("entries", queryset=FeedEntry.all().order_by("-date"))
Expand Down
32 changes: 30 additions & 2 deletions telegram_to_rss/poll_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,36 @@ def progress_callback(current, total, media_path=media_path):
f"TelegramPoller._process_new_dialog_messages -> downloading media failed with {e} for message {dialog_message.id} {dialog_message.date} {dialog_message.text}",
)
last_processed_message.downloaded_media.append("FAIL")
elif isinstance(dialog_message.media, types.MessageMediaDocument):
document = dialog_message.media.document
mime_type = getattr(document, 'mime_type', None)
if mime_type and mime_type.startswith("video/"):
try:
feed_entry_media_id = "{}-{}".format(
to_feed_entry_id(feed, dialog_message),
len(last_processed_message.downloaded_media),
)
media_path = self._static_path.joinpath(feed_entry_media_id)

def progress_callback(current, total, media_path=media_path):
logging.debug(
"TelegramPoller._process_new_dialog_messages -> downloading video %s: %s out of %s",
media_path,
current,
total,
)

res_path = await dialog_message.download_media(
file=media_path, progress_callback=progress_callback
)
last_processed_message.downloaded_media.append(Path(res_path).name)
except Exception as e:
logging.warning(
f"TelegramPoller._process_new_dialog_messages -> downloading video failed with {e} for message {dialog_message.id} {dialog_message.date} {dialog_message.text}",
)
last_processed_message.downloaded_media.append("FAIL")

# creating FeedEntry with mediafiles
feed_entries: list[FeedEntry] = []
for dialog_message in filtered_dialog_messages:
feed_entry_id = to_feed_entry_id(feed, dialog_message)
Expand All @@ -190,10 +219,9 @@ def progress_callback(current, total, media_path=media_path):
date=dialog_message.date,
media=dialog_message.downloaded_media,
has_unsupported_media=dialog_message.media is not None
and not isinstance(dialog_message.media, types.MessageMediaPhoto),
and not isinstance(dialog_message.media, (types.MessageMediaPhoto, types.MessageMediaDocument)),
)
)

return feed_entries


Expand Down

0 comments on commit ae573b5

Please sign in to comment.