From: W. Trevor King Date: Sat, 20 Sep 2014 19:51:37 +0000 (-0700) Subject: nmhive.py: Add GET /gmane// X-Git-Tag: v0.1.0~36 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c9cd99947fb7a01f28d2a16d171558f429b19ebf;p=nmhive.git nmhive.py: Add GET /gmane// Gmane's download endpoint [1] doesn't allow cross-origin requests, and article.gmane.org -> download.gmane.org is a cross-origin request [2]. Work around that with this proxy endpoint, which uses Flask-Cors [3] to accept all origins. The bookmarklet can figure out the current message's group and article id, and hit this endpoint. Then nmhive will use Gmane's download endpoint to fetch the message as an mbox, after which we can use Python's stdlib to extract the Message-ID from the mbox, and return the extracted Message-ID to the bookmarklet. Later on we can also add local caching and rate-limiting here, so we don't bother Gmane more than necessary (the downloads are somewhat expensive [1]). [1]: http://gmane.org/export.php [2]: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Definition_of_an_origin [3]: https://pypi.python.org/pypi/Flask-Cors/ --- diff --git a/nmhive.py b/nmhive.py new file mode 100755 index 0000000..6f12084 --- /dev/null +++ b/nmhive.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import json +import mailbox +import tempfile +import urllib.request + +import flask +import flask_cors + + +app = flask.Flask(__name__) +flask_cors.CORS(app) + + +@app.route('/gmane//', methods=['GET']) +def gmane_message_id(group, article): + url = 'http://download.gmane.org/{}/{}/{}'.format( + group, article, article + 1) + response = urllib.request.urlopen(url=url, timeout=3) + mbox_bytes = response.read() + with tempfile.NamedTemporaryFile(prefix='nmbug-', suffix='.mbox') as f: + f.write(mbox_bytes) + mbox = mailbox.mbox(path=f.name) + _, message = mbox.popitem() + message_id = message['message-id'] + return flask.Response( + response=message_id.lstrip('<').rstrip('>'), + mimetype='text/plain') + + +if __name__ == '__main__': + app.debug = True + app.run(host='0.0.0.0')