Browse Source

add cutomised logging in util.py

master
cinnaboot 7 years ago
parent
commit
a506db324f
  1. 19
      video_metadata/util.py
  2. 33
      video_metadata/views/tmdb.py

19
video_metadata/util.py

@ -1,4 +1,23 @@
import logging
import sys
def getLogger(is_debug):
if (is_debug):
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
formatter = logging.Formatter(
fmt='%(asctime)s, %(levelname)-5s [%(filename)s:%(lineno)d, %(funcName)s] %(message)s'
)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
# NOTE: modified from here: https://code.activestate.com/recipes/137951-dump-all-the-attributes-of-an-object/ # NOTE: modified from here: https://code.activestate.com/recipes/137951-dump-all-the-attributes-of-an-object/
# to only print attributes # to only print attributes
def dumpObjAttr(obj): def dumpObjAttr(obj):

33
video_metadata/views/tmdb.py

@ -1,6 +1,5 @@
import cgi import cgi
from contextlib import closing
from datetime import date from datetime import date
from enum import Enum from enum import Enum
import json import json
@ -11,7 +10,7 @@ from urllib.error import URLError, HTTPError
import urllib.parse import urllib.parse
import urllib.request import urllib.request
from flask import Blueprint, current_app, flash, request, render_template from flask import Blueprint, current_app, flash, Flask, request, render_template
from .. import util from .. import util
from .. import db from .. import db
@ -20,7 +19,6 @@ from .. import db
#################### ####################
# constants # constants
bp = Blueprint('tmdb', __name__, url_prefix='/tmdb')
TMDB_API_URL = 'https://api.themoviedb.org' TMDB_API_URL = 'https://api.themoviedb.org'
TMDB_SEARCH_MOVIE = '/3/search/movie' TMDB_SEARCH_MOVIE = '/3/search/movie'
TMDB_SEARCH_TV = '/3/search/tv' TMDB_SEARCH_TV = '/3/search/tv'
@ -28,6 +26,13 @@ TMDB_MOVIE_LINK_URL = 'https://www.themoviedb.org/movie/'
TMDB_TV_LINK_URL = 'https://www.themoviedb.org/tv/' TMDB_TV_LINK_URL = 'https://www.themoviedb.org/tv/'
####################
# globals
bp = Blueprint('tmdb', __name__, url_prefix='/tmdb')
logger = util.getLogger(Flask(__name__).config['DEBUG'])
#################### ####################
# data structures # data structures
@ -90,6 +95,10 @@ class view_result(object):
@bp.route('/query_movie') @bp.route('/query_movie')
def query_movie(): def query_movie():
logger.debug('Flask(__name__): {}'.format(Flask(__name__)))
logger.debug('debug: {}'.format(Flask(__name__).config['DEBUG']))
errors = [] errors = []
query = query_data() query = query_data()
response = response_data() response = response_data()
@ -174,7 +183,7 @@ def isQueryCached(query_str):
cur.execute('SELECT COUNT(*) FROM movie_queries WHERE query=?', (query_str,)) cur.execute('SELECT COUNT(*) FROM movie_queries WHERE query=?', (query_str,))
count = cur.fetchone()[0] count = cur.fetchone()[0]
print('DEBUG: {}, count: "{}"'.format(util.getFunctionName(), count)) logger.debug('count: "{}"'.format(count))
return (count > 0) return (count > 0)
@ -190,13 +199,11 @@ def makeLocalQuery(query_obj):
elif query_obj.query_type == query_type.TV_SHOW: elif query_obj.query_type == query_type.TV_SHOW:
pass pass
print('DEBUG: {}, number of rows matched: "{}"'.format( logger.debug('number of rows matched: "{}"'.format(len(cur.fetchall())))
util.getFunctionName(), len(cur.fetchall()))
)
if len(cur.fetchall()) > 0: if len(cur.fetchall()) > 0:
print('DEBUG: {}(), query string "{}", match found in local db'.format( logger.debug('query string "{}", match found in local db'.format(
util.getFunctionName(), query_obj.parsed_query_str) query_obj.parsed_query_str)
) )
for row in cur: for row in cur:
@ -204,8 +211,8 @@ def makeLocalQuery(query_obj):
vr.title = row["title"] vr.title = row["title"]
results.append(vr) results.append(vr)
else: else:
print('DEBUG: {}(), query string "{}", no matches found in local db'.format( logger.debug('query string "{}", no matches found in local db'.format(
util.getFunctionName(), query_obj.parsed_query_str) query_obj.parsed_query_str)
) )
return results return results
@ -263,9 +270,7 @@ def storeTMDBQueryResults(query_obj, result_list):
conn = db.get_db() conn = db.get_db()
cur = conn.cursor() cur = conn.cursor()
print('DEBUG: {}, adding query string "{}" into local cache'.format( logger.debug('adding query string "{}" into local cache'.format(query_obj.parsed_query_str))
util.getFunctionName(), query_obj.parsed_query_str)
)
try: try:
cur.execute('INSERT INTO movie_queries(query, year) VALUES (?, ?)', cur.execute('INSERT INTO movie_queries(query, year) VALUES (?, ?)',

Loading…
Cancel
Save