Skip to content

Commit a51ceed

Browse files
author
Kyle Mahan
committed
initial checkin
0 parents  commit a51ceed

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
venv
2+
__pycache__
3+
*.pyc

app.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import collections
2+
import json
3+
import sys
4+
import traceback
5+
6+
import mf2py
7+
import mf2util
8+
from flask import Flask, render_template, jsonify, request, make_response
9+
10+
app = Flask(__name__)
11+
12+
mf2py.Parser.user_agent = 'mf2.kylewm.com (mf2py v' + mf2py.__version__ + ')'
13+
mf2py.Parser.dict_class = collections.OrderedDict
14+
15+
@app.route('/', methods=['GET', 'POST'])
16+
def index():
17+
try:
18+
util = request.args.get('util') or request.form.get('util')
19+
url = request.args.get('url') or request.form.get('url')
20+
doc = request.args.get('doc') or request.form.get('doc')
21+
parser = request.args.get('parser') or request.form.get('parser')
22+
callback = request.args.get('callback') or request.form.get('callback')
23+
24+
cached_mf2 = {}
25+
26+
def fetch_mf2(url):
27+
if url in cached_mf2:
28+
return cached_mf2[url]
29+
p = mf2py.parse(
30+
url=url, html_parser=parser or None)
31+
cached_mf2[url] = p
32+
return p
33+
34+
if url or doc:
35+
p = mf2py.parse(url=url or None,
36+
doc=doc or None,
37+
html_parser=parser or None)
38+
if util:
39+
if any('h-feed' in item['type'] for item in p['items']):
40+
p = mf2util.interpret_feed(
41+
p, url, want_json=True, fetch_mf2_func=fetch_mf2)
42+
else:
43+
p = mf2util.interpret(
44+
p, url, want_json=True, fetch_mf2_func=fetch_mf2)
45+
if callback:
46+
response = make_response('{}({})'.format(callback, json.dumps(p)), 200)
47+
response.headers['Content-Type'] = 'text/javascript'
48+
else:
49+
response = make_response(json.dumps(p, indent=True), 200)
50+
response.headers['Content-Type'] = 'application/json'
51+
return response
52+
53+
return render_template('index.jinja2',
54+
mf2py_version=mf2py.__version__)
55+
except BaseException as e:
56+
traceback.print_exc()
57+
return jsonify(error='%s: %s' % (type(e).__name__, e)), 400

maybe_upgrade.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
SCRIPT=$(readlink -m "$0")
3+
4+
cd "$(dirname "$SCRIPT")"
5+
source venv/bin/activate
6+
7+
if [ -n "$(pip list -o | grep -e mf2py -e mf2util)" ]; then
8+
echo "upgrading mf2py"
9+
pip install --upgrade mf2py mf2util
10+
restart mf2
11+
fi

mf2.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[uwsgi]
2+
master=true
3+
socket=/tmp/mf2.sock
4+
chmod-socket=666
5+
module=app:app
6+
7+
threads=2
8+
cheaper-algo=spare
9+
cheaper=1
10+
cheaper-initial=1
11+
workers=10

requirements.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Flask==0.10.1
2+
Jinja2==2.8
3+
MarkupSafe==0.23
4+
Werkzeug==0.11.3
5+
beautifulsoup4==4.4.1
6+
html5lib==0.9999999
7+
itsdangerous==0.24
8+
mf2py==1.0.1
9+
mf2util==0.2.11
10+
requests==2.9.1
11+
six==1.10.0

templates/index.jinja2

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>mf2py and utilities</title>
5+
<style>
6+
body {
7+
font-family: sans;
8+
background: #ddd;
9+
}
10+
.container {
11+
width: 100%;
12+
max-width: 640px;
13+
margin: 1em auto;
14+
background: white;
15+
border: 1px solid black;
16+
border-radius: 5px;
17+
padding: 1em;
18+
}
19+
.container + .container {
20+
margin-top: 1em;
21+
}
22+
h1 {
23+
1.5em;
24+
margin: 5px;
25+
}
26+
label {
27+
font-weight: bold;
28+
margin: 10px 0 0 0;
29+
display: block;
30+
}
31+
button {
32+
margin: 10px 0 0 0;
33+
}
34+
textarea, input[type="url"] {
35+
display: block;
36+
width: 90%;
37+
}
38+
select {
39+
display: block;
40+
}
41+
textarea {
42+
height: 5em;
43+
}
44+
</style>
45+
</head>
46+
<body>
47+
48+
<div class="container">
49+
<h1>mf2 from a url</h1>
50+
51+
Using mf2py version {{ mf2py_version }}
52+
53+
<form action="/" method="GET">
54+
<label>URL</label>
55+
<input type="url" name="url" placeholder="http://..." />
56+
<label>Parser</label>
57+
<select name="parser">
58+
<option>html5lib</option>
59+
<option>lxml</option>
60+
<option>html.parser</option>
61+
</select>
62+
<label title="mf2util interprets certain post types (currently h-entry and h-event) and returns a normalized result">
63+
<input type="checkbox" name="util" value="true"> Process with mf2util
64+
</label>
65+
<button type="submit">Convert</button>
66+
</form>
67+
</div>
68+
69+
<div class="container">
70+
<h1>mf2 from a document</h1>
71+
<form action="/" method="POST">
72+
<label>HTML</label>
73+
<textarea name="doc"></textarea>
74+
<label>Base URL</label>
75+
<input type="url" name="url" placeholder="http://..." />
76+
<label>Parser</label>
77+
<select name="parser">
78+
<option>html5lib</option>
79+
<option>lxml</option>
80+
<option>html.parser</option>
81+
</select>
82+
<button type="submit">Convert</button>
83+
</form>
84+
</div>
85+
86+
87+
88+
</body>
89+
</html>

0 commit comments

Comments
 (0)