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,76 +108,70 @@ 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 = []
for post in rdata['data']: pqueue = []
if post['user']['username'] == app.config['MATRIX_PNUT_USER']: for msg in messages:
continue
if post['source']['id'] == app.config['PNUTCLIENT_ID']:
continue
if 'content' in post: # bypass messages posted by the bridge to avoid duplicates and loops
user = self.generate_matrix_id(post['user']['username']) if msg.user.username == app.config['MATRIX_PNUT_USER']:
text = post['content']['text'] + "\n" continue
dt = datetime.datetime.strptime(post["created_at"], "%Y-%m-%dT%H:%M:%SZ") if msg.source.id == app.config['PNUTCLIENT_ID']:
ts = time.mktime(dt.timetuple()) continue
msgid = post['id']
puser = post['user']['username']
# handle entities user = self.generate_matrix_id(msg.user.username)
if 'entities' in post['content']: text = msg.content.text + "\n"
if 'links' in post['content']['entities']: # dt = datetime.datetime.strptime(msg.created_at, "%Y-%m-%dT%H:%M:%SZ")
for lnk in post['content']['entities']['links']: dt = msg.created_at
text += "\n" ts = time.mktime(dt.timetuple())
if 'title' in lnk:
text += lnk['title'] + "\n"
if 'link' in lnk:
text += lnk['link'] + "\n"
# handle raw data for lnk in msg.content.entities.links:
if 'raw' in post: text += "\n"
for raw in post['raw']: if 'title' in lnk:
text += "\n" text += lnk.title + "\n"
if raw['type'] == 'io.pnut.core.oembed': if 'link' in lnk:
if 'title' in raw['value']: text += lnk.link + "\n"
text += raw['value']['title'] + "\n"
if 'url' in raw['value']:
text += raw['value']['url'] + "\n"
pqueue.insert(0, for raw in msg.raw:
{'user':user,'text':text,'ts':ts,'msgid':msgid,'puser':puser,'chan':room.pnut_chan}) if raw.type == 'io.pnut.core.oembed':
if 'title' in raw.value:
text += raw.value.title + "\n"
if 'url' in raw.value:
text += raw.value.url + "\n"
if len(rdata["data"]) > 0: # queue the message in reverse order (because of how pnut returns the messages)
room.pnut_since = rdata["data"][0]["id"] pqueue.insert(0, {'user':user,'text':text,'ts':ts,'msgid':msg.id,'puser':msg.user.username,'chan':room.pnut_chan})
db.session.commit()
for item in pqueue: # update the last message id
self.txId += 1 if len(messages) > 0:
room.pnut_since = messages[0].id
db.session.commit()
d = self.is_registered(item['user']['user_id']) # empty the queue to the matrix room
if not d: for item in pqueue:
self.create_matrix_user(item['user']['username']) self.txId += 1
if d != item['user']['displayname']: d = self.is_registered(item['user']['user_id'])
self.set_displayname(item['user']) if not d:
self.create_matrix_user(item['user']['username'])
self.join_room(item['user']['user_id'], room.room_id) if d != item['user']['displayname']:
time.sleep(1) self.set_displayname(item['user'])
self.send_message(room.room_id, item) self.join_room(item['user']['user_id'], room.room_id)
time.sleep(1)
self.send_message(room.room_id, item)
def run(self): def run(self):