From 9f74189eaa3cf4df785f5ef509f2f17b08b44b61 Mon Sep 17 00:00:00 2001 From: maksii <1761348+maksii@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:50:44 +0300 Subject: [PATCH] handle empty api key and new json --- app/routes/stream.py | 3 ++- app/routes/toloka.py | 8 +++++++- app/static/js/search.js | 31 ++++++++++++++++++++++++++----- app/static/js/toloka.js | 2 +- requirements.txt | 3 ++- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/routes/stream.py b/app/routes/stream.py index f5c7437..3460fe7 100644 --- a/app/routes/stream.py +++ b/app/routes/stream.py @@ -1,4 +1,5 @@ from flask import Blueprint, jsonify, request +import jsonpickle from app.services.services import add_title_from_streaming_site, search_titles_from_streaming_site @@ -8,7 +9,7 @@ @stream_bp.route('/api/stream', methods=['GET']) def search_titles_from_streaming(): query = request.args.get('query') - return jsonify(search_titles_from_streaming_site(query)) + return jsonpickle.encode(search_titles_from_streaming_site(query)) @stream_bp.route('/api/stream', methods=['POST']) def add_title_from_streaming(): diff --git a/app/routes/toloka.py b/app/routes/toloka.py index cab943e..b903cde 100644 --- a/app/routes/toloka.py +++ b/app/routes/toloka.py @@ -11,7 +11,13 @@ def get_torrents(): try: query = request.args.get('query') - return jsonify(get_torrents_logic(query)) + response = get_torrents_logic(query) + + # Check if the response is a "No results found" message + if isinstance(response, str) and response.startswith("No results found"): + return jsonify({"error": response}) + else: + return jsonify(response) except Exception as e: # Return a custom JSON error message with a 500 Internal Server Error status error_message = { diff --git a/app/static/js/search.js b/app/static/js/search.js index 7bfca83..985a835 100644 --- a/app/static/js/search.js +++ b/app/static/js/search.js @@ -54,6 +54,10 @@ function processMultiSearchData(responses) { let slice = 4; let tmdbPromises = []; // Process MAL data + // Check if the first response contains an error + if (responses[0].error || responses[0].status_code) { + console.error("Error from MAL API:", responses[0].message || responses[0].status_message); + } else { responses[0].data.slice(0, slice).forEach(item => { let alternatives = item.node.alternative_titles.en + ' | ' + item.node.alternative_titles.ja; alternatives += ' | ' + item.node.alternative_titles.synonyms.join(' | '); @@ -69,7 +73,11 @@ function processMultiSearchData(responses) { alternative: alternatives }); }); - + } + + if (responses[1].error || responses[1].status_code) { + console.error("Error from TMDB API:", responses[1].message || responses[1].status_message); + } else { // Process TMDB data responses[1].results.slice(0, 4).forEach(item => { tmdbPromises.push( @@ -77,10 +85,22 @@ function processMultiSearchData(responses) { .then(response => response.json()) .then(details => { const relevantCountries = ['JP', 'US', 'UA', 'UK']; - const alternativeTitles = details.alternative_titles.results - .filter(title => relevantCountries.includes(title.iso_3166_1)) - .map(title => title.title) - .join(' | '); + // First, determine the source array to use: either results or titles + const sourceArray = details && details.alternative_titles + ? (Array.isArray(details.alternative_titles.results) ? details.alternative_titles.results + : Array.isArray(details.alternative_titles.titles) ? details.alternative_titles.titles + : null) + : null; + + // Now process the source array if it's not null + const alternativeTitles = sourceArray + ? sourceArray + .filter(title => relevantCountries.includes(title.iso_3166_1)) + .map(title => title.title) + .join(' | ') + : ''; // Default to an empty string if no valid array is found + + console.log(alternativeTitles); const alternative = item.original_name ? `${item.original_name} | ${alternativeTitles}` : alternativeTitles; @@ -101,6 +121,7 @@ function processMultiSearchData(responses) { }) ); }); +} // Process custom API data responses[2].slice(0, slice).forEach(item => { diff --git a/app/static/js/toloka.js b/app/static/js/toloka.js index 2087b65..aac0d8e 100644 --- a/app/static/js/toloka.js +++ b/app/static/js/toloka.js @@ -1,7 +1,7 @@ $(document).ready(function () { var initialized = false; var table; - + $.fn.dataTable.ext.errMode = 'none'; // Handle form submission event $('.d-flex[role="search"]').on('submit', function (e) { e.preventDefault(); diff --git a/requirements.txt b/requirements.txt index dc65581..0159034 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ Flask-WTF SQLAlchemy requests configparser -setuptools \ No newline at end of file +setuptools +jsonpickle \ No newline at end of file