Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Last active April 9, 2022 07:39
Show Gist options
  • Save psychemedia/15be9d3101b6923b506e to your computer and use it in GitHub Desktop.
Save psychemedia/15be9d3101b6923b506e to your computer and use it in GitHub Desktop.
UK Parliament api test and members data platform OData service test
import json, re
import urllib2
from urlparse import urlparse
from urllib import urlopen, urlencode
class UKParliamentReader():
"""
Chat to the UK Parliament API
"""
def __init__(self):
""" Need to think more about the structure of this... """
pass
def qpatch(self,query):
t=[]
for a in query.split('or'):
t.append('({})'.format(a.strip().replace(' ',' AND ')))
return ' OR '.join(t)
def search_one(self,query, typ='Research Papers',page=0,ps=100):
url='http://lda.data.parliament.uk/researchbriefings.json'
urlargs={'_view':typ,'_pageSize':ps,'_search':self.qpatch(query),'_page':page}
url='{}?{}'.format(url, urlencode(urlargs))
data =json.loads(urlopen(url).read())
response=[]
for i in data['result']['items']:
response.append("{} [http://researchbriefings.parliament.uk/ResearchBriefing/Summary/{}']".format( i['title'],i['identifier']['_value']))
return response
def search_all(self,query, typ='Research Papers',ps=100):
return
def responder(self,hook):
ukparl_token=hook['env']['ukparl_token']
ukparl_url=hook['env']['ukparl_url']
r=self.search_one(Hook['params']['text'])
r2="; \n".join(r)
payload={"channel": "#slashtest", "username": "parlibot",
"text":"I know about the following Parliamentary research papers:\n\n {}".format(r2)}
req = urllib2.Request(ukparl_url)
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(payload))
u=UKParliamentReader()
if Hook['params']['token'] == Hook['env']['ukparl_token']:
u.responder(Hook)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment