Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Photos, video thumbs and views to posts #1026

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions snscrape/modules/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

_logger = logging.getLogger(__name__)
_SINGLE_MEDIA_LINK_PATTERN = re.compile(r'^https://t\.me/[^/]+/\d+\?single$')

_TELEGRAM_IMG_RE = re.compile(r"background-image:url\(\'(.*?)\'\)")

@dataclasses.dataclass
class LinkPreview:
Expand All @@ -31,6 +31,9 @@ class TelegramPost(snscrape.base.Item):
date: datetime.datetime
content: str
outlinks: list
photos: list
videoThumbs: list
views: str
linkPreview: typing.Optional[LinkPreview] = None

outlinksss = snscrape.base._DeprecatedProperty('outlinksss', lambda self: ' '.join(self.outlinks), 'outlinks')
Expand Down Expand Up @@ -91,6 +94,7 @@ def _soup_to_items(self, soup, pageUrl, onlyUsername = False):
_logger.warning(f'Possibly incorrect URL: {rawUrl!r}')
url = rawUrl.replace('//t.me/', '//t.me/s/')
date = datetime.datetime.strptime(dateDiv.find('time', datetime = True)['datetime'].replace('-', '', 2).replace(':', ''), '%Y%m%dT%H%M%S%z')
views = post.find("span", {"class": "tgme_widget_message_views"}).text
if (message := post.find('div', class_ = 'tgme_widget_message_text')):
content = message.text
outlinks = []
Expand All @@ -107,6 +111,29 @@ def _soup_to_items(self, soup, pageUrl, onlyUsername = False):
href = urllib.parse.urljoin(pageUrl, link['href'])
if href not in outlinks:
outlinks.append(href)

photos = []
photosHtml = post.select(
"a[class='tgme_widget_message_photo_wrap grouped_media_wrap blured js-message_photo']"
)

if photosHtml:
for photo in photosHtml:
photos.append(_TELEGRAM_IMG_RE.search(photo.get("style")).group(1))
else:
photosBlock = post.select_one("a[class*='tgme_widget_message_photo_wrap']")
if photosBlock:
photos.append(_TELEGRAM_IMG_RE.search(photosBlock.get("style")).group(1))

VideoThumbsHtml = post.select(
"i[class='tgme_widget_message_video_thumb']"
)

videoThumbs = []
if VideoThumbsHtml:
for thumb in VideoThumbsHtml:
videoThumbs.append(_TELEGRAM_IMG_RE.search(thumb.get("style")).group(1))

else:
content = None
outlinks = []
Expand All @@ -126,7 +153,7 @@ def _soup_to_items(self, soup, pageUrl, onlyUsername = False):
else:
_logger.warning(f'Could not process link preview image on {url}')
linkPreview = LinkPreview(**kwargs)
yield TelegramPost(url = url, date = date, content = content, outlinks = outlinks, linkPreview = linkPreview)
yield TelegramPost(url = url, date = date, content = content, outlinks = outlinks, linkPreview = linkPreview, photos = photos, videoThumbs = videoThumbs, views=views)

def get_items(self):
r, soup = self._initial_page()
Expand Down