Skip to content

Commit 7193d1e

Browse files
authored
Simplify wikibot a bit. (#620)
1 parent ae8dccb commit 7193d1e

File tree

2 files changed

+39
-50
lines changed

2 files changed

+39
-50
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Flask==0.11.1
22
requests==2.11.1
33
sleekxmpp==1.3.1
4+
six==1.10.0

compute/xmpp_wikibot/wikibot.py

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
import argparse
1818
import getpass
1919
import json
20-
import logging
21-
import threading
20+
import re
21+
import sys
2222
import urllib
2323

2424
from flask import Flask, request
2525
import requests
26+
from six.moves import html_parser
2627
import sleekxmpp
2728

28-
2929
app = Flask(__name__)
3030

31+
3132
@app.route('/send_message', methods=['GET'])
3233
def send_message():
3334
recipient = request.args.get('recipient')
@@ -37,14 +38,9 @@ def send_message():
3738
chat_client.send_message(mto=recipient, mbody=message)
3839
return 'message sent to {} with body: {}'.format(recipient, message)
3940
else:
40-
logging.info('chat client or recipient or message does not exist!')
4141
return 'message failed to send', 400
4242

4343

44-
def run_server(host='0.0.0.0'):
45-
app.run(threaded=False, use_reloader=False, host=host)
46-
47-
4844
class WikiBot(sleekxmpp.ClientXMPP):
4945
"""A simple SleekXMPP bot that will take messages, look up their content on
5046
wikipedia and provide a link to the page if it exists.
@@ -65,6 +61,13 @@ def __init__(self, jid, password):
6561
# MUC messages and error messages.
6662
self.add_event_handler('message', self.message)
6763

64+
# Register plugins. Note that while plugins may have
65+
# interdependencies, the order you register them in doesn't matter.
66+
self.register_plugin('xep_0030') # Service Discovery
67+
self.register_plugin('xep_0004') # Data Forms
68+
self.register_plugin('xep_0060') # PubSub
69+
self.register_plugin('xep_0199') # XMPP Ping
70+
6871
def start(self, event):
6972
"""Process the session_start event.
7073
@@ -94,22 +97,27 @@ def message(self, msg):
9497
"""
9598
if msg['type'] in ('chat', 'normal'):
9699
msg_body = msg['body']
97-
logging.info('Message sent was: {}'.format(msg_body))
98100
encoded_body = urllib.quote_plus(msg_body)
99-
svrResponse = requests.get(
101+
response = requests.get(
100102
'https://en.wikipedia.org/w/api.php?'
101-
'action=parse&prop=sections&format=json&page={}'.format(
102-
encoded_body))
103-
doc = json.loads(svrResponse.content)
104-
try:
105-
page_id = str(doc['parse']['pageid'])
106-
defn_url = 'https://en.wikipedia.org/?curid={}'.format(page_id)
107-
msg.reply('find out more about: "{}" here: {}'.format(
108-
msg_body, defn_url)).send()
109-
except KeyError as e:
110-
logging.info('key error: {0}'.format(e))
103+
'action=query&list=search&format=json&srprop=snippet&'
104+
'srsearch={}'.format(encoded_body))
105+
doc = json.loads(response.content)
106+
107+
results = doc.get('query', {}).get('search')
108+
if not results:
111109
msg.reply('I wasn\'t able to locate info on "{}" Sorry'.format(
112110
msg_body)).send()
111+
return
112+
113+
snippet = results[0]['snippet']
114+
title = urllib.quote_plus(results[0]['title'])
115+
116+
# Strip out html
117+
snippet = html_parser.HTMLParser().unescape(
118+
re.sub(r'<[^>]*>', '', snippet))
119+
msg.reply(u'{}...\n(http://en.wikipedia.org/w/?title={})'.format(
120+
snippet, title)).send()
113121

114122

115123
if __name__ == '__main__':
@@ -118,48 +126,28 @@ def message(self, msg):
118126
description=__doc__,
119127
formatter_class=argparse.RawDescriptionHelpFormatter)
120128

121-
# Output verbosity options.
122-
parser.add_argument(
123-
'-q', '--quiet', help='set logging to ERROR', action='store_const',
124-
dest='loglevel', const=logging.ERROR, default=logging.INFO)
125-
parser.add_argument(
126-
'-d', '--debug', help='set logging to DEBUG', action='store_const',
127-
dest='loglevel', const=logging.DEBUG, default=logging.INFO)
128-
parser.add_argument(
129-
'-v', '--verbose', help='set logging to COMM', action='store_const',
130-
dest='loglevel', const=5, default=logging.INFO)
131-
132129
# JID and password options.
133130
parser.add_argument('-j', '--jid', help='JID to use', required=True)
134131
parser.add_argument('-p', '--password', help='password to use')
135132

136133
args = parser.parse_args()
137134

138-
# Setup logging.
139-
logging.basicConfig(level=args.loglevel,
140-
format='%(levelname)-8s %(message)s')
141-
142135
if args.password is None:
143136
args.password = getpass.getpass('Password: ')
144137

145-
# Setup the WikiBot and register plugins. Note that while plugins may
146-
# have interdependencies, the order in which you register them does
147-
# not matter.
148138
xmpp = WikiBot(args.jid, args.password)
149-
xmpp.register_plugin('xep_0030') # Service Discovery
150-
xmpp.register_plugin('xep_0004') # Data Forms
151-
xmpp.register_plugin('xep_0060') # PubSub
152-
xmpp.register_plugin('xep_0199') # XMPP Ping
153139

154140
chat_client = xmpp # set the global variable
155141

156-
# start the app server and run it as a thread so that the XMPP server may
157-
# also start
158-
threading.Thread(target=run_server).start()
142+
try:
143+
# Connect to the XMPP server and start processing XMPP stanzas.
144+
if not xmpp.connect():
145+
print('Unable to connect.')
146+
sys.exit(1)
147+
148+
xmpp.process(block=False)
159149

160-
# Connect to the XMPP server and start processing XMPP stanzas.
161-
if xmpp.connect():
162-
xmpp.process(block=True)
150+
app.run(threaded=True, use_reloader=False, host='0.0.0.0', debug=False)
163151
print('Done')
164-
else:
165-
print('Unable to connect.')
152+
finally:
153+
xmpp.disconnect()

0 commit comments

Comments
 (0)