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,62 +108,56 @@ 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 msg in messages:
for post in rdata['data']:
if post['user']['username'] == app.config['MATRIX_PNUT_USER']:
# bypass messages posted by the bridge to avoid duplicates and loops
if msg.user.username == app.config['MATRIX_PNUT_USER']:
continue
if post['source']['id'] == app.config['PNUTCLIENT_ID']:
if msg.source.id == app.config['PNUTCLIENT_ID']:
continue
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")
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())
msgid = post['id']
puser = post['user']['username']
# handle entities
if 'entities' in post['content']:
if 'links' in post['content']['entities']:
for lnk in post['content']['entities']['links']:
for lnk in msg.content.entities.links:
text += "\n"
if 'title' in lnk:
text += lnk['title'] + "\n"
text += lnk.title + "\n"
if 'link' in lnk:
text += lnk['link'] + "\n"
text += lnk.link + "\n"
# 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 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"
pqueue.insert(0,
{'user':user,'text':text,'ts':ts,'msgid':msgid,'puser':puser,'chan':room.pnut_chan})
# 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})
if len(rdata["data"]) > 0:
room.pnut_since = rdata["data"][0]["id"]
# update the last message id
if len(messages) > 0:
room.pnut_since = messages[0].id
db.session.commit()
# empty the queue to the matrix room
for item in pqueue:
self.txId += 1