Ejemplo n.º 1
0
 async def _handle_twitter_attachment(
         self, source: 'u.User', sender: 'p.Puppet',
         message: MessageData) -> Optional[MediaMessageEventContent]:
     content = None
     intent = sender.intent_for(self)
     media = message.attachment.media
     if media:
         reuploaded_info = await self._reupload_twitter_media(
             source, media.media_url_https, intent)
         thumbnail_info = None
         if media.video_info:
             thumbnail_info = reuploaded_info
             best_variant = None
             for variant in media.video_info.variants:
                 if ((not best_variant or (variant.bitrate or 0) >
                      (best_variant.bitrate or 0)
                      or self._is_better_mime(best_variant, variant))):
                     best_variant = variant
             reuploaded_info = await self._reupload_twitter_media(
                 source, best_variant.url, intent)
         content = MediaMessageEventContent(
             body=reuploaded_info.file_name,
             url=reuploaded_info.mxc,
             file=reuploaded_info.decryption_info,
             external_url=media.media_url_https)
         if message.attachment.video:
             content.msgtype = MessageType.VIDEO
             content.info = VideoInfo(
                 mimetype=reuploaded_info.mime_type,
                 size=reuploaded_info.size,
                 width=media.original_info.width,
                 height=media.original_info.height,
                 duration=media.video_info.duration_millis // 1000)
         elif message.attachment.photo or message.attachment.animated_gif:
             content.msgtype = MessageType.IMAGE
             content.info = ImageInfo(mimetype=reuploaded_info.mime_type,
                                      size=reuploaded_info.size,
                                      width=media.original_info.width,
                                      height=media.original_info.height)
         if thumbnail_info:
             content.info.thumbnail_url = thumbnail_info.mxc
             content.info.thumbnail_file = thumbnail_info.decryption_info
             content.info.thumbnail_info = ThumbnailInfo(
                 mimetype=thumbnail_info.mime_type,
                 size=thumbnail_info.size,
                 width=media.original_info.width,
                 height=media.original_info.height)
         # Remove the attachment link from message.text
         start, end = media.indices
         message.text = message.text[:start] + message.text[end:]
     elif message.attachment.tweet:
         # TODO special handling for tweets?
         pass
     return content
Ejemplo n.º 2
0
 async def _handle_twitter_attachment(
         self, source: 'u.User', sender: 'p.Puppet',
         message: MessageData) -> Optional[MediaMessageEventContent]:
     content = None
     intent = sender.intent_for(self)
     media = message.attachment.media
     if media:
         # TODO this doesn't actually work for gifs and videos
         #      The actual url is in media.video_info.variants[0].url
         #      Gifs are also videos
         reuploaded_info = await self._reupload_twitter_media(
             source, media, intent)
         content = MediaMessageEventContent(
             body=reuploaded_info.file_name,
             url=reuploaded_info.mxc,
             file=reuploaded_info.decryption_info,
             external_url=media.media_url_https)
         if message.attachment.video:
             content.msgtype = MessageType.VIDEO
             content.info = VideoInfo(
                 mimetype=reuploaded_info.mime_type,
                 size=reuploaded_info.size,
                 width=media.original_info.width,
                 height=media.original_info.height,
                 duration=media.video_info.duration_millis // 1000)
         elif message.attachment.photo or message.attachment.animated_gif:
             content.msgtype = MessageType.IMAGE
             content.info = ImageInfo(mimetype=reuploaded_info.mime_type,
                                      size=reuploaded_info.size,
                                      width=media.original_info.width,
                                      height=media.original_info.height)
         # Remove the attachment link from message.text
         start, end = media.indices
         message.text = message.text[:start] + message.text[end:]
     elif message.attachment.tweet:
         # TODO special handling for tweets?
         pass
     return content