Last active
February 28, 2020 13:54
-
-
Save zhombie/0b3c37ccf212f42be68037b494d993e9 to your computer and use it in GitHub Desktop.
Telethon python library: Mini-script for obtaining web_preview text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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