Browse Source

store queries in local sqlite db

master
cinnaboot 7 years ago
parent
commit
0299b369be
  1. 2
      video_metadata/static/style.css
  2. 2
      video_metadata/templates/tmdb/query.html
  3. 105
      video_metadata/views/tmdb.py

2
video_metadata/static/style.css

@ -25,5 +25,5 @@ nav ul li a, nav ul li span, header .action { display: block; padding: 0.5rem; }
input { color: #333; background-color: #eee; }
input.danger { color: #cc2f2e; }
input[type=submit] { align-self: start; min-width: 10em; }
.debug { background: lightgrey; padding: 1em; overflow: auto; }
.debug { background: lightgrey; padding: 1em; overflow: auto; border: 1px solid darkgrey }
.search_result { max-width: 400px; padding: 1em; background: aquamarine; }

2
video_metadata/templates/tmdb/query.html

@ -21,7 +21,7 @@
<ul>
<li>Release Date: {{ r.release_date }}</li>
<li>Description:
<p>{{ r.description }}</p>
<p>{{ r.overview }}</p>
</li>
</ul>
</div>

105
video_metadata/views/tmdb.py

@ -5,6 +5,7 @@ from datetime import date
from enum import Enum
import json
import re
from sqlite3 import DatabaseError
import ssl
from urllib.error import URLError, HTTPError
import urllib.parse
@ -13,6 +14,7 @@ import urllib.request
from flask import Blueprint, current_app, flash, request, render_template
from .. import util
from .. import db
####################
@ -58,8 +60,8 @@ class view_result(object):
def __init__(self):
self.id = 0
self.title = ''
self.date = 0
self.description = ''
self.release_date = 0
self.overview = ''
self.genre_ids = []
self.original_language = ''
self.poster_path = 'no_image.png'
@ -70,7 +72,7 @@ class view_result(object):
self.id = result['id']
self.title = result['original_title']
self.release_date = result['release_date']
self.description = result['overview']
self.overview = result['overview']
self.genre_ids = result['genre_ids']
self.original_language = result['original_language']
self.poster_path = result['poster_path']
@ -101,20 +103,27 @@ def query_movie():
query.raw_query_str = request.args['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:
response.parsed_response = json.loads(response.response_body)
response.result_count = response.parsed_response['total_results']
except json.JSONDecodeError as e:
view_data.errors.append(e)
for result in response.parsed_response['results']:
copy = view_result()
view_results.append(copy.copy_result(result, query.query_type))
else:
query.errors.append('{}, error making query'.format(util.getFunctionName))
else:
errors.append('{}, error in query string'.format(util.getFunctionName()))
if 'total_results' in response.parsed_response:
response.result_count = response.parsed_response['total_results']
# TODO: it's possible for the query results to span multiple 'pages'
# see) https://developers.themoviedb.org/3/search/search-movies
if 'results' in response.parsed_response:
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(response.errors)
@ -154,12 +163,54 @@ def parseQueryStr(query_obj):
# TODO: parse tv show titles
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 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:
search_part = TMDB_SEARCH_MOVIE
elif query_obj.query_type == query_type.TV_SHOW:
@ -208,3 +259,29 @@ def makeQuery(query_obj, response_obj):
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()

Loading…
Cancel
Save