krz/crumb

A local alternative to your browser's history. browser-history cli local-first privacy

Commit d4166e0398

d4166e03980177c3395fddac85977645ee2a4294

parent: 2c8b05646b

Verified · cmc

cmc <hello@cleberg.net> · 2025-06-04 18:32 UTC

feat: format python files

Layout: unified · split

search.py +16 −3
@@ -1,15 +1,24 @@
1import sys
21import os
2import sys
33
44LOG_PATH = os.path.expanduser("~/.crumb/history.org")
55
66def 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 """
716 if not os.path.exists(LOG_PATH):
817 print("No history file found.")
918 return
1019
1120 with open(LOG_PATH, "r") as f:
12 entries = f.read().split("* ")[1:] # split on org-mode headings
21 entries = f.read().split("* ")[1:]
1322
1423 found = 0
1524 for entry in entries:
@@ -21,7 +30,11 @@ def search_log(query):
2130 print("No matches found.")
2231
2332if __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 """
2437 if len(sys.argv) < 2:
2538 print("Usage: search_crumb.py <search term>")
2639 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 @@
1from flask import Flask, request, jsonify, make_response
21import os
32from datetime import datetime
3from flask import Flask, request
44
55app = Flask(__name__)
66LOG_PATH = os.path.expanduser("~/.crumb/history.org")
77
88os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
99
10
1011@app.after_request
1112def 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"
1424 return response
1525
16@app.route('/', methods=['POST', 'OPTIONS'])
26
27@app.route("/", methods=["POST", "OPTIONS"])
1728def 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
2042
2143 data = request.json
2244 title = data.get("title", "No Title")
@@ -44,7 +66,11 @@ def log_visit():
4466 f.write(f":FAVICON: {favicon}\n")
4567 f.write(":END:\n\n")
4668
47 return '', 204
69 return "", 204
70
4871
49if __name__ == '__main__':
50 app.run(port=3555)
\ No newline at end of file
72if __name__ == "__main__":
73 """
74 Run the Flask application on port 3555 when executed as the main program.
75 """
76 app.run(port=3555)