From 567ae15ae51a6945af7230058af30ebd2c4e0c78 Mon Sep 17 00:00:00 2001 From: Exouth Date: Tue, 26 Sep 2023 19:13:32 +0200 Subject: [PATCH] Functions for Obsidian App --- src/examgenerator.py | 12 +++++++----- src/lib/exam_prompt.py | 32 +++++++++++++++++++------------- src/lib/obsidian_api.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 src/lib/obsidian_api.py diff --git a/src/examgenerator.py b/src/examgenerator.py index 8d33dc6..4f689c1 100755 --- a/src/examgenerator.py +++ b/src/examgenerator.py @@ -7,12 +7,14 @@ from odf.opendocument import OpenDocumentText from odf.text import P -engine = ExamPrompt("180bdc1f34ea46a48abaf34f2c121cb05391263a49a1b3c246c6241df172bf8dc06dd27d4c0b8edbc60eed6380be8b27566c6bcd7dfa34578efe5e0113605839") +engine = ExamPrompt() try: name = engine.ask_exam_name() + engine.ask_note_path() + engine.ask_topic_number() toppic_notes = engine.give_topic_notes() @@ -25,13 +27,13 @@ doc = OpenDocumentText() for topic in toppic_notes: - page, note_id, title = topic - engine.prompt_exam(note_id, title, doc) + engine.prompt_exam(topic, doc) engine.next_step() doc.save(name); engine.exit() - + except KeyboardInterrupt: - engine.exit() \ No newline at end of file + engine.exit() + diff --git a/src/lib/exam_prompt.py b/src/lib/exam_prompt.py index 5bb4ffa..a72151e 100644 --- a/src/lib/exam_prompt.py +++ b/src/lib/exam_prompt.py @@ -1,5 +1,6 @@ import sys from pyperclip import copy +from lib.obsidian_api import ObsidianAPI from lib.prompting import Prompt from lib.joplin_api import JoplinAPI from odf.text import P, Span, List, ListItem @@ -8,25 +9,28 @@ class ExamPrompt(): - def __init__(self, joplin_api): + def __init__(self): self.name = "" + self.note_path = "" self.topic_number = "" self.prompt_instance = Prompt() - self.api_instance = JoplinAPI(joplin_api) + self.api_instance = ObsidianAPI() def ask_exam_name(self): self.name = self.prompt_instance.promp_user_return("What is the Exam Name?", "You set the Exam name to") return self.name + + def ask_note_path(self): + self.note_path = self.prompt_instance.promp_user_return("What is the Path to the Notes?", "You set the Path to") def ask_topic_number(self): - self.topic_number = self.prompt_instance.promp_user_return("What is the Topic Number?", "You set the Topic Number to") + self.topic_number = self.prompt_instance.promp_user_return("What is the Topic?", "You set the Topic to") def give_topic_notes(self): - matching_notes = self.api_instance.find_all_topic_notes(self.topic_number) + matching_notes = self.api_instance.find_all_topic_notes(self.note_path, self.topic_number) for note in matching_notes: - page, note_id, title = note - self.prompt_instance.write_message(f"Used Note - Page: {page}, ID: {note_id}, Title: {title}") + self.prompt_instance.write_message(f"Used Note: {note}") return matching_notes @@ -35,17 +39,19 @@ def give_initial_Exam_Message(self): copy(message) self.prompt_instance.write_message(message) + def ask_obsidian_path(self): + self.general_path = self.__prompt_instance.promp_user_return("What is the Path to Obsidian?", "You set the Path to Obsidian to") + return self.general_path + def next_step(self): self.prompt_instance.prompt_user("Are you ready to go to the next step? Click a Button") self.prompt_instance.send_logo() - def prompt_exam(self, topic_id, title, document): - markdown_file = self.api_instance.get_note_text(topic_id) - - self.prompt_instance.write_message("Doing now: " + title) + def prompt_exam(self, path, document): + markdown_file = self.api_instance.get_note_text(path) # Multiplie Choice Questions - message = "Here is my Markdown Text. Can you give me 15 Multiplie Choices Questions based on this Note? Also put the Characters '+++' after each Multiplie Choice at the end without the Answer just the Characters! Here is my Markdown File: " + markdown_file["body"] + message = "Here is my Markdown Text. Can you give me 15 Multiplie Choices Questions based on this Note? Also put the Characters '+++' after each Multiplie Choice at the end without the Answer just the Characters! Here is my Markdown File: " + markdown_file copy(message) self.prompt_instance.write_message(message) @@ -58,7 +64,7 @@ def prompt_exam(self, topic_id, title, document): self.next_step() # True or False Questions - message = "Here is my Markdown Text. Can you give me 10 True or False Questions based on this Note? Also put the Characters '+++' after each True or False Question at the end without the Answer just the Characters! Here is my Markdown File: " + markdown_file["body"] + message = "Here is my Markdown Text. Can you give me 10 True or False Questions based on this Note? Also put the Characters '+++' after each True or False Question at the end without the Answer just the Characters! Here is my Markdown File: " + markdown_file copy(message) self.prompt_instance.write_message(message) @@ -71,7 +77,7 @@ def prompt_exam(self, topic_id, title, document): self.next_step() # Full Sentence Answers - message = "Here is my Markdown Text. Can you give me 15 Full Sentence Questions, where i need to answer with an Full Sentence based on this Note? Also put the Characters '+++' after each Full Sentence Question at the end without the Answer just the Characters! Here is my Markdown File: " + markdown_file["body"] + message = "Here is my Markdown Text. Can you give me 15 Full Sentence Questions, where i need to answer with an Full Sentence based on this Note? Also put the Characters '+++' after each Full Sentence Question at the end without the Answer just the Characters! Here is my Markdown File: " + markdown_file copy(message) self.prompt_instance.write_message(message) diff --git a/src/lib/obsidian_api.py b/src/lib/obsidian_api.py new file mode 100644 index 0000000..fcddb56 --- /dev/null +++ b/src/lib/obsidian_api.py @@ -0,0 +1,34 @@ +import os + +class ObsidianAPI: + + def __init__(self): + self.note_path = "" + + def findNote(self, path, name): + for root, dirs, files in os.walk(path): + if name in files: + note_path = os.path.join(root, name) + return note_path + return None + + def find_all_topic_notes(self, note_path, topic): + matching_files = [] + + for dirpath, _, filenames in os.walk(note_path): + for filename in filenames: + if filename.endswith(".md"): + full_filepath = os.path.join(dirpath, filename) + try: + with open(full_filepath, 'r', encoding='utf-8') as file: + content = file.read() + if topic in content: + matching_files.append(full_filepath) + except Exception as e: + print(f"Error reading {full_filepath}: {e}") + + return matching_files + + def get_note_text(self, path): + with open(path, 'r', encoding='utf-8') as file: + return file.read() \ No newline at end of file