Added caching of the alt cover image URL list

git-svn-id: http://comictagger.googlecode.com/svn/trunk@393 6c5673fe-1810-88d6-992b-cd32ca31540c
This commit is contained in:
beville@gmail.com 2013-02-02 18:41:06 +00:00
parent 7225f261f1
commit 75471aaddc
2 changed files with 61 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import datetime
import ctversion
from settings import ComicTaggerSettings
import utils
class ComicVineCacher:
@ -97,6 +98,13 @@ class ComicVineCacher:
"PRIMARY KEY (id) )"
)
cur.execute("CREATE TABLE AltCovers(" +
"issue_id INT," +
"url_list TEXT," +
"timestamp DATE DEFAULT (datetime('now','localtime')), " +
"PRIMARY KEY (issue_id) )"
)
cur.execute("CREATE TABLE Issues(" +
"id INT," +
"volume_id INT," +
@ -150,7 +158,7 @@ class ComicVineCacher:
url,
record['description'])
)
def get_search_results( self, search_term ):
results = list()
@ -185,7 +193,47 @@ class ComicVineCacher:
return results
def add_alt_covers( self, issue_id, url_list ):
con = lite.connect( self.db_file )
with con:
con.text_factory = unicode
cur = con.cursor()
# remove all previous entries with this search term
cur.execute("DELETE FROM AltCovers WHERE issue_id = ?", [ issue_id ])
url_list_str = utils.listToString(url_list)
# now add in new record
cur.execute("INSERT INTO AltCovers " +
"(issue_id, url_list ) " +
"VALUES( ?, ? )" ,
( issue_id,
url_list_str)
)
def get_alt_covers( self, issue_id ):
con = lite.connect( self.db_file )
with con:
cur = con.cursor()
con.text_factory = unicode
# purge stale issue info - probably issue data won't change much....
a_month_ago = datetime.datetime.today()-datetime.timedelta(days=30)
cur.execute( "DELETE FROM AltCovers WHERE timestamp < ?", [ str(a_month_ago) ] )
cur.execute("SELECT url_list FROM AltCovers WHERE issue_id=?", [ issue_id ])
row = cur.fetchone()
if row is None :
return None
else:
url_list_str = row[0]
url_list = url_list_str.split(",")
return url_list
def add_volume_info( self, cv_volume_record ):
con = lite.connect( self.db_file )

View File

@ -386,6 +386,14 @@ class ComicVineTalker(QObject):
cvc.add_issue_select_details( issue_id, image_url, thumb_url, month, year, page_url )
def fetchAlternateCoverURLs(self, issue_id):
# before we search online, look in our cache, since we might already
# have this info
cvc = ComicVineCacher( )
url_list = cvc.get_alt_covers( issue_id )
if url_list is not None:
return url_list
issue_page_url = self.fetchIssuePageURL( issue_id )
# scrape the CV issue page URL to get the alternate cover URLs
@ -403,6 +411,10 @@ class ComicVineTalker(QObject):
c = d['class']
if 'content-pod' in c and 'alt-cover' in c:
alt_cover_url_list.append( d.img['src'] )
# cache this alt cover URL list
cvc.add_alt_covers( issue_id, alt_cover_url_list )
return alt_cover_url_list
#---------------------------------------------------------------------------