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 data objects #17

Merged
merged 8 commits into from
Dec 29, 2022
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ api = Api(token=YOUR_TOKEN)
for tag in api.get_all_tags():

# Delete all tags that match the regex. I. e. start with "!".
if re.search("^!", tag["title"]) is not None:
api.delete_tag(tag["id"])
if re.search("^!", tag.title) is not None:
api.delete_tag(tag.id)
```

</details>
Expand All @@ -159,7 +159,7 @@ def to_camel_case(name: str) -> str:

# Iterate through all tags and apply the conversion.
for tag in api.get_all_tags():
api.modify_tag(id_=tag["id"], title=to_camel_case(tag["title"]))
api.modify_tag(id_=tag.id, title=to_camel_case(tag.title))
```

</details>
Expand All @@ -185,15 +185,15 @@ api = Api(token=YOUR_TOKEN)
# Iterate through all notes and find the referenced resources.
referenced_resources = set()
for note in api.get_all_notes(fields="id,body"):
matches = re.findall(r"\[.*\]\(:.*\/([A-Za-z0-9]{32})\)", note["body"])
matches = re.findall(r"\[.*\]\(:.*\/([A-Za-z0-9]{32})\)", note.body)
referenced_resources.update(matches)

assert len(referenced_resources) > 0, "sanity check"

for resource in api.get_all_resources():
if resource["id"] not in referenced_resources:
if resource.id not in referenced_resources:
print("Deleting resource:", resource)
api.delete_resource(resource["id"])
api.delete_resource(resource.id)
```

</details>
Expand Down
8 changes: 4 additions & 4 deletions examples/note_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ def main():
# Find notes with matching titles.
candidates = []
for title in args.note_titles:
candidates.extend([note for note in notes if note["title"] == title])
candidates.extend([note for note in notes if note.title == title])
print(f"Found {len(candidates)} matching notes.")

# Convert all notes to the specified format.
os.makedirs(args.output_folder, exist_ok=True)
for candidate in candidates:
note = api.get_note(id_=candidate["id"], fields="body")
note = api.get_note(id_=candidate.id, fields="body")

title_normalized = (
candidate["title"].lower().replace(" ", "_") + "_" + candidate["id"]
candidate.title.lower().replace(" ", "_") + "_" + candidate.id
)
output_path = f"{args.output_folder}/{title_normalized}.{args.output_format}"

Expand All @@ -78,7 +78,7 @@ def main():
}

pypandoc.convert_text(
f"# {candidate['title']}\n{note['body']}",
f"# {candidate.title}\n{note.body}",
format="md",
outputfile=output_path,
**format_kwargs.get(args.output_format, {"to": args.output_format}),
Expand Down
3 changes: 2 additions & 1 deletion examples/note_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def main():

# download nltk data at the first start
if False:
# "punkt" and "tokenizers" are needed.
nltk.download()

# get all notes from joplin
Expand All @@ -81,7 +82,7 @@ def main():
print("Notes:", len(notes))

# concatenate and convert them to text
text = markdown_to_text("\n".join(note["body"] for note in notes))
text = markdown_to_text("\n".join(note.body for note in notes))

# analyze them
analyze_text(text)
Expand Down
4 changes: 2 additions & 2 deletions examples/pdf_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ def get_item_tree(api):
# - Note IDs get the prefix "n"
notebooks_flat_api = api.get_all_notebooks(fields="id,parent_id,title")
notebooks_flat = {
"nb" + nb["id"]: Notebook(nb["id"], nb["parent_id"], nb["title"])
"nb" + nb.id: Notebook(nb.id, nb.parent_id, nb.title)
for nb in notebooks_flat_api
}
notes_flat_api = api.get_all_notes(fields="id,parent_id,title,body")
notes_flat = {
"n" + n["id"]: Note(n["id"], n["parent_id"], n["title"], n["body"])
"n" + n.id: Note(n.id, n.parent_id, n.title, n.body)
for n in notes_flat_api
}

Expand Down
8 changes: 4 additions & 4 deletions examples/reddit_clipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ def create_joplin_note(note_title, note_body, destination_notebook):
joplin_api = Api(token=os.getenv("API_TOKEN"))

# Search the parent notebook in Joplin.
notebook_candidates = joplin_api.search(query=destination_notebook, type="folder")[
"items"
]
notebook_candidates = joplin_api.search(
query=destination_notebook, type="folder"
).items
if len(notebook_candidates) < 1:
exit(1)
else:
notebook_id = notebook_candidates[0]["id"]
notebook_id = notebook_candidates[0].id

# Create note in joplin
joplin_api.add_note(
Expand Down
2 changes: 1 addition & 1 deletion examples/visualize_note_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
print("Notes:", len(notes))

# filter notes with location (0, 0)
notes = [note for note in notes if note["latitude"] != 0 and note["longitude"] != 0]
notes = [note for note in notes if note.latitude != 0 and note.longitude != 0]

df = pd.DataFrame(notes)
fig = px.scatter_geo(df, lat="latitude", lon="longitude", hover_name="title")
Expand Down
Loading