|
|
|
@ -5,6 +5,7 @@ from datetime import date |
|
|
|
from enum import Enum |
|
|
|
from enum import Enum |
|
|
|
import json |
|
|
|
import json |
|
|
|
import re |
|
|
|
import re |
|
|
|
|
|
|
|
from sqlite3 import DatabaseError |
|
|
|
import ssl |
|
|
|
import ssl |
|
|
|
from urllib.error import URLError, HTTPError |
|
|
|
from urllib.error import URLError, HTTPError |
|
|
|
import urllib.parse |
|
|
|
import urllib.parse |
|
|
|
@ -13,6 +14,7 @@ import urllib.request |
|
|
|
from flask import Blueprint, current_app, flash, request, render_template |
|
|
|
from flask import Blueprint, current_app, flash, request, render_template |
|
|
|
|
|
|
|
|
|
|
|
from .. import util |
|
|
|
from .. import util |
|
|
|
|
|
|
|
from .. import db |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#################### |
|
|
|
#################### |
|
|
|
@ -58,8 +60,8 @@ class view_result(object): |
|
|
|
def __init__(self): |
|
|
|
def __init__(self): |
|
|
|
self.id = 0 |
|
|
|
self.id = 0 |
|
|
|
self.title = '' |
|
|
|
self.title = '' |
|
|
|
self.date = 0 |
|
|
|
self.release_date = 0 |
|
|
|
self.description = '' |
|
|
|
self.overview = '' |
|
|
|
self.genre_ids = [] |
|
|
|
self.genre_ids = [] |
|
|
|
self.original_language = '' |
|
|
|
self.original_language = '' |
|
|
|
self.poster_path = 'no_image.png' |
|
|
|
self.poster_path = 'no_image.png' |
|
|
|
@ -70,7 +72,7 @@ class view_result(object): |
|
|
|
self.id = result['id'] |
|
|
|
self.id = result['id'] |
|
|
|
self.title = result['original_title'] |
|
|
|
self.title = result['original_title'] |
|
|
|
self.release_date = result['release_date'] |
|
|
|
self.release_date = result['release_date'] |
|
|
|
self.description = result['overview'] |
|
|
|
self.overview = result['overview'] |
|
|
|
self.genre_ids = result['genre_ids'] |
|
|
|
self.genre_ids = result['genre_ids'] |
|
|
|
self.original_language = result['original_language'] |
|
|
|
self.original_language = result['original_language'] |
|
|
|
self.poster_path = result['poster_path'] |
|
|
|
self.poster_path = result['poster_path'] |
|
|
|
@ -101,20 +103,27 @@ def query_movie(): |
|
|
|
query.raw_query_str = request.args['query'] |
|
|
|
query.raw_query_str = request.args['query'] |
|
|
|
|
|
|
|
|
|
|
|
if query.raw_query_str and parseQueryStr(query): |
|
|
|
if query.raw_query_str and parseQueryStr(query): |
|
|
|
if makeQuery(query, response): |
|
|
|
if isQueryCached(query.parsed_query_str): |
|
|
|
|
|
|
|
view_results = makeLocalQuery(query) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
makeTMDBQuery(query, response) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
response.parsed_response = json.loads(response.response_body) |
|
|
|
response.parsed_response = json.loads(response.response_body) |
|
|
|
response.result_count = response.parsed_response['total_results'] |
|
|
|
|
|
|
|
except json.JSONDecodeError as e: |
|
|
|
except json.JSONDecodeError as e: |
|
|
|
view_data.errors.append(e) |
|
|
|
view_data.errors.append(e) |
|
|
|
|
|
|
|
|
|
|
|
for result in response.parsed_response['results']: |
|
|
|
if 'total_results' in response.parsed_response: |
|
|
|
copy = view_result() |
|
|
|
response.result_count = response.parsed_response['total_results'] |
|
|
|
view_results.append(copy.copy_result(result, query.query_type)) |
|
|
|
|
|
|
|
else: |
|
|
|
# TODO: it's possible for the query results to span multiple 'pages' |
|
|
|
query.errors.append('{}, error making query'.format(util.getFunctionName)) |
|
|
|
# see) https://developers.themoviedb.org/3/search/search-movies |
|
|
|
else: |
|
|
|
if 'results' in response.parsed_response: |
|
|
|
errors.append('{}, error in query string'.format(util.getFunctionName())) |
|
|
|
for result in response.parsed_response['results']: |
|
|
|
|
|
|
|
copy = view_result() |
|
|
|
|
|
|
|
view_results.append(copy.copy_result(result, query.query_type)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
storeTMDBQueryResults(query, view_results) |
|
|
|
|
|
|
|
|
|
|
|
errors.extend(query.errors) |
|
|
|
errors.extend(query.errors) |
|
|
|
errors.extend(response.errors) |
|
|
|
errors.extend(response.errors) |
|
|
|
@ -154,12 +163,54 @@ def parseQueryStr(query_obj): |
|
|
|
|
|
|
|
|
|
|
|
# TODO: parse tv show titles |
|
|
|
# TODO: parse tv show titles |
|
|
|
if query_obj.query_type == query_type.UNKNOWN: |
|
|
|
if query_obj.query_type == query_type.UNKNOWN: |
|
|
|
query_obj.errors.append('%s: unable to determine query type' % util.getFunctionName()) |
|
|
|
query_obj.errors.append('{}: unable to determine query type'.format(util.getFunctionName())) |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
return True |
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def makeQuery(query_obj, response_obj): |
|
|
|
def isQueryCached(query_str): |
|
|
|
|
|
|
|
conn = db.get_db() |
|
|
|
|
|
|
|
cur = conn.cursor() |
|
|
|
|
|
|
|
cur.execute('SELECT COUNT(*) FROM movie_queries WHERE query=?', (query_str,)) |
|
|
|
|
|
|
|
count = cur.fetchone()[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('DEBUG: {}, count: "{}"'.format(util.getFunctionName(), count)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (count > 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# NOTE: returns list of 'view_result' objects that match the query_obj.parsed_query_str |
|
|
|
|
|
|
|
# stored in the local db cache |
|
|
|
|
|
|
|
def makeLocalQuery(query_obj): |
|
|
|
|
|
|
|
conn = db.get_db() |
|
|
|
|
|
|
|
cur = conn.cursor() |
|
|
|
|
|
|
|
results = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if query_obj.query_type == query_type.MOVIE: |
|
|
|
|
|
|
|
cur.execute('SELECT * FROM movie_queries WHERE query=?', (query_obj.parsed_query_str,)) |
|
|
|
|
|
|
|
elif query_obj.query_type == query_type.TV_SHOW: |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('DEBUG: {}, number of rows matched: "{}"'.format( |
|
|
|
|
|
|
|
util.getFunctionName(), len(cur.fetchall())) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(cur.fetchall()) > 0: |
|
|
|
|
|
|
|
print('DEBUG: {}(), query string "{}", match found in local db'.format( |
|
|
|
|
|
|
|
util.getFunctionName(), query_obj.parsed_query_str) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for row in cur: |
|
|
|
|
|
|
|
vr = view_result() |
|
|
|
|
|
|
|
vr.title = row["title"] |
|
|
|
|
|
|
|
results.append(vr) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
print('DEBUG: {}(), query string "{}", no matches found in local db'.format( |
|
|
|
|
|
|
|
util.getFunctionName(), query_obj.parsed_query_str) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return results |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def makeTMDBQuery(query_obj, response_obj): |
|
|
|
if query_obj.query_type == query_type.MOVIE: |
|
|
|
if query_obj.query_type == query_type.MOVIE: |
|
|
|
search_part = TMDB_SEARCH_MOVIE |
|
|
|
search_part = TMDB_SEARCH_MOVIE |
|
|
|
elif query_obj.query_type == query_type.TV_SHOW: |
|
|
|
elif query_obj.query_type == query_type.TV_SHOW: |
|
|
|
@ -208,3 +259,29 @@ def makeQuery(query_obj, response_obj): |
|
|
|
|
|
|
|
|
|
|
|
return True |
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def storeTMDBQueryResults(query_obj, result_list): |
|
|
|
|
|
|
|
conn = db.get_db() |
|
|
|
|
|
|
|
cur = conn.cursor() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('DEBUG: {}, adding query string "{}" into local cache'.format( |
|
|
|
|
|
|
|
util.getFunctionName(), query_obj.parsed_query_str) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
cur.execute('INSERT INTO movie_queries(query, year) VALUES (?, ?)', |
|
|
|
|
|
|
|
(query_obj.parsed_query_str, query_obj.movie_year) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
except DatabaseError as e: |
|
|
|
|
|
|
|
query_obj.errors.append(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for result in result_list: |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
cur.execute("INSERT INTO movie_details(" |
|
|
|
|
|
|
|
+ "movie_id, title, overview, release_date, poster_path)" |
|
|
|
|
|
|
|
+ " VALUES (?,?,?,?,?)", |
|
|
|
|
|
|
|
(result.id, result.title, result.overview, result.release_date, result.poster_path) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
except DatabaseError as e: |
|
|
|
|
|
|
|
query_obj.errors.append(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn.commit() |
|
|
|
|