mirror of
https://github.com/not-kennethreitz/markdownplease.com.git
synced 2026-06-05 23:20:19 +00:00
3ce06e39bf
* 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
34 lines
802 B
Python
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/')
|