From 49fd79e8822c28913fc710fcfd9f31c89e8d9946 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 17 Jan 2021 21:47:31 -0800 Subject: [PATCH] fix raw object handling for api v1 --- pnut-matrix.py | 59 +++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/pnut-matrix.py b/pnut-matrix.py index 997a2b8..378ebe4 100644 --- a/pnut-matrix.py +++ b/pnut-matrix.py @@ -93,7 +93,7 @@ def new_message(msg): db_session.add(event) db_session.commit() - if len(msg.raw) > 0: + if 'raw' in msg: logger.debug('-handle media uploads-') new_media(room.room_id, msg) @@ -104,36 +104,45 @@ def new_media(room_id, msg): identity=matrix_id) ts = int(msg.created_at.strftime('%s')) * 1000 - for item in msg.raw: - if item.type == 'io.pnut.core.oembed' and 'url' in item.value: + if 'io.pnut.core.oembed' in msg.raw: - dl = requests.get(item.value.url, stream=True) + for oembed in msg.raw['io.pnut.core.oembed']: + info = {} + + if oembed.type == 'photo': + msgtype = 'm.image' + dl_url = oembed.url + info['h'] = oembed.height + info['w'] = oembed.width + elif oembed.type == 'audio': + logger.debug("* recieved audio attachment") + continue + elif oembed.type == 'video': + logger.debug("* recieved video attachment") + continue + elif oembed.type == 'html5video': + logger.debug("* recieved html5 video attachment") + continue + elif oembed.type == 'rich': + logger.debug("* recieved video attachment") + continue + else: + logger.debug("* recieved unknown attachment") + continue + + dl = requests.get(dl_url, stream=True) dl.raise_for_status() with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: - mtype = m.id_buffer(dl.content) - info = {'mimetype': mtype} - + info['mimetype'] = m.id_buffer(dl.content) + info['size'] = dl.content ul = matrix_api.media_upload(dl.content, mtype) - if item.value.type == 'photo': - msgtype = 'm.image' - info['h'] = item.value.height - info['w'] = item.value.width - info['size'] = len(dl.content) - elif item.value.type == 'video' or item.value.type == 'html5video': - msgtype = 'm.video' - info['h'] = item.value.height - info['w'] = item.value.width - info['size'] = len(dl.content) - elif item.value.type == 'audio': - msgtype = 'm.audio' - info['duration'] = int(item.value.duration) * 1000 - info['size'] = len(dl.content) + if 'title' in oembed: + title = oembed.title else: - msgtype = 'm.file' - info['size'] = len(dl.content) + title = "" - r = matrix_api.send_content(room_id, ul['content_uri'], item.value.title, msgtype, extra_information=info, timestamp=ts) + r = matrix_api.send_content(room_id, ul['content_uri'], title, msgtype, extra_information=info, timestamp=ts) event = Events( event_id=r['event_id'], room_id=room_id, @@ -327,7 +336,7 @@ if __name__ == '__main__': with open(configyaml, "rb") as config_file: config = yaml.load(config_file, Loader=yaml.SafeLoader) - ws_url = 'wss://stream.pnut.io/v0/app?access_token=' + ws_url = 'wss://stream.pnut.io/v1/app?access_token=' ws_url += config['PNUT_APPTOKEN'] + '&key=' + config['PNUT_APPKEY'] ws_url += '&include_raw=1' matrix_url = config['MATRIX_HOST'] + '/_matrix/client/r0'