Code naming and spacing improvement in converter

This commit is contained in:
Gil Gonçalves
2014-01-17 23:53:41 +00:00
parent 83bfd9b872
commit 27ac7d1eb7
+10 -4
View File
@@ -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/')