Skip to content

Commit

Permalink
linted
Browse files Browse the repository at this point in the history
  • Loading branch information
zsoltika committed Jun 11, 2024
1 parent 31fc1a9 commit 3f425bf
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions o2e.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,78 @@
#!/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']

# 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:

content = ''
title = article['article']['article']['title']
title = title.replace('&','&amp;')
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 += '<h3>' + author + '</h3>'

if title:
if title:
content += '<h2>' + title + '</h2>'

if url:
if url:
content += '<h6>' + url + '</h6>'

if clabel:
Expand All @@ -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())
Expand Down

0 comments on commit 3f425bf

Please sign in to comment.