2012-09-13 15:27:34 +00:00
# This file is part of Mylar.
#
# Mylar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mylar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mylar. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
import time
import threading
2014-01-29 05:06:00 +00:00
import urllib , urllib2
2012-09-13 15:27:34 +00:00
from xml . dom . minidom import parseString , Element
import mylar
from mylar import logger , db , cv
from mylar . helpers import multikeysort , replace_all , cleanName
mb_lock = threading . Lock ( )
2014-06-02 19:02:28 +00:00
def pullsearch ( comicapi , comicquery , offset , explicit ) :
2014-01-29 05:06:00 +00:00
u_comicquery = urllib . quote ( comicquery . encode ( ' utf-8 ' ) . strip ( ) )
2014-02-26 19:48:50 +00:00
u_comicquery = u_comicquery . replace ( " " , " % 20 " )
2014-06-04 18:53:33 +00:00
if explicit == ' all ' or explicit == ' loose ' :
2014-06-02 19:02:28 +00:00
PULLURL = mylar . CVURL + ' search?api_key= ' + str ( comicapi ) + ' &resources=volume&query= ' + u_comicquery + ' &field_list=id,name,start_year,site_detail_url,count_of_issues,image,publisher,description&format=xml&offset= ' + str ( offset )
2014-02-26 19:48:50 +00:00
2014-06-02 19:02:28 +00:00
else :
2014-06-04 18:53:33 +00:00
# 02/22/2014 use the volume filter label to get the right results.
2014-06-02 19:02:28 +00:00
PULLURL = mylar . CVURL + ' volumes?api_key= ' + str ( comicapi ) + ' &filter=name: ' + u_comicquery + ' &field_list=id,name,start_year,site_detail_url,count_of_issues,image,publisher,description&format=xml&offset= ' + str ( offset ) # 2012/22/02 - CVAPI flipped back to offset instead of page
2014-02-26 19:48:50 +00:00
2012-09-13 15:27:34 +00:00
#all these imports are standard on most modern python implementations
#download the file:
2013-03-27 07:03:10 +00:00
try :
file = urllib2 . urlopen ( PULLURL )
except urllib2 . HTTPError , err :
2014-01-29 05:06:00 +00:00
logger . error ( ' err : ' + str ( err ) )
2013-03-27 07:03:10 +00:00
logger . error ( " There was a major problem retrieving data from ComicVine - on their end. You ' ll have to try again later most likely. " )
return
2012-09-13 15:27:34 +00:00
#convert to string:
data = file . read ( )
#close file because we dont need it anymore:
file . close ( )
#parse the xml you downloaded
dom = parseString ( data )
return dom
2014-06-02 19:02:28 +00:00
def findComic ( name , mode , issue , limityear = None , explicit = None ) :
2012-09-13 15:27:34 +00:00
#with mb_lock:
comiclist = [ ]
comicResults = None
chars = set ( ' !?* ' )
if any ( ( c in chars ) for c in name ) :
name = ' " ' + name + ' " '
2013-02-06 19:55:23 +00:00
2013-03-12 16:13:43 +00:00
#print ("limityear: " + str(limityear))
2013-02-06 19:55:23 +00:00
if limityear is None : limityear = ' None '
2014-06-02 19:02:28 +00:00
2014-02-26 19:48:50 +00:00
comicquery = name
2014-01-29 05:06:00 +00:00
#comicquery=name.replace(" ", "%20")
2014-06-02 19:02:28 +00:00
if explicit is None :
2014-06-04 18:53:33 +00:00
#logger.fdebug('explicit is None. Setting to Default mode of ALL search words.')
comicquery = name . replace ( " " , " AND " )
explicit = ' all '
#OR
if explicit == ' loose ' :
logger . fdebug ( ' Changing to loose mode - this will match ANY of the search words ' )
comicquery = name . replace ( " " , " OR " )
elif explicit == ' explicit ' :
logger . fdebug ( ' Changing to explicit mode - this will match explicitly on the EXACT words ' )
2014-06-02 19:02:28 +00:00
comicquery = name . replace ( " " , " AND " )
else :
2014-06-04 18:53:33 +00:00
logger . fdebug ( ' Default search mode - this will match on ALL search words ' )
comicquery = name . replace ( " " , " AND " )
explicit = ' all '
2014-06-09 07:55:05 +00:00
if mylar . COMICVINE_API == ' None ' or mylar . COMICVINE_API == mylar . DEFAULT_CVAPI :
logger . warn ( ' You have not specified your own ComicVine API key - alot of things will be limited. Get your own @ http://api.comicvine.com. ' )
comicapi = mylar . DEFAULT_CVAPI
else :
comicapi = mylar . COMICVINE_API
2012-09-13 15:27:34 +00:00
#let's find out how many results we get from the query...
2014-06-02 19:02:28 +00:00
searched = pullsearch ( comicapi , comicquery , 0 , explicit )
2013-03-27 07:03:10 +00:00
if searched is None : return False
2012-09-13 15:27:34 +00:00
totalResults = searched . getElementsByTagName ( ' number_of_total_results ' ) [ 0 ] . firstChild . wholeText
2014-02-26 19:48:50 +00:00
logger . fdebug ( " there are " + str ( totalResults ) + " search results... " )
2012-09-13 15:27:34 +00:00
if not totalResults :
return False
countResults = 0
2013-03-29 11:16:55 +00:00
while ( countResults < int ( totalResults ) ) :
2014-02-26 19:48:50 +00:00
#logger.fdebug("querying " + str(countResults))
2012-12-12 22:15:06 +00:00
if countResults > 0 :
2014-03-02 19:57:57 +00:00
#2012/22/02 - CV API flipped back to offset usage instead of page
offsetcount = countResults
2014-02-26 19:48:50 +00:00
2014-06-02 19:02:28 +00:00
searched = pullsearch ( comicapi , comicquery , offsetcount , explicit )
2012-12-12 22:15:06 +00:00
comicResults = searched . getElementsByTagName ( ' volume ' )
2012-09-13 15:27:34 +00:00
body = ' '
n = 0
if not comicResults :
break
for result in comicResults :
#retrieve the first xml tag (<tag>data</tag>)
#that the parser finds with name tagName:
2012-12-12 22:15:06 +00:00
xmlcnt = result . getElementsByTagName ( ' count_of_issues ' ) [ 0 ] . firstChild . wholeText
2012-09-13 15:27:34 +00:00
#here we can determine what called us, and either start gathering all issues or just limited ones.
#print ("n: " + str(n) + "--xmcnt" + str(xmlcnt))
2013-04-22 03:11:12 +00:00
if issue is not None and str ( issue ) . isdigit ( ) :
2013-03-05 01:05:26 +00:00
#this gets buggered up with NEW/ONGOING series because the db hasn't been updated
#to reflect the proper count. Drop it by 1 to make sure.
limiter = int ( issue ) - 1
2012-09-13 15:27:34 +00:00
else : limiter = 0
if int ( xmlcnt ) > = limiter :
2013-02-06 19:55:23 +00:00
2012-12-12 22:15:06 +00:00
xmlTag = result . getElementsByTagName ( ' name ' ) [ 0 ] . firstChild . wholeText
if ( result . getElementsByTagName ( ' start_year ' ) [ 0 ] . firstChild ) is not None :
xmlYr = result . getElementsByTagName ( ' start_year ' ) [ 0 ] . firstChild . wholeText
2012-09-13 15:27:34 +00:00
else : xmlYr = " 0000 "
2013-02-06 19:55:23 +00:00
if xmlYr in limityear or limityear == ' None ' :
xmlurl = result . getElementsByTagName ( ' site_detail_url ' ) [ 0 ] . firstChild . wholeText
xmlid = result . getElementsByTagName ( ' id ' ) [ 0 ] . firstChild . wholeText
publishers = result . getElementsByTagName ( ' publisher ' )
if len ( publishers ) > 0 :
pubnames = publishers [ 0 ] . getElementsByTagName ( ' name ' )
if len ( pubnames ) > 0 :
xmlpub = pubnames [ 0 ] . firstChild . wholeText
2013-05-09 02:35:12 +00:00
else :
xmlpub = " Unknown "
else :
xmlpub = " Unknown "
2013-02-06 19:55:23 +00:00
if ( result . getElementsByTagName ( ' name ' ) [ 0 ] . childNodes [ 0 ] . nodeValue ) is None :
xmlimage = result . getElementsByTagName ( ' super_url ' ) [ 0 ] . firstChild . wholeText
else :
xmlimage = " cache/blankcover.jpg "
2013-07-01 05:19:15 +00:00
try :
xmldesc = result . getElementsByTagName ( ' description ' ) [ 0 ] . firstChild . wholeText
except :
xmldesc = " None "
2013-02-06 19:55:23 +00:00
comiclist . append ( {
' name ' : xmlTag ,
' comicyear ' : xmlYr ,
' comicid ' : xmlid ,
' url ' : xmlurl ,
' issues ' : xmlcnt ,
' comicimage ' : xmlimage ,
2013-07-01 05:19:15 +00:00
' publisher ' : xmlpub ,
' description ' : xmldesc
2013-02-06 19:55:23 +00:00
} )
2012-09-13 15:27:34 +00:00
else :
2014-03-19 18:54:39 +00:00
logger . fdebug ( ' year: ' + str ( xmlYr ) + ' - contraint not met. Has to be within ' + str ( limityear ) )
2013-02-06 19:55:23 +00:00
n + = 1
2013-03-27 07:03:10 +00:00
#search results are limited to 100 and by pagination now...let's account for this.
countResults = countResults + 100
2012-09-13 15:27:34 +00:00
2014-06-04 18:53:33 +00:00
return comiclist , explicit