Skip to content

Commit d362327

Browse files
committed
add polls and contacts exporting
1 parent 4b9a2b1 commit d362327

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "t-export"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
description = "Telegram chats export tool."
55
authors = ["RuslanUC <dev_ruslan_uc@protonmail.com>"]
66
readme = "README.md"

texport/exporter.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Union
44

55
from pyrogram import Client
6+
from pyrogram.enums import MessageMediaType
67
from pyrogram.types import Message as PyroMessage
78
from pyrogram.utils import zero_datetime
89

@@ -31,14 +32,15 @@ async def _export_media(self, message: PyroMessage) -> None:
3132
return
3233
m = MEDIA_TYPES[message.media]
3334
media = m.get_media(message)
34-
if media.file_size > self._config.size_limit * 1024 * 1024:
35+
if m.has_size_limit and media.file_size > self._config.size_limit * 1024 * 1024:
3536
return
3637

37-
self._media_downloader.add(media.file_id, f"{self._config.output_dir.absolute()}/{m.dir_name}/", message.id)
38+
if m.downloadable:
39+
self._media_downloader.add(media.file_id, f"{self._config.output_dir.absolute()}/{m.dir_name}/", message.id)
3840

39-
if hasattr(media, "thumbs") and media.thumbs:
40-
self._media_downloader.add(media.thumbs[0].file_id, f"{self._config.output_dir.absolute()}/thumbs/",
41-
f"{message.id}_thumb")
41+
if hasattr(media, "thumbs") and media.thumbs:
42+
self._media_downloader.add(media.thumbs[0].file_id, f"{self._config.output_dir.absolute()}/thumbs/",
43+
f"{message.id}_thumb")
4244

4345
async def _write(self, wait_media: list[int]) -> None:
4446
self.progress.status = "Waiting for all media to be downloaded..."
@@ -69,7 +71,7 @@ async def _export(self, chat_id: Union[int, str]):
6971
medias.append(f"{message.id}_thumb")
7072
await self._export_media(message)
7173

72-
if not message.text and not message.caption and message.id not in self._media:
74+
if not message.text and not message.caption and message.media not in MEDIA_TYPES:
7375
continue
7476

7577
self._messages.append(message)

texport/html/contact.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pyrogram.types import Message as PyroMessage
2+
3+
from .base import HtmlMedia
4+
5+
6+
class Contact(HtmlMedia):
7+
def __init__(self, *args, message: PyroMessage):
8+
self.contact = message.contact
9+
10+
def no_media(self) -> str:
11+
return ""
12+
13+
def to_html(self) -> str:
14+
last_name = "" if not self.contact.last_name else self.contact.last_name
15+
16+
return f"""
17+
<div class="media clearfix pull_left media_contact">
18+
<div class="fill pull_left"></div>
19+
<div class="body">
20+
<div class="title bold">{self.contact.first_name} {last_name}</div>
21+
<div class="status details">{self.contact.phone_number}</div>
22+
</div>
23+
</div>
24+
"""

texport/html/poll.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from pyrogram.enums import PollType
2+
from pyrogram.types import Message as PyroMessage, Poll as PyroPoll
3+
4+
from .base import HtmlMedia, BaseComponent
5+
6+
7+
class PollOption(BaseComponent):
8+
def __init__(self, poll: PyroPoll, option: int):
9+
self.poll = poll
10+
self.option_id = option
11+
self.option = poll.options[option]
12+
13+
def to_html(self) -> str:
14+
details = []
15+
if self.option.voter_count:
16+
details.append(f"{self.option.voter_count} votes")
17+
if self.option_id == self.poll.chosen_option_id:
18+
details.append("chosen vote")
19+
if self.poll.type == PollType.QUIZ and self.option_id == self.poll.correct_option_id:
20+
details.append("correct vote")
21+
details = ", ".join(details)
22+
details = "" if not details else f"""<span class="details">{details}</span>"""
23+
24+
return f"""
25+
<div class="answer">- {self.option.text} {details}</div>
26+
"""
27+
28+
29+
class Poll(HtmlMedia):
30+
def __init__(self, *args, message: PyroMessage):
31+
self.poll = message.poll
32+
33+
def no_media(self) -> str:
34+
return ""
35+
36+
def to_html(self) -> str:
37+
poll_details = None
38+
if self.poll.is_anonymous:
39+
poll_details = "<div class=\"details\">Anonymous poll</div>"
40+
if self.poll.is_closed:
41+
poll_details = "<div class=\"details\">Closed poll</div>"
42+
43+
options = ""
44+
for idx, opt in enumerate(self.poll.options):
45+
options += PollOption(self.poll, idx).to_html()
46+
47+
return f"""
48+
<div class="media_poll">
49+
<div class="question bold">{self.poll.question}</div>
50+
51+
{"" if poll_details is None else poll_details}
52+
53+
{options}
54+
55+
<div class="total details"{self.poll.total_voter_count} votes</div>
56+
</div>
57+
"""

texport/media.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from .html.animation import Animation
77
from .html.audio import Audio
88
from .html.base import HtmlMedia
9+
from .html.contact import Contact
910
from .html.document import Document
1011
from .html.photo import Photo
12+
from .html.poll import Poll
1113
from .html.sticker import Sticker
1214
from .html.video import Video
1315
from .html.video_note import VideoNote
@@ -19,6 +21,12 @@ class Media:
1921
attr_name: str
2022
dir_name: str
2123
html_media_type: type[HtmlMedia]
24+
has_size_limit: bool = True
25+
downloadable: bool = True
26+
27+
def __post_init__(self):
28+
if not self.downloadable:
29+
self.has_size_limit = False
2230

2331
def get_media(self, message: PyroMessage):
2432
return getattr(message, self.attr_name)
@@ -33,4 +41,6 @@ def get_media(self, message: PyroMessage):
3341
MessageMediaType.VIDEO: Media("video", "videos", Video),
3442
MessageMediaType.ANIMATION: Media("animation", "animations", Animation),
3543
MessageMediaType.VIDEO_NOTE: Media("video_note", "video_notes", VideoNote),
44+
MessageMediaType.POLL: Media("poll", "_", Poll, False, False),
45+
MessageMediaType.CONTACT: Media("contact", "_", Contact, False, False),
3646
}

0 commit comments

Comments
 (0)