Skip to content

Instantly share code, notes, and snippets.

@zhombie
Last active February 28, 2020 13:54
Show Gist options
  • Save zhombie/0b3c37ccf212f42be68037b494d993e9 to your computer and use it in GitHub Desktop.
Save zhombie/0b3c37ccf212f42be68037b494d993e9 to your computer and use it in GitHub Desktop.
Telethon python library: Mini-script for obtaining web_preview text
def collect_article(self, blocks) -> str:
article = []
for block in blocks:
text = self.obtain_text(block)
if text:
article.append(text)
return '\n'.join(article)
def obtain_text(self, block):
if hasattr(block, 'text'):
attr = getattr(block, 'text')
if isinstance(attr, str):
return attr
elif hasattr(attr, 'texts') and isinstance(getattr(attr, 'texts'), list):
texts = []
for text in getattr(attr, 'texts'):
texts.append(self.obtain_text(text))
return ''.join(texts)
else:
return self.obtain_text(attr)
else:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment