Make code re-useable

This commit is contained in:
Kovid Goyal 2024-04-28 14:16:55 +05:30
parent d37f436c4c
commit a067f1d519
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -9,7 +9,7 @@ from xml.sax.saxutils import escape, quoteattr
from calibre.utils.iso8601 import parse_iso8601
module_version = 4 # needed for live updates
module_version = 5 # needed for live updates
pprint
@ -185,15 +185,12 @@ def extract_html(soup):
return json_to_html(raw)
def download_url(url=None, br=None):
# Get the URL from the Wayback machine
def download_url_from_wayback(category, url, br=None):
from mechanize import Request
host = 'http://localhost:8090'
host = 'https://wayback1.calibre-ebook.com'
if url is None:
url = sys.argv[-1]
rq = Request(
host + '/nytimes',
host + '/' + category,
data=json.dumps({"url": url}),
headers={'User-Agent': 'calibre', 'Content-Type': 'application/json'}
)
@ -204,6 +201,13 @@ def download_url(url=None, br=None):
return br.open_novisit(rq, timeout=3 * 60).read()
def download_url(url=None, br=None):
# Get the URL from the Wayback machine
if url is None:
url = sys.argv[-1]
return download_url_from_wayback('nytimes', url, br)
if __name__ == '__main__':
f = sys.argv[-1]
raw = open(f).read()