Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Check key "document" exists before parsing
Browse files Browse the repository at this point in the history
Check if the key "document" key exists on message['media'] before parsing, otherwise script fails

To reproduce configure CERT-PA channel @certpaitalia; they usually don't attach files to the news. Launch the script and you see following error

gmellini@19-04:~/Scrivania/OpenSource/DL-Telegram-by-file-attachment$ ./downloader.py 
DL-Telegram-by-file-attachment started...
  Config loaded!
Traceback (most recent call last):
  File "./downloader.py", line 40, in <module>
    if 'webpage' in message['media'] and re.search(term, message['media']['document']['attributes'][0]['file_name'], re.IGNORECASE):
KeyError: 'document'
gmellini@19-04:~/Scrivania/OpenSource/DL-Telegram-by-file-attachment$

With the fix is ok
  • Loading branch information
gmellini committed Aug 22, 2019
1 parent 10279ba commit 9691da7
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion downloader.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
for message in data['messages']: for message in data['messages']:
if 'media' in message: if 'media' in message:
for term in search: for term in search:
if re.search(term, message['media']['document']['attributes'][0]['file_name'], re.IGNORECASE): if 'document' in message['media'] and re.search(term, message['media']['document']['attributes'][0]['file_name'], re.IGNORECASE):
print(' Download ' + message['media']['document']['attributes'][0]['file_name']) print(' Download ' + message['media']['document']['attributes'][0]['file_name'])
response = urlopen('https://tg.i-c-a.su/media/' + config.get('channel', 'name') + '/' + str(message['id'])) response = urlopen('https://tg.i-c-a.su/media/' + config.get('channel', 'name') + '/' + str(message['id']))
file = open(config.get('channel', 'download') + message['media']['document']['attributes'][0]['file_name'], 'wb') file = open(config.get('channel', 'download') + message['media']['document']['attributes'][0]['file_name'], 'wb')
Expand Down

0 comments on commit 9691da7

Please sign in to comment.