From c9cd99947fb7a01f28d2a16d171558f429b19ebf Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 20 Sep 2014 12:51:37 -0700 Subject: [PATCH] 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/ --- nmhive.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 nmhive.py 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') -- 2.26.2