Skip to content

Commit

Permalink
handle empty api key and new json
Browse files Browse the repository at this point in the history
  • Loading branch information
maksii committed Jun 17, 2024
1 parent fde6b6d commit 9f74189
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/routes/stream.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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():
Expand Down
8 changes: 7 additions & 1 deletion app/routes/toloka.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
31 changes: 26 additions & 5 deletions app/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(' | ');
Expand All @@ -69,18 +73,34 @@ 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(
fetch(`/api/tmdb/detail/${item.id}?type=${item.media_type}`)
.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;

Expand All @@ -101,6 +121,7 @@ function processMultiSearchData(responses) {
})
);
});
}

// Process custom API data
responses[2].slice(0, slice).forEach(item => {
Expand Down
2 changes: 1 addition & 1 deletion app/static/js/toloka.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Flask-WTF
SQLAlchemy
requests
configparser
setuptools
setuptools
jsonpickle

0 comments on commit 9f74189

Please sign in to comment.