mirror of
https://github.com/not-kennethreitz/markdownplease.com.git
synced 2026-06-05 23:20:19 +00:00
Code naming and spacing improvement in converter
This commit is contained in:
+10
-4
@@ -5,9 +5,11 @@ import os
|
||||
import requests
|
||||
from html2text import html2text
|
||||
|
||||
|
||||
_READABILITY_URL = 'https://www.readability.com/api/content/v1/parser'
|
||||
|
||||
def _readability(url):
|
||||
|
||||
def _get_readability_html_and_title(url):
|
||||
token = os.environ.get('READABILITY_TOKEN')
|
||||
params = {'url': url, 'token': token}
|
||||
|
||||
@@ -18,7 +20,8 @@ def _readability(url):
|
||||
)
|
||||
return decoded_content
|
||||
|
||||
def _convert(html, title=None):
|
||||
|
||||
def _convert_html_to_markdown(html, title=None):
|
||||
if title:
|
||||
title = '# {}'.format(title)
|
||||
html = '\n\n'.join([title, html])
|
||||
@@ -26,13 +29,16 @@ def _convert(html, title=None):
|
||||
text_from_html = html2text(html)
|
||||
return text_from_html
|
||||
|
||||
|
||||
def get_readable_content_from_url(url):
|
||||
try:
|
||||
content, title = _readability(url)
|
||||
return _convert(content, title=title)
|
||||
content, title = _get_readability_html_and_title(url)
|
||||
markdown = _convert_html_to_markdown(content, title=title)
|
||||
return markdown
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print get_readable_content_from_url('http://kennethreitz.org/')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user