replace custom pnutlib with pnutpy module for polling pnut channels

issue #17
This commit is contained in:
Morgan McMillian 2018-01-09 16:28:53 -08:00
parent b9ad6ea08a
commit 10209348b3

View file

@ -6,8 +6,8 @@ import time
import datetime import datetime
import requests import requests
import json import json
import pnutpy
from pnutlib import Pnut
from appservice import app from appservice import app
from models import * from models import *
@ -17,7 +17,7 @@ class ChannelMonitor(threading.Thread):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.pnut = Pnut(app.config['MATRIX_PNUT_TOKEN']) pnutpy.api.add_authorization_token(app.config['MATRIX_PNUT_TOKEN'])
self.matrix_api_url = app.config['MATRIX_HOST'] + '/_matrix/client/r0' self.matrix_api_url = app.config['MATRIX_HOST'] + '/_matrix/client/r0'
self.matrix_api_token = app.config['MATRIX_AS_TOKEN'] self.matrix_api_token = app.config['MATRIX_AS_TOKEN']
self.txId = 0 self.txId = 0
@ -108,62 +108,56 @@ class ChannelMonitor(threading.Thread):
db.session.commit() db.session.commit()
def poll_channel(self, room): def poll_channel(self, room):
try: try:
r = self.pnut.get_channel_stream(room.pnut_chan, room.pnut_since) messages, meta = pnutpy.api.get_channel_messages(room.pnut_chan, since_id=room.pnut_since, include_raw=1)
print(r.headers['X-RateLimit-Remaining'], r.headers['X-RateLimit-Reset']) except pnutpy.errors.PnutRateLimitAPIException:
except requests.exceptions.ConnectionError: logging.warning('*** Rate limit error while trying to fetch messages! Waiting to retry. ***')
logging.info('*** Problem connecting to pnut.io! Waiting to retry. ***')
time.sleep(30) time.sleep(30)
return return
except: except:
logging.info('*** Registered some other error! Waiting to retry. ***') logging.warning('*** An error occured while trying to fetch messages! Waiting to retry. ***')
time.sleep(30) time.sleep(30)
return return
if r is not None and r.status_code == 200:
rdata = r.json()
pqueue = [] pqueue = []
for msg in messages:
for post in rdata['data']: # bypass messages posted by the bridge to avoid duplicates and loops
if post['user']['username'] == app.config['MATRIX_PNUT_USER']: if msg.user.username == app.config['MATRIX_PNUT_USER']:
continue continue
if post['source']['id'] == app.config['PNUTCLIENT_ID']: if msg.source.id == app.config['PNUTCLIENT_ID']:
continue continue
if 'content' in post: user = self.generate_matrix_id(msg.user.username)
user = self.generate_matrix_id(post['user']['username']) text = msg.content.text + "\n"
text = post['content']['text'] + "\n" # dt = datetime.datetime.strptime(msg.created_at, "%Y-%m-%dT%H:%M:%SZ")
dt = datetime.datetime.strptime(post["created_at"], "%Y-%m-%dT%H:%M:%SZ") dt = msg.created_at
ts = time.mktime(dt.timetuple()) ts = time.mktime(dt.timetuple())
msgid = post['id']
puser = post['user']['username']
# handle entities for lnk in msg.content.entities.links:
if 'entities' in post['content']:
if 'links' in post['content']['entities']:
for lnk in post['content']['entities']['links']:
text += "\n" text += "\n"
if 'title' in lnk: if 'title' in lnk:
text += lnk['title'] + "\n" text += lnk.title + "\n"
if 'link' in lnk: if 'link' in lnk:
text += lnk['link'] + "\n" text += lnk.link + "\n"
# handle raw data for raw in msg.raw:
if 'raw' in post: if raw.type == 'io.pnut.core.oembed':
for raw in post['raw']: if 'title' in raw.value:
text += "\n" text += raw.value.title + "\n"
if raw['type'] == 'io.pnut.core.oembed': if 'url' in raw.value:
if 'title' in raw['value']: text += raw.value.url + "\n"
text += raw['value']['title'] + "\n"
if 'url' in raw['value']:
text += raw['value']['url'] + "\n"
pqueue.insert(0, # queue the message in reverse order (because of how pnut returns the messages)
{'user':user,'text':text,'ts':ts,'msgid':msgid,'puser':puser,'chan':room.pnut_chan}) pqueue.insert(0, {'user':user,'text':text,'ts':ts,'msgid':msg.id,'puser':msg.user.username,'chan':room.pnut_chan})
if len(rdata["data"]) > 0: # update the last message id
room.pnut_since = rdata["data"][0]["id"] if len(messages) > 0:
room.pnut_since = messages[0].id
db.session.commit() db.session.commit()
# empty the queue to the matrix room
for item in pqueue: for item in pqueue:
self.txId += 1 self.txId += 1