fix: remove debug point + cleaning script by youenchene · Pull Request #4 · thiswillbeyourgithub/karakeep_python_api · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions README.md
22 changes: 22 additions & 0 deletions community_scripts/karakeep-archive-before-date/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Karakeep Archive before date

Small cleaning script to clean old article not archived after an import from another readlater app.

## Prerequisites

N/A

## Usage

Define a date to limit archiving. All not archived bookmarks before this date will be archived.

```bash
python archiving_before_date.py --before-date 2023-12-24
```

`--before-date` format is `YYYY-MM-DD`

You might need to set up environment variables for the Karakeep API client or pass them as arguments if the script supports it (e.g., `KARAKEEP_PYTHON_API_BASE_URL` and `KARAKEEP_PYTHON_API_KEY`). Refer to the script's help or the `karakeep-python-api` documentation for more details on authentication.



Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
Small script to clean old article not archived after an import from another readlater app.

Parameters:
before_date: Date in YYYY-MM-DD format. Articles created before this date will be archived.
"""
import time
from datetime import datetime

from Levenshtein import ratio
import pickle
from fire import Fire
from typing import Optional
from pathlib import Path
import json
import csv
from karakeep_python_api import KarakeepAPI
from tqdm import tqdm

VERSION: str = "1.0.0"

karakeep = KarakeepAPI(verbose=False)


def main(before_date: str) -> None:
"""Archive articles created before the specified date.

Args:
before_date: Date string in YYYY-MM-DD format
"""
before_date = datetime.strptime(before_date, "%Y-%m-%d")

n = karakeep.get_current_user_stats()["numBookmarks"]
pbar = tqdm(total=n, desc="Fetching bookmarks")
all_bm = []
batch_size = 100 # if you set it too high, you can crash the karakeep instance, 100 being the maximum allowed
page = karakeep.get_all_bookmarks(
include_content=False,
limit=batch_size,
)
all_bm.extend(page.bookmarks)
pbar.update(len(all_bm))
while page.nextCursor:
page = karakeep.get_all_bookmarks(
include_content=False,
limit=batch_size,
cursor=page.nextCursor,
)
all_bm.extend(page.bookmarks)
pbar.update(len(page.bookmarks))

assert (
len(all_bm) == n
), f"Only retrieved {len(all_bm)} bookmarks instead of {n}"
pbar.close()

failed = []
for bookmark in all_bm:

# skip already archived
if bookmark.archived:
continue


#tqdm.write(f"Creation Date: {bookmark.createdAt}")
creation_date = datetime.strptime(bookmark.createdAt, "%Y-%m-%dT%H:%M:%S.%fZ")

if creation_date > before_date:
continue

# do the archiving
retries = 3
for attempt in range(retries):
try:
res_arch = karakeep.update_a_bookmark(
bookmark_id=bookmark.id,
update_data={"archived": True},
)
break
except Exception as e:
if attempt == retries - 1:
raise e
tqdm.write(f"Update failed, retrying ({attempt + 1}/{retries})")
time.sleep(1)
if isinstance(res_arch, dict):
assert res_arch["archived"], res_arch
else:
assert res_arch.archived, res_arch
tqdm.write(f"Successfuly archived: {bookmark.title}")


if __name__ == "__main__":
Fire(main)