Skip to content

Commit

Permalink
fix: detection of updated file is fixed by the inclusion of ``file_un…
Browse files Browse the repository at this point in the history
…ique_id`` attribute.

NOTE: this will only work when `PTB supports Bot API 4.5`__.

__ python-telegram-bot/python-telegram-bot#1508
  • Loading branch information
blueset committed Feb 10, 2020
1 parent 2a05441 commit 0ff72cc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ Removed

Fixed
-----
- Detection of updated file is fixed by the inclusion of ``file_unique_id``
attribute. NOTE: this will only work when `PTB supports Bot API 4.5`__.

__ https://github.com/python-telegram-bot/python-telegram-bot/pull/1508

2.0.0_ - 2020-01-31
======
===================
First release.

.. _2.0.0: https://etm.1a23.studio/releases/tag/v2.0.0
Expand Down
2 changes: 1 addition & 1 deletion efb_telegram_master/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.1.dev3"
__version__ = "2.0.1.dev4"
11 changes: 11 additions & 0 deletions efb_telegram_master/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class MsgLog(BaseModel):
"""MIME type of attachment."""
file_id = TextField(null=True)
"""File ID of attachment in Telegram."""
file_unique_id = TextField(null=True)
"""Unique file ID of attachment in Telegram."""
msg_type = TextField()
"""Message type in EFB framework."""
pickle = BlobField(null=True)
Expand Down Expand Up @@ -198,6 +200,8 @@ def __init__(self, channel: 'TelegramChannel'):
self._migrate(1)
elif "slave_chat_group_id" not in slave_chat_info_columns:
self._migrate(2)
elif "file_unique_id" not in msg_log_columns:
self._migrate(3)
self.logger.debug("Database migration finished...")

def task_worker(self):
Expand Down Expand Up @@ -261,6 +265,12 @@ def _migrate(i: int):
migrate(
migrator.add_column("slavechatinfo", "slave_chat_group_id", SlaveChatInfo.slave_chat_group_id)
)
if i <= 3:
# Migration 3: Add column for unique file ID to message log table
# 2019NOV18
migrate(
migrator.add_column("msglog", "file_unique_id", MsgLog.file_unique_id)
)

def add_chat_assoc(self, master_uid: EFBChannelChatIDStr,
slave_uid: EFBChannelChatIDStr,
Expand Down Expand Up @@ -422,6 +432,7 @@ def add_or_update_message_log(self,
row.slave_message_id = msg.uid or f"{self.FAIL_FLAG}.{time.time()}"
row.media_type = msg.type_telegram.value
row.file_id = msg.file_id
row.file_unique_id = msg.file_unique_id
row.mime = msg.mime
pickle_data = self.pickle_misc_msg(msg)
if pickle_data:
Expand Down
8 changes: 8 additions & 0 deletions efb_telegram_master/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
class ETMMsg(Message):
file_id: Optional[str] = None
"""File ID from Telegram Bot API"""
file_unique_id: Optional[str] = None
"""Unique file ID from Telegram Bot API"""
type_telegram: TGMsgType
"""Type of message in Telegram Bot API"""
chat: ETMChatType
Expand Down Expand Up @@ -188,26 +190,32 @@ def put_telegram_file(self, message: telegram.Message):
if attachment:
is_common_file = True
self.file_id = attachment.file_id
self.file_unique_id = attachment.file_unique_id
self.mime = attachment.mime_type
break

if not is_common_file:
if self.type_telegram is TGMsgType.Audio:
self.file_id = message.audio.file_id
self.file_unique_id = message.audio.file_unique_id
self.mime = message.audio.mime_type
extension = mimetypes.guess_extension(message.audio.mime_type)
self.filename = f"{message.audio.title} - {message.audio.performer}{extension}"
elif self.type_telegram is TGMsgType.Sticker:
self.file_id = message.sticker.file_id
self.file_unique_id = message.sticker.file_unique_id
self.mime = 'image/webp'
elif self.type_telegram is TGMsgType.AnimatedSticker:
self.file_id = message.sticker.file_id
self.file_unique_id = message.sticker.file_unique_id
self.mime = 'application/json+tgs'
self.type = MsgType.Animation
elif getattr(message, 'photo', None):
attachment = message.photo[-1]
self.file_id = attachment.file_id
self.file_unique_id = attachment.file_unique_id
self.mime = 'image/jpeg'
elif self.type_telegram is TGMsgType.VideoNote:
self.file_id = message.video_note.file_id
self.file_unique_id = message.video_note.file_unique_id
self.mime = 'video/mpeg'

0 comments on commit 0ff72cc

Please sign in to comment.