From 3f425bf5507ad926d7f117b9da105f5a0a60c459 Mon Sep 17 00:00:00 2001 From: Zsolt Botykai Date: Tue, 11 Jun 2024 20:08:05 +0200 Subject: [PATCH] linted --- o2e.py | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/o2e.py b/o2e.py index 500cb46..d17d2ef 100755 --- a/o2e.py +++ b/o2e.py @@ -1,23 +1,41 @@ #!/usr/bin/env python -import sys, json, datetime +""" +So this simple sciprt relies on the following python packages: + +- loguru +- omnivoreq +- mkepub + +You also need to have an Omnivore API key (saved in an `API.key` file) +in the same directory as the script itself, and a `cover.jpg` file too. + +**NOTE** +The generated EPUB file is not always accepted by Amazon's send to kindle +service, because there might be some validation errors. +""" +import sys +import datetime from loguru import logger from omnivoreql import OmnivoreQL import mkepub now = datetime.datetime.now() today = now.strftime("%Y-%m-%d") -lf = "{time:YYYY-MM-DD HH:mm:ss} {level} {file:<20} {line:4} {message} " +LF = "{time:YYYY-MM-DD HH:mm:ss} {level} {file:<20} {line:4} {message} " -logger.add("o2e.log", rotation="2 days", compression="zip", format=lf) +logger.add("o2e.log", rotation="2 days", compression="zip", format=LF) @logger.catch(onerror=lambda _: sys.exit(1)) def main(): + """ + This is the business part. + """ token = open('API.key', 'r').read().strip() logger.debug("API key found") - + book = mkepub.Book(title='OmniVore saved articles') - + oc = OmnivoreQL(token) profile = oc.get_profile() user=profile['me']['profile']['username'] @@ -25,7 +43,7 @@ def main(): # limit: number of articles # query: newest by the bellow setting, can be changed to saved asc articles = oc.get_articles(limit=20, query='sort:saved-desc') - + for a in articles['search']['edges']: article = oc.get_article(username=user, slug=a['node']['slug']) if article['article']['article']['isArchived'] is False: @@ -33,29 +51,28 @@ def main(): content = '' title = article['article']['article']['title'] title = title.replace('&','&') - id = article['article']['article']['id'] + aid = article['article']['article']['id'] url = article['article']['article']['url'] author = article['article']['article']['author'] labels = article['article']['article']['labels'] - - if not author: author = "No author" - if not title: author = "No title" - if not url: author = "No URL" + if not author: author = "No author" + if not title: author = "No title" + if not url: author = "No URL" logger.debug("Title: " + title) - logger.debug("ID: " + id) + logger.debug("ID: " + aid) if labels and 'name' in labels[0]: labellist = [dict['name'] for dict in labels] clabel = ', '.join(['#' + str(e) + ' ' for e in labellist]) - if author: + if author: content += '

' + author + '

' - if title: + if title: content += '

' + title + '

' - if url: + if url: content += '
' + url + '
' if clabel: @@ -64,8 +81,8 @@ def main(): content += article['article']['article']['content'] book.add_page(title,content) - - oc.archive_article(id) + + oc.archive_article(aid) with open('cover.jpg', 'rb') as file: book.set_cover(file.read())