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 requests
import json
import pnutpy
from pnutlib import Pnut
from appservice import app
from models import *
@ -17,7 +17,7 @@ class ChannelMonitor(threading.Thread):
def __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_token = app.config['MATRIX_AS_TOKEN']
self.txId = 0
@ -108,76 +108,70 @@ class ChannelMonitor(threading.Thread):
db.session.commit()
def poll_channel(self, room):
try:
r = self.pnut.get_channel_stream(room.pnut_chan, room.pnut_since)
print(r.headers['X-RateLimit-Remaining'], r.headers['X-RateLimit-Reset'])
except requests.exceptions.ConnectionError:
logging.info('*** Problem connecting to pnut.io! Waiting to retry. ***')
messages, meta = pnutpy.api.get_channel_messages(room.pnut_chan, since_id=room.pnut_since, include_raw=1)
except pnutpy.errors.PnutRateLimitAPIException:
logging.warning('*** Rate limit error while trying to fetch messages! Waiting to retry. ***')
time.sleep(30)
return
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)
return
if r is not None and r.status_code == 200:
rdata = r.json()
pqueue = []
for post in rdata['data']:
if post['user']['username'] == app.config['MATRIX_PNUT_USER']:
continue
if post['source']['id'] == app.config['PNUTCLIENT_ID']:
continue
pqueue = []
for msg in messages:
if 'content' in post:
user = self.generate_matrix_id(post['user']['username'])
text = post['content']['text'] + "\n"
dt = datetime.datetime.strptime(post["created_at"], "%Y-%m-%dT%H:%M:%SZ")
ts = time.mktime(dt.timetuple())
msgid = post['id']
puser = post['user']['username']
# bypass messages posted by the bridge to avoid duplicates and loops
if msg.user.username == app.config['MATRIX_PNUT_USER']:
continue
if msg.source.id == app.config['PNUTCLIENT_ID']:
continue
# handle entities
if 'entities' in post['content']:
if 'links' in post['content']['entities']:
for lnk in post['content']['entities']['links']:
text += "\n"
if 'title' in lnk:
text += lnk['title'] + "\n"
if 'link' in lnk:
text += lnk['link'] + "\n"
user = self.generate_matrix_id(msg.user.username)
text = msg.content.text + "\n"
# dt = datetime.datetime.strptime(msg.created_at, "%Y-%m-%dT%H:%M:%SZ")
dt = msg.created_at
ts = time.mktime(dt.timetuple())
# handle raw data
if 'raw' in post:
for raw in post['raw']:
text += "\n"
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"
for lnk in msg.content.entities.links:
text += "\n"
if 'title' in lnk:
text += lnk.title + "\n"
if 'link' in lnk:
text += lnk.link + "\n"
pqueue.insert(0,
{'user':user,'text':text,'ts':ts,'msgid':msgid,'puser':puser,'chan':room.pnut_chan})
for raw in msg.raw:
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:
room.pnut_since = rdata["data"][0]["id"]
db.session.commit()
# queue the message in reverse order (because of how pnut returns the messages)
pqueue.insert(0, {'user':user,'text':text,'ts':ts,'msgid':msg.id,'puser':msg.user.username,'chan':room.pnut_chan})
for item in pqueue:
self.txId += 1
# update the last message id
if len(messages) > 0:
room.pnut_since = messages[0].id
db.session.commit()
d = self.is_registered(item['user']['user_id'])
if not d:
self.create_matrix_user(item['user']['username'])
# empty the queue to the matrix room
for item in pqueue:
self.txId += 1
if d != item['user']['displayname']:
self.set_displayname(item['user'])
d = self.is_registered(item['user']['user_id'])
if not d:
self.create_matrix_user(item['user']['username'])
self.join_room(item['user']['user_id'], room.room_id)
time.sleep(1)
if d != item['user']['displayname']:
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):