from flask import Flask, request, jsonify import requests import json app = Flask(__name__) # Laden der API-Keys aus einer Konfigurationsdatei def load_api_keys(): try: with open("config.json", "r") as file: return json.load(file) except FileNotFoundError: return {"synonym_api_keys": {"primary": "", "secondary": ""}, "grammar_api_key": ""} # Speichern der API-Keys def save_api_keys(api_keys): with open("config.json", "w") as file: json.dump(api_keys, file) # API-Dienst für Synonyme (mit Fallback) class SynonymAPI: def __init__(self, api_keys): self.api_keys = api_keys def fetch(self, word, language='en'): try: response = self._fetch_from_primary_api(word, language) if response.status_code == 200: return response.json()['entries'][0]['synonyms'] except Exception: return self._fetch_from_secondary_api(word, language) def _fetch_from_primary_api(self, word, language): url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={word}&language={language}" headers = { 'x-rapidapi-key': self.api_keys['primary'], 'x-rapidapi-host': "lingua-robot.p.rapidapi.com" } return requests.get(url, headers=headers) def _fetch_from_secondary_api(self, word, language): url = f"https://api.datamuse.com/words?rel_syn={word}&ml={language}" return requests.get(url) # API-Dienst für Grammatik class GrammarAPI: def __init__(self, api_key): self.api_key = api_key def fetch(self, text, language='en'): response = requests.post(self.api_key, data={'text': text, 'language': language}) return response.json()['matches'] if response.status_code == 200 else [] # API Manager zum Verwalten der Dienste class APIManager: def __init__(self): self.synonym_api = None self.grammar_api = None def configure_apis(self, synonym_api_keys, grammar_api_key): self.synonym_api = SynonymAPI(synonym_api_keys) self.grammar_api = GrammarAPI(grammar_api_key) def fetch_synonyms(self, word, language='en'): return self.synonym_api.fetch(word, language) if self.synonym_api else "Keine Synonym-API konfiguriert." def fetch_grammar(self, text, language='en'): return self.grammar_api.fetch(text, language) if self.grammar_api else "Keine Grammatik-API konfiguriert." # Instanziierung des API-Managers api_manager = APIManager() # API-Konfiguration @app.route('/configure', methods=['POST']) def configure_apis(): data = request.json api_keys = { 'synonym_api_keys': { 'primary': data.get('synonym_primary_api_key'), 'secondary': data.get('synonym_secondary_api_key') }, 'grammar_api_key': data.get('grammar_api_key') } save_api_keys(api_keys) api_manager.configure_apis(api_keys['synonym_api_keys'], api_keys['grammar_api_key']) return jsonify({"status": "APIs erfolgreich konfiguriert."}) # Textanalyse-API mit Zeichenanpassung @app.route('/analyze-text', methods=['POST']) def analyze_text(): data = request.json text = data.get('text') desired_length = data.get('desired_length', len(text)) language = data.get('language', 'en') word = text.split()[0] synonyms = api_manager.fetch_synonyms(word, language) grammar = api_manager.fetch_grammar(text, language) current_length = len(text) length_difference = desired_length - current_length response = { "synonyms": synonyms, "grammar_suggestions": grammar, "current_length": current_length, "desired_length": desired_length, "length_difference": length_difference, "adjustment_suggestions": [] } if length_difference > 0: response["adjustment_suggestions"].append(f"Fügen Sie {length_difference} Zeichen hinzu.") elif length_difference < 0: response["adjustment_suggestions"].append(f"Entfernen Sie {abs(length_difference)} Zeichen.") return jsonify(response) # Serverstart if __name__ == '__main__': app.run(debug=True)