matrix-shoutbox

git clone https://git.tarina.org/matrix-shoutbox
Log | Files | Refs

shoutbox-recv.py (3503B)


      1 import asyncio
      2 from nio import (AsyncClient, RoomMessageText)
      3 from nio import (AsyncClient, RoomMessageImage)
      4 import config
      5 from datetime import datetime, timedelta
      6 
      7 msg_to_file = '/srv/www/radiorymd.com/public_html/static/msg.html'
      8 open(msg_to_file, 'w').close()
      9 
     10 datestamp = ''
     11 
     12 async def message_cb(room, event):
     13     global datestamp
     14     todaysdate = datetime.now().strftime('%A %d %B')
     15     if room.display_name in config.display_name:
     16         print("{}: {}".format(room.user_name(event.sender), event.body))
     17         #msg_file = open(msg_to_file, 'a')
     18         #msg_file.write(room.user_name(event.sender) + ': ' + event.body + '\n')
     19         timestamp = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%H:%M")
     20         datestamp_new = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%A %d %B")
     21         time_html = '<span id="time">' + timestamp + '</span>'
     22         user_html = '<span id="user"> ' + room.user_name(event.sender) + ' </span>'
     23         msg_html = '<span id="msg"> ' + event.body + ' </span>'
     24         if datestamp != '' and datestamp != todaysdate and datestamp != datestamp_new:
     25             new_msg = '<p>' + time_html + user_html + msg_html + '</p>\n'+'<br><h4>'+datestamp+'</h4>\n'
     26         else:
     27             new_msg = '<p>' + time_html + user_html + msg_html + '</p>\n'
     28         datestamp = datestamp_new
     29         with open(msg_to_file, 'r') as original:
     30             data = original.readlines()[0:100]
     31         data = ''.join(data)
     32         #print(data)
     33         with open(msg_to_file, 'w') as modified:
     34             modified.write(new_msg + "\n" + data)
     35 
     36 async def image_cb(room, event):
     37     global datestamp
     38     todaysdate = datetime.now().strftime('%A %d %B')
     39     if room.display_name in config.display_name:
     40         media_mxc = event.url
     41         media_url = media_mxc
     42         media_http = await client.mxc_to_http(media_url, config.matrixserver)
     43         print(media_http)
     44         now = datetime.now()
     45         timestamp = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%H:%M")
     46         datestamp_new = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%A %d %B")
     47         time_html = '<span id="time">' + timestamp + '</span>'
     48         user_html = '<span id="user"> ' + room.user_name(event.sender) + ' </span>'
     49         msg_html = '<span id="msg"> ' + event.body + ' </span>'
     50         if datestamp != '' and datestamp != todaysdate and datestamp != datestamp_new:
     51             new_msg =  '<p>' + time_html + user_html + ' ' + '</p>\n' + '<img src="' + str(media_http) + '" style="border-radius:5px;"/>\n'+'<br><h4>'+datestamp+'</h4>\n'
     52         else:
     53             new_msg =  '<p>' + time_html + user_html + ' ' + '</p>\n' + '<img src="' + str(media_http) + '" style="border-radius:5px;"/>\n'
     54         datestamp = datestamp_new
     55         with open(msg_to_file, 'r') as original:
     56             data = original.readlines()[0:100]
     57         data = ''.join(data)
     58         #print(data)
     59         with open(msg_to_file, 'w') as modified:
     60             modified.write(new_msg + "\n" + data)
     61 
     62 async def main():
     63     global client, datestamp
     64     client = AsyncClient(config.matrixserver, config.username)
     65     client.add_event_callback(message_cb, RoomMessageText)
     66     client.add_event_callback(image_cb, RoomMessageImage)
     67     await client.login(config.password)
     68     await client.sync_forever(timeout=30000)
     69 
     70 asyncio.get_event_loop().run_until_complete(main())
     71