cloudwp's picture
Update app.py
cbf6837 verified
raw
history blame
3.18 kB
from flask import Flask, request, jsonify
import os
import json
import zipfile
import requests
from urllib.parse import quote as url_quote # Korrektur des Imports
app = Flask(__name__)
class APIService:
""" Basisklasse für alle API-Dienste. """
def __init__(self, api_key):
self.api_key = api_key
def fetch(self, *args, **kwargs):
raise NotImplementedError("Diese Methode muss von Unterklassen implementiert werden.")
class SynonymAPI(APIService):
""" API-Dienst für Synonyme. """
def fetch(self, word):
url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={url_quote(word)}"
headers = {
'x-rapidapi-key': self.api_key,
'x-rapidapi-host': "lingua-robot.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
return response.json()['entries'][0]['synonyms'] if response.status_code == 200 else []
class AliasManager:
""" Verwaltung von Aliasen. """
aliases = []
@classmethod
def add_alias(cls, alias_name, command, description):
cls.aliases.append({
"alias_name": alias_name,
"command": command,
"description": description
})
@classmethod
def list_aliases(cls):
return cls.aliases
class BackupManager:
""" Backup-Funktionalitäten """
@staticmethod
def create_backup():
backup_filename = "project_backup.zip"
with zipfile.ZipFile(backup_filename, 'w') as zipf:
for root, dirs, files in os.walk("."):
for file in files:
if file not in ["project_backup.zip"]:
zipf.write(os.path.join(root, file))
return backup_filename
# API-Routen
@app.route('/aliases', methods=['GET', 'POST'])
def manage_aliases():
if request.method == 'POST':
data = request.json
alias_name = data.get('alias_name')
command = data.get('command')
description = data.get('description')
AliasManager.add_alias(alias_name, command, description)
return jsonify({"status": "Alias erfolgreich erstellt", "alias_name": alias_name})
else:
return jsonify(AliasManager.list_aliases())
@app.route('/create-script', methods=['POST'])
def create_script():
data = request.json
script_name = data.get('script_name')
content = data.get('content')
description = data.get('description')
with open(f"my_scripts/{script_name}.py", "w") as script_file:
script_file.write(content)
return jsonify({"status": "Skript erfolgreich erstellt", "script_name": script_name})
@app.route('/create-backup', methods=['POST'])
def create_backup():
backup_filename = BackupManager.create_backup()
return jsonify({"status": "Backup erfolgreich erstellt", "backup_filename": backup_filename})
@app.route('/tasks', methods=['GET'])
def get_tasks():
if os.path.exists("tasks.json"):
with open("tasks.json", "r") as tasks_file:
tasks = json.load(tasks_file)
else:
tasks = []
return jsonify(tasks)
# Start des Servers
if __name__ == '__main__':
app.run(debug=True)