Commit d4166e0398
Verified · cmc
Layout: unified · split
search.py +16 −3
| @@ -1,15 +1,24 @@ | ||
| 1 | import sys | |
| 2 | 1 | import os |
| 2 | import sys | |
| 3 | 3 | |
| 4 | 4 | LOG_PATH = os.path.expanduser("~/.crumb/history.org") |
| 5 | 5 | |
| 6 | 6 | def search_log(query): |
| 7 | """ | |
| 8 | Search for a given query string within the crumb history log file and print matching entries. | |
| 9 | ||
| 10 | Args: | |
| 11 | query (str): The search term to look for in the history entries. | |
| 12 | ||
| 13 | Returns: | |
| 14 | None | |
| 15 | """ | |
| 7 | 16 | if not os.path.exists(LOG_PATH): |
| 8 | 17 | print("No history file found.") |
| 9 | 18 | return |
| 10 | 19 | |
| 11 | 20 | with open(LOG_PATH, "r") as f: |
| 12 | entries = f.read().split("* ")[1:] # split on org-mode headings | |
| 21 | entries = f.read().split("* ")[1:] | |
| 13 | 22 | |
| 14 | 23 | found = 0 |
| 15 | 24 | for entry in entries: |
| @@ -21,7 +30,11 @@ def search_log(query): | ||
| 21 | 30 | print("No matches found.") |
| 22 | 31 | |
| 23 | 32 | if __name__ == "__main__": |
| 33 | """ | |
| 34 | Entry point for the script. Parses command line arguments and calls the search function. | |
| 35 | Usage: search_crumb.py <search term> | |
| 36 | """ | |
| 24 | 37 | if len(sys.argv) < 2: |
| 25 | 38 | print("Usage: search_crumb.py <search term>") |
| 26 | 39 | else: |
| 27 | search_log(sys.argv[1]) | |
| \ No newline at end of file | ||
| 40 | search_log(sys.argv[1]) | |
server.py +35 −9
| @@ -1,22 +1,44 @@ | ||
| 1 | from flask import Flask, request, jsonify, make_response | |
| 2 | 1 | import os |
| 3 | 2 | from datetime import datetime |
| 3 | from flask import Flask, request | |
| 4 | 4 | |
| 5 | 5 | app = Flask(__name__) |
| 6 | 6 | LOG_PATH = os.path.expanduser("~/.crumb/history.org") |
| 7 | 7 | |
| 8 | 8 | os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True) |
| 9 | 9 | |
| 10 | ||
| 10 | 11 | @app.after_request |
| 11 | 12 | def add_cors_headers(response): |
| 12 | response.headers['Access-Control-Allow-Origin'] = '*' | |
| 13 | response.headers['Access-Control-Allow-Headers'] = 'Content-Type' | |
| 13 | """ | |
| 14 | Add CORS headers to the response to allow cross-origin requests. | |
| 15 | ||
| 16 | Args: | |
| 17 | response (flask.Response): The response object to modify. | |
| 18 | ||
| 19 | Returns: | |
| 20 | flask.Response: The modified response object with CORS headers. | |
| 21 | """ | |
| 22 | response.headers["Access-Control-Allow-Origin"] = "*" | |
| 23 | response.headers["Access-Control-Allow-Headers"] = "Content-Type" | |
| 14 | 24 | return response |
| 15 | 25 | |
| 16 | @app.route('/', methods=['POST', 'OPTIONS']) | |
| 26 | ||
| 27 | @app.route("/", methods=["POST", "OPTIONS"]) | |
| 17 | 28 | def log_visit(): |
| 18 | if request.method == 'OPTIONS': | |
| 19 | return '', 204 | |
| 29 | """ | |
| 30 | Handle POST requests to log visit information and OPTIONS requests for CORS preflight. | |
| 31 | ||
| 32 | For POST requests, parse JSON data from the request, extract visit details, | |
| 33 | and append them to the log file in org-mode format. | |
| 34 | ||
| 35 | For OPTIONS requests, return a 204 No Content response. | |
| 36 | ||
| 37 | Returns: | |
| 38 | tuple: An empty string and the HTTP status code 204. | |
| 39 | """ | |
| 40 | if request.method == "OPTIONS": | |
| 41 | return "", 204 | |
| 20 | 42 | |
| 21 | 43 | data = request.json |
| 22 | 44 | title = data.get("title", "No Title") |
| @@ -44,7 +66,11 @@ def log_visit(): | ||
| 44 | 66 | f.write(f":FAVICON: {favicon}\n") |
| 45 | 67 | f.write(":END:\n\n") |
| 46 | 68 | |
| 47 | return '', 204 | |
| 69 | return "", 204 | |
| 70 | ||
| 48 | 71 | |
| 49 | if __name__ == '__main__': | |
| 50 | app.run(port=3555) | |
| \ No newline at end of file | ||
| 72 | if __name__ == "__main__": | |
| 73 | """ | |
| 74 | Run the Flask application on port 3555 when executed as the main program. | |
| 75 | """ | |
| 76 | app.run(port=3555) | |