LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.77k stars 1.39k forks source link

download_media() thows error when downloading a contact #1147

Closed bertploeger closed 5 years ago

bertploeger commented 5 years ago

Hi,

I use the latest telethon 1.6.2, which i think is the latest currently. When i try to download_media() a contact an error is thrown:

Media download start
Traceback (most recent call last):
  File "./telegramSyncHistory.py", line 90, in <module>
    item = helper.messages.process_message(message, chat, message.sender, message.media, dest_index, account_id, config)
  File "/opt/application/helper/messages.py", line 79, in process_message
    progress_callback=download_progress_callback
  File "/usr/lib/python3.6/site-packages/telethon/sync.py", line 35, in syncified
    return loop.run_until_complete(coro)
  File "/usr/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
    return future.result()
  File "/usr/lib/python3.6/site-packages/telethon/tl/custom/message.py", line 682, in download_media
    return await self._client.download_media(self, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/telethon/client/downloads.py", line 161, in download_media
    media, file
  File "/usr/lib/python3.6/site-packages/telethon/client/downloads.py", line 408, in _download_contact
    f.write(result)
TypeError: write() argument must be str, not bytes

The code i use is this;

r = client.iter_messages(chat, reverse=False, offset_date=offset_date, limit=1000)
        for message in r:
                filename = message.download_media(
                    file=destdir,
                    progress_callback=download_progress_callback
        )

What could be wrong?

Regards, Bert

bertploeger commented 5 years ago

I now temprorary fixed it by modifying line 408 in /usr/lib/python3.6/site-packages/telethon/client/downloads.py to:

f.write(str(result))
Lonami commented 5 years ago

Instead of doing str(result), removing .encode('utf-8') would have been a better manual fix.

Anyway the correct fix was opening the file in 'b'inary mode because all media can be "arbitrary bytes" and contacts are no exception.

bertploeger commented 5 years ago

Thanx for the quick response. I am very happy with the library and the good work you did!