Files
markdownplease.com/converter.py
T
Gilberto Gonçalves 3ce06e39bf Now this project features a mini paranoid browser
* Changed the names of some functions;
* Bootstrapped it up;
* Added the capability of being a mini-browser by converting the
markdown to HTML by default via the html app, if you curl it, it
should give you the markdown as always.
* Some prettification of the templates
2013-11-18 22:18:37 +00:00

34 lines
802 B
Python

# -*- coding: utf-8 -*-
import os
import requests
from html2text import html2text
_READABILITY_URL = 'https://www.readability.com/api/content/v1/parser'
def readability(url):
token = os.environ.get('READABILITY_TOKEN')
params = {'url': url, 'token': token}
r = requests.get(_READABILITY_URL, params=params)
return r.json()['content'], r.json()['title']
def convert(html, title=None):
if title:
title = '# {}'.format(title)
html = '\n\n'.join([title, html])
return html2text(html)
def get_readable_content_from_url(url):
try:
content, title = readability(url)
return convert(content, title=title)
except KeyError:
return None
if __name__ == '__main__':
print get_readable_content_from_url('http://kennethreitz.org/')