aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--slack.py12
-rw-r--r--xkcd.py12
2 files changed, 24 insertions, 0 deletions
diff --git a/slack.py b/slack.py
index e919637..c6c9faf 100644
--- a/slack.py
+++ b/slack.py
@@ -1,4 +1,5 @@
from flask import Flask, jsonify, request
+from xkcd import get_xkcd
app = Flask(__name__)
app.config.from_envvar('CONF')
@@ -12,5 +13,16 @@ def echo():
}
return jsonify(d)
+@app.route("/xkcd", methods=['POST'])
+def xkcd_view():
+ title, url = get_xkcd(request.form['text'])
+ d = {'response_type': 'in_channel',
+ 'text': title,
+ 'attachments': [
+ {'image_url':"http:{0}:".format(url)}
+ ]
+ }
+ return jsonify(d)
+
if __name__=="__main__":
app.run()
diff --git a/xkcd.py b/xkcd.py
new file mode 100644
index 0000000..3bbac65
--- /dev/null
+++ b/xkcd.py
@@ -0,0 +1,12 @@
+import requests
+import bs4
+
+def get_xkcd(comicid):
+ r = requests.get('http://www.xkcd.org/{0}/'.format(comicid))
+ if r.status_code == 200:
+ soup = bs4.BeautifulSoup(r.content)
+ img = soup.find("div", {'id':"comic"}).find("img")
+ return img['title'], img['src']
+
+if __name__=="__main__":
+ print(get_xkcd(1600))