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: viewCount property to TelegramPost data class #1035

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
21 changes: 19 additions & 2 deletions snscrape/modules/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LinkPreview:
class TelegramPost(snscrape.base.Item):
url: str
date: datetime.datetime
viewCount: int
content: str
outlinks: list
linkPreview: typing.Optional[LinkPreview] = None
Expand Down Expand Up @@ -85,12 +86,28 @@ def _soup_to_items(self, soup, pageUrl, onlyUsername = False):
if onlyUsername:
yield post['data-post'].split('/')[0]
return
dateDiv = post.find('div', class_ = 'tgme_widget_message_footer').find('a', class_ = 'tgme_widget_message_date')
footerDiv = post.find('div', class_ = 'tgme_widget_message_footer')
dateDiv = footerDiv.find('a', class_ = 'tgme_widget_message_date')
rawUrl = dateDiv['href']
if not rawUrl.startswith('https://t.me/') or sum(x == '/' for x in rawUrl) != 4 or rawUrl.rsplit('/', 1)[1].strip('0123456789') != '':
_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')
viewDiv = footerDiv.find('span', class_ = 'tgme_widget_message_views')
viewText = viewDiv.text
viewPostfix = viewText[-1].lower()
if viewPostfix.isdigit():
viewCount = int(float(viewText))
else:
viewFactor = {
'k': 1000,
'm': 1000 * 1000,
'g': 1000 * 1000 * 1000,
}.get(viewPostfix)
if viewFactor:
viewCount = int(float(viewText[:-1])) * viewFactor
else:
raise snscrape.base.ScraperException(f'Got unknown postfix from views text: "{viewText}"')
if (message := post.find('div', class_ = 'tgme_widget_message_text')):
content = message.text
outlinks = []
Expand Down Expand Up @@ -126,7 +143,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, viewCount = viewCount, content = content, outlinks = outlinks, linkPreview = linkPreview)

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