Skip to content

Commit

Permalink
feat: Define Zotero2Markdown class for easier implementation. Updat…
Browse files Browse the repository at this point in the history
…e failed item handling. Use Zotero2Markdown in `generate.py`.
  • Loading branch information
e-alizadeh committed Jan 1, 2022
1 parent 3cb2d57 commit 1a461a5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 72 deletions.
75 changes: 10 additions & 65 deletions zotero2md/generate.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,6 @@
from argparse import ArgumentParser
from typing import Union

from pyzotero.zotero import Zotero

from zotero2md.utils import group_by_parent_item
from zotero2md.zotero import (
ItemAnnotationsAndNotes,
get_zotero_client,
retrieve_all_annotations,
retrieve_all_notes,
)


def generate_annotations_for_all_items(
zotero_client: Zotero, params_filepath: Union[str, None] = None
) -> None:
highlights = group_by_parent_item(retrieve_all_annotations(zotero_client))
notes = group_by_parent_item(retrieve_all_notes(zotero_client))
failed_files = []
for i, item_key in enumerate(highlights.keys()):
item = zotero_client.item(item_key)
parent_item_key = item["data"].get("parentItem", None)
print(f"File {i + 1} of {len(highlights)} is under process ...")
item = ItemAnnotationsAndNotes(
zotero_client=zotero_client,
params_filepath=params_filepath,
item_annotations=highlights[item_key],
item_notes=notes.get(parent_item_key, None),
item_key=item_key,
)
# del notes[parent_item_key]

out = item.generate_output()
if out:
failed_files.append(out)
print("\n")
#
# for i, item_key in enumerate(notes.keys()):
# print(f"File {i + 1} of {len(highlights)} is under process ...")
# item = ItemAnnotationsAndNotes(
# zotero_client=zotero_client,
# params_filepath=params_filepath,
# item_annotations=None,
# item_notes=notes[item_key],
# item_key=item_key,
# )
# out = item.generate_output()
# if out:
# failed_files.append(out)
# print("\n")

if failed_files:
print("\nItems that failed to compile to a markdown file:")
for (file, item_key) in failed_files:
print(f"Item Key: {item_key} | File: {file}\n")
else:
print("\nAll items were successfully created!")

from zotero2md.zt2md import Zotero2Markdown

if __name__ == "__main__":
parser = ArgumentParser(description="Generate Markdown files")
Expand All @@ -80,13 +24,14 @@ def generate_annotations_for_all_items(

args = vars(parser.parse_args())

# ----- Create a Zotero client object
zot_client = get_zotero_client(
library_id=args["zotero_user_id"],
library_type=args["library_type"],
api_key=args["zotero_key"],
zt = Zotero2Markdown(
zotero_key=args["zotero_key"],
zotero_library_id=args["zotero_user_id"],
zotero_library_type=args["library_type"],
params_filepath=args.get("config_filepath", None),
include_annotations=True,
include_notes=True,
)

generate_annotations_for_all_items(
zot_client, params_filepath=args.get("config_filepath", None)
)
zt.run_all()
zt.save_failed_items_to_txt()
12 changes: 5 additions & 7 deletions zotero2md/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def __init__(
super().__init__(zotero_client, item_key, params_filepath)
self.item_annotations = item_annotations
self.item_notes = item_notes
self.failed_item: str = ""

self.doc = Document(
name="initial_filename"
Expand Down Expand Up @@ -277,7 +278,7 @@ def create_annotations_section(self, annotations: List) -> None:

self.doc.add_element(MDList(annots))

def generate_output(self) -> Union[None, Tuple[str, str]]:
def generate_output(self) -> None:
"""Generate the markdown file for a Zotero Item combining metadata, annotations
Returns
Expand All @@ -303,13 +304,10 @@ def generate_output(self) -> Union[None, Tuple[str, str]]:
print(
f'File "{output_filename}" (item_key="{self.item_key}") was successfully created.'
)
return None
except:
print(
f'File "{output_filename}" (item_key="{self.item_key}") is failed to generate.\n'
f"SKIPPING..."
)
return output_filename, self.item_key
msg = f'File "{output_filename}" (item_key="{self.item_key}") is failed to generate.\n'
print(msg + "SKIPPING...")
self.failed_item = msg


def retrieve_all_annotations(zotero_client: Zotero) -> List[Dict]:
Expand Down
81 changes: 81 additions & 0 deletions zotero2md/zt2md.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import List, Union

from zotero2md.utils import group_by_parent_item
from zotero2md.zotero import (
ItemAnnotationsAndNotes,
get_zotero_client,
retrieve_all_annotations,
retrieve_all_notes,
)


class Zotero2Markdown:
def __init__(
self,
zotero_key: str,
zotero_library_id: str,
zotero_library_type: str = "user",
params_filepath: str = None,
include_annotations: bool = True,
include_notes: bool = True,
):

self.zotero_client = get_zotero_client(
library_id=zotero_library_id,
library_type=zotero_library_type,
api_key=zotero_key,
)
self.config_filepath = params_filepath
self.include_annots = include_annotations
self.include_notes = include_notes
self.failed_items: List[str] = []

def run_all(self, params_filepath: Union[str, None] = None) -> None:
highlights = group_by_parent_item(retrieve_all_annotations(self.zotero_client))
notes = group_by_parent_item(retrieve_all_notes(self.zotero_client))
for i, item_key in enumerate(highlights.keys()):
item = self.zotero_client.item(item_key)
parent_item_key = item["data"].get("parentItem", None)
print(f"File {i + 1} of {len(highlights)} is under process ...")
zotero_item = ItemAnnotationsAndNotes(
zotero_client=self.zotero_client,
params_filepath=params_filepath,
item_annotations=highlights[item_key],
item_notes=notes.get(parent_item_key, None),
item_key=item_key,
)
# del notes[parent_item_key]

if zotero_item.failed_item:
zotero_item.generate_output()
else:
self.failed_items.append(zotero_item.failed_item)

# for i, item_key in enumerate(notes.keys()):
# print(f"File {i + 1} of {len(highlights)} is under process ...")
# item = ItemAnnotationsAndNotes(
# zotero_client=zotero_client,
# params_filepath=params_filepath,
# item_annotations=None,
# item_notes=notes[item_key],
# item_key=item_key,
# )
# out = item.generate_output()
# if out:
# failed_files.append(out)
# print("\n")

def save_failed_items_to_txt(self, txt_filepath: str = ""):
if txt_filepath == "":
txt_filepath = "failed_zotero_items.txt"

if self.failed_items:
print(
f"\n {len(self.failed_items)} markdown files (with all their annotations and notes failed to create."
)
with open(txt_filepath, "w") as f:
file_content = "\n".join(self.failed_items)
f.write(file_content)
print(f"List of all failed items are saved into {txt_filepath}")
else:
print("No failed item")

0 comments on commit 1a461a5

Please sign in to comment.