krz/crumb

A local alternative to your browser's history.

clone: git clone https://gitbay.org/krz/crumb.git

d4166e03980177c3395fddac85977645ee2a4294

verified · cmc

author: Christian Cleberg <hello@cleberg.net> · 2025-06-04T18:32:08Z

feat: format python files
 search.py | 19 ++++++++++++++++---
 server.py | 44 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/search.py b/search.py
index 098e392..ad81184 100644
--- a/search.py
+++ b/search.py
@@ -1,15 +1,24 @@
-import sys
 import os
+import sys
 
 LOG_PATH = os.path.expanduser("~/.crumb/history.org")
 
 def search_log(query):
+    """
+    Search for a given query string within the crumb history log file and print matching entries.
+
+    Args:
+        query (str): The search term to look for in the history entries.
+
+    Returns:
+        None
+    """
     if not os.path.exists(LOG_PATH):
         print("No history file found.")
         return
 
     with open(LOG_PATH, "r") as f:
-        entries = f.read().split("* ")[1:]  # split on org-mode headings
+        entries = f.read().split("* ")[1:]
 
     found = 0
     for entry in entries:
@@ -21,7 +30,11 @@ def search_log(query):
         print("No matches found.")
 
 if __name__ == "__main__":
+    """
+    Entry point for the script. Parses command line arguments and calls the search function.
+    Usage: search_crumb.py <search term>
+    """
     if len(sys.argv) < 2:
         print("Usage: search_crumb.py <search term>")
     else:
-        search_log(sys.argv[1])
\ No newline at end of file
+        search_log(sys.argv[1])
diff --git a/server.py b/server.py
index 31c0d4c..310fdae 100644
--- a/server.py
+++ b/server.py
@@ -1,22 +1,44 @@
-from flask import Flask, request, jsonify, make_response
 import os
 from datetime import datetime
+from flask import Flask, request
 
 app = Flask(__name__)
 LOG_PATH = os.path.expanduser("~/.crumb/history.org")
 
 os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
 
+
 @app.after_request
 def add_cors_headers(response):
-    response.headers['Access-Control-Allow-Origin'] = '*'
-    response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
+    """
+    Add CORS headers to the response to allow cross-origin requests.
+
+    Args:
+        response (flask.Response): The response object to modify.
+
+    Returns:
+        flask.Response: The modified response object with CORS headers.
+    """
+    response.headers["Access-Control-Allow-Origin"] = "*"
+    response.headers["Access-Control-Allow-Headers"] = "Content-Type"
     return response
 
-@app.route('/', methods=['POST', 'OPTIONS'])
+
+@app.route("/", methods=["POST", "OPTIONS"])
 def log_visit():
-    if request.method == 'OPTIONS':
-        return '', 204
+    """
+    Handle POST requests to log visit information and OPTIONS requests for CORS preflight.
+
+    For POST requests, parse JSON data from the request, extract visit details,
+    and append them to the log file in org-mode format.
+
+    For OPTIONS requests, return a 204 No Content response.
+
+    Returns:
+        tuple: An empty string and the HTTP status code 204.
+    """
+    if request.method == "OPTIONS":
+        return "", 204
 
     data = request.json
     title = data.get("title", "No Title")
@@ -44,7 +66,11 @@ def log_visit():
             f.write(f":FAVICON:   {favicon}\n")
         f.write(":END:\n\n")
 
-    return '', 204
+    return "", 204
+
 
-if __name__ == '__main__':
-    app.run(port=3555)
\ No newline at end of file
+if __name__ == "__main__":
+    """
+    Run the Flask application on port 3555 when executed as the main program.
+    """
+    app.run(port=3555)