-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
48 lines (40 loc) · 1.75 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, jsonify, request, render_template
from rename_files import InteractiveFileRenamer
from pathlib import Path
import webview
renamer = None
def create_app():
global renamer
app = Flask(__name__, template_folder="templates", static_folder="static")
renamer = InteractiveFileRenamer()
@app.route('/')
def index():
return render_template("index.html") # Render the main GUI HTML file
@app.route('/set_directory', methods=['POST'])
def set_directory():
data = request.json
directory = Path(data['directory'])
if directory.is_dir():
renamer.base_dir = directory
return jsonify({"message": "Directory set successfully."})
return jsonify({"error": "Invalid directory."}), 400
@app.route('/discover_patterns', methods=['GET'])
def discover_patterns():
if renamer.base_dir: # Check if base_dir is set
patterns = renamer._discover_file_patterns()
return jsonify(patterns)
return jsonify({'error': 'Directory not set.'}), 400 # Return an error if not
@app.route('/rename_files', methods=['POST'])
async def rename_files_route(): # Make rename_files async
global renamer
if not renamer.base_dir:
return jsonify({'error': 'Directory not set.'}), 400
data = request.json
renamer.filename_prefix = data.get('prefix', 'new')
renamer.filename_extension = data.get('extension', '.txt')
try:
renamed, failed, failed_files = await renamer.rename_files()
return jsonify({'renamed_count': renamed, 'failed_count': failed, 'failed_files': failed_files})
except Exception as e:
return jsonify({'error': str(e)}), 500
return app