fix raw object handling for api v1
This commit is contained in:
parent
2ce39ea06f
commit
49fd79e882
1 changed files with 34 additions and 25 deletions
|
@ -93,7 +93,7 @@ def new_message(msg):
|
||||||
db_session.add(event)
|
db_session.add(event)
|
||||||
db_session.commit()
|
db_session.commit()
|
||||||
|
|
||||||
if len(msg.raw) > 0:
|
if 'raw' in msg:
|
||||||
logger.debug('-handle media uploads-')
|
logger.debug('-handle media uploads-')
|
||||||
new_media(room.room_id, msg)
|
new_media(room.room_id, msg)
|
||||||
|
|
||||||
|
@ -104,36 +104,45 @@ def new_media(room_id, msg):
|
||||||
identity=matrix_id)
|
identity=matrix_id)
|
||||||
ts = int(msg.created_at.strftime('%s')) * 1000
|
ts = int(msg.created_at.strftime('%s')) * 1000
|
||||||
|
|
||||||
for item in msg.raw:
|
if 'io.pnut.core.oembed' in msg.raw:
|
||||||
if item.type == 'io.pnut.core.oembed' and 'url' in item.value:
|
|
||||||
|
|
||||||
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()
|
dl.raise_for_status()
|
||||||
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
|
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
|
||||||
mtype = m.id_buffer(dl.content)
|
info['mimetype'] = m.id_buffer(dl.content)
|
||||||
info = {'mimetype': mtype}
|
info['size'] = dl.content
|
||||||
|
|
||||||
ul = matrix_api.media_upload(dl.content, mtype)
|
ul = matrix_api.media_upload(dl.content, mtype)
|
||||||
|
|
||||||
if item.value.type == 'photo':
|
if 'title' in oembed:
|
||||||
msgtype = 'm.image'
|
title = oembed.title
|
||||||
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)
|
|
||||||
else:
|
else:
|
||||||
msgtype = 'm.file'
|
title = ""
|
||||||
info['size'] = len(dl.content)
|
|
||||||
|
|
||||||
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 = Events(
|
||||||
event_id=r['event_id'],
|
event_id=r['event_id'],
|
||||||
room_id=room_id,
|
room_id=room_id,
|
||||||
|
@ -327,7 +336,7 @@ if __name__ == '__main__':
|
||||||
with open(configyaml, "rb") as config_file:
|
with open(configyaml, "rb") as config_file:
|
||||||
config = yaml.load(config_file, Loader=yaml.SafeLoader)
|
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 += config['PNUT_APPTOKEN'] + '&key=' + config['PNUT_APPKEY']
|
||||||
ws_url += '&include_raw=1'
|
ws_url += '&include_raw=1'
|
||||||
matrix_url = config['MATRIX_HOST'] + '/_matrix/client/r0'
|
matrix_url = config['MATRIX_HOST'] + '/_matrix/client/r0'
|
||||||
|
|
Loading…
Reference in a new issue