Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add location to readwise #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from calibre_plugins.readwise.config import prefs
from PyQt5.Qt import QDialog, QVBoxLayout, QLabel, QPushButton, QMessageBox
import urllib.request
from urllib.parse import quote
import json

class ReadwiseDialog(QDialog):
Expand Down Expand Up @@ -41,7 +42,8 @@ def about(self):

def sync(self):
db = self.db.new_api

library_id = getattr(db, 'server_library_id', None)
library_id = '_hex_-' + library_id.encode('utf-8').hex()
books = {}
for annotation in db.all_annotations(None, None, 'highlight', True, None):
books.setdefault(annotation['book_id'], []).append(annotation)
Expand All @@ -56,14 +58,22 @@ def sync(self):
for book_id, annotations in books.items():
metadata = db.get_metadata(book_id)
for annotation in annotations:
link_prefix = f'calibre://view-book/{library_id}/{book_id}/{annotation["format"]}?open_at='
spine_index = (1 + annotation['annotation']['spine_index']) * 2
cfi = annotation['annotation']['start_cfi']
link = (link_prefix + quote(f'epubcfi(/{spine_index}{cfi})')).replace(')', '%29')
highlight = {
'text': annotation['annotation']['highlighted_text'],
'title': metadata.title,
'author': metadata.authors[0],
'source_type': 'book',
'note': annotation['annotation'].get('notes', None),
'highlighted_at': annotation['annotation']['timestamp']
'highlighted_at': annotation['annotation']['timestamp'],
'location_type': 'location',
'location': annotation['id'],
'highlight_url': link
}

body['highlights'].append(highlight)

headers = {
Expand Down