Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable experimental alt text attribute, bump versions, add new debug mode for local coding #5

Merged
merged 9 commits into from Dec 8, 2022
2 changes: 1 addition & 1 deletion Procfile
@@ -1 +1 @@
web: gunicorn app:app --log-file -
web: gunicorn app:app
34 changes: 17 additions & 17 deletions README.md
Expand Up @@ -8,43 +8,39 @@ https://python.microformats.io

All commits to the `master` branch get auto-deployed to the live website (running on [Heroku](https://python.microformats.io))

## Getting Started

## Requirements

- Python 2.7

This website is built using Python Flask.

## Installation

Clone the repo:
To start working with this project, first clone the repository for this project:

```
git clone https://github.com/indieweb/microformats-python-parser-website.git
cd microformats-python-parser-website
```

Install the dependencies:
Next, install the required dependencies:

```
TODO
pip3 install -r requirements.txt
```

Start the server:
Next, start the server. You can do this in either debugging mode (where `debug=True`) in Flask or in production mode using Gunicorn.

```
TODO
python3 app.py --debug (debug mode)
gunicorn app:app (production mode)
```

Open the site in your browser:
You can view your running local application at this URL:

```
TODO
http://localhost:8080
```

## Requirements

## Authors

- Kyle Mahan / [@kylewm](https://github.com/kylewm)
- Python 3.9

## Contributions

Expand All @@ -58,9 +54,13 @@ TODO
If you find bugs, have feature requests or questions, please
[file an issue](https://github.com/indieweb/microformats-parser-website-python/issues).


## License

Microformats Python Parser Website is dedicated to the public domain using Creative Commons -- CC0 1.0 Universal.

http://creativecommons.org/publicdomain/zero/1.0

## Contributors

- Kyle Mahan / [@kylewm](https://github.com/kylewm) (Author)
- [@capjamesg](https://github.com/capjamesg) (Contributor)
65 changes: 41 additions & 24 deletions app.py
@@ -1,57 +1,74 @@
import collections
import json
import sys
import traceback
from collections import OrderedDict
from optparse import OptionParser

import mf2py
import mf2util
from flask import Flask, render_template, jsonify, request, make_response
from flask import Flask, jsonify, make_response, render_template, request

parser = OptionParser()

parser.add_option(
"-d",
"--debug",
action="store_true",
default=False,
help="Run application in debug mode",
)

(options, args) = parser.parse_args()

app = Flask(__name__)

mf2py.Parser.user_agent = "python.microformats.io (mf2py/" + mf2py.__version__ + ") Mozilla/5.0 Chrome/29.0.1547.57 Safari/537.36"
mf2py.Parser.dict_class = collections.OrderedDict
mf2py.Parser.dict_class = OrderedDict
mf2py.Parser.img_with_alt = True

@app.route('/', methods=['GET', 'POST'])
@app.route("/", methods=["GET", "POST"])
def index():
try:
util = request.args.get('util') or request.form.get('util')
url = request.args.get('url') or request.form.get('url')
doc = request.args.get('doc') or request.form.get('doc')
parser = request.args.get('parser') or request.form.get('parser')
callback = request.args.get('callback') or request.form.get('callback')
util = request.args.get("util") or request.form.get("util")
url = request.args.get("url") or request.form.get("url")
doc = request.args.get("doc") or request.form.get("doc")
parser = request.args.get("parser") or request.form.get("parser")
callback = request.args.get("callback") or request.form.get("callback")

cached_mf2 = {}

def fetch_mf2(url):
if url in cached_mf2:
return cached_mf2[url]
p = mf2py.parse(
url=url, html_parser=parser or None)
p = mf2py.parse(url=url, html_parser=parser or None)
cached_mf2[url] = p
return p

if url or doc:
p = mf2py.parse(url=url or None,
doc=doc or None,
html_parser=parser or None)
p = mf2py.parse(
url=url or None, doc=doc or None, html_parser=parser or None
)
if util:
if any('h-feed' in item['type'] for item in p['items']):
if any("h-feed" in item["type"] for item in p["items"]):
p = mf2util.interpret_feed(
p, url, want_json=True, fetch_mf2_func=fetch_mf2)
p, url, want_json=True, fetch_mf2_func=fetch_mf2
)
else:
p = mf2util.interpret(
p, url, want_json=True, fetch_mf2_func=fetch_mf2)
p, url, want_json=True, fetch_mf2_func=fetch_mf2
)
if callback:
response = make_response('{}({})'.format(callback, json.dumps(p)), 200)
response.headers['Content-Type'] = 'text/javascript'
response = make_response("{}({})".format(callback, json.dumps(p)), 200)
response.headers["Content-Type"] = "text/javascript"
else:
response = make_response(json.dumps(p, indent=True), 200)
response.headers['Content-Type'] = 'application/json'
response.headers["Content-Type"] = "application/json"
return response

return render_template('index.jinja2',
mf2py_version=mf2py.__version__)
return render_template("index.jinja2", mf2py_version=mf2py.__version__)
except BaseException as e:
traceback.print_exc()
return jsonify(error='%s: %s' % (type(e).__name__, e)), 400
return jsonify(error="%s: %s" % (type(e).__name__, e)), 400


if options.debug:
app.run(debug=True, port=8080)
37 changes: 18 additions & 19 deletions requirements.txt
@@ -1,20 +1,19 @@
appdirs==1.4.3
beautifulsoup4==4.6.0
certifi==2017.4.17
chardet==3.0.3
Flask==0.10.1
gunicorn==19.7.1
html5lib==0.999999999
idna==2.5
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
mf2py==1.1.2
mf2util==0.5.0
packaging==16.8
pyparsing==2.2.0
requests==2.16.4
six==1.10.0
urllib3==1.21.1
beautifulsoup4==4.11.1
certifi==2022.9.14
charset-normalizer==2.1.1
click==8.1.3
Flask==2.2.2
gunicorn==20.1.0
html5lib==1.1
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
mf2py==1.1.0
mf2util==0.5.1
requests==2.28.1
six==1.16.0
soupsieve==2.3.2.post1
urllib3==1.26.12
webencodings==0.5.1
Werkzeug==0.11.3
Werkzeug==2.2.2
2 changes: 1 addition & 1 deletion runtime.txt
@@ -1 +1 @@
python-2.7.15
python-3.9.14