Skip to content

Commit

Permalink
Detect and reject animated gifs in Calibre because they come out so p…
Browse files Browse the repository at this point in the history
…oorly.
  • Loading branch information
JimmXinu committed Apr 22, 2015
1 parent 53ca251 commit ca6bc7f
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 14 deletions.
7 changes: 7 additions & 0 deletions fanficfare/exceptions.py
Expand Up @@ -24,6 +24,13 @@ def __init__(self,error):
def __str__(self):
return self.error

class RejectImage(Exception):
def __init__(self,error):
self.error=error

def __str__(self):
return self.error

class InvalidStoryURL(Exception):
def __init__(self,url,domain,example):
self.url=url
Expand Down
32 changes: 19 additions & 13 deletions fanficfare/story.py
Expand Up @@ -44,6 +44,8 @@

try:
from calibre.utils.magick import Image
from StringIO import StringIO
from gif import GifInfo, CHECK_IS_ANIMATED
convtype = {'jpg':'JPG', 'png':'PNG'}

def convert_image(url,data,sizes,grayscale,
Expand All @@ -55,6 +57,10 @@ def convert_image(url,data,sizes,grayscale,
owidth, oheight = img.size
nwidth, nheight = sizes
scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)

if normalize_format_name(img.format)=="gif" and GifInfo(StringIO(data),CHECK_IS_ANIMATED).frameCount > 1:
raise exceptions.RejectImage("Animated gifs come out purely--not going to use it.")

if scaled:
img.size = (nwidth, nheight)
export = True
Expand Down Expand Up @@ -224,7 +230,7 @@ def fit_image(width, height, pwidth, pheight):
"Devanagari":"hi",

## These are from/for AO3:

u'العربية':'ar',
u'беларуская':'be',
u'Български език':'bg',
Expand Down Expand Up @@ -325,7 +331,7 @@ def is_match(self,value):
else:
retval = self.match == value
#print(">>>>>>>>>>>>>%s==%s r: %s,%s=%s"%(self.match,value,self.negate,retval, self.negate != retval))

return self.negate != retval

def __str__(self):
Expand All @@ -338,7 +344,7 @@ def __str__(self):
else:
s='='
return u'InExMatch(%s %s%s %s)'%(self.keys,f,s,self.match)

## metakey[,metakey]=~pattern
## metakey[,metakey]==string
## *for* part lines. Effect only when trailing conditional key=~regexp matches
Expand All @@ -358,7 +364,7 @@ def set_in_ex_clude(setting):
match = InExMatch(line)
dest.append([match,condmatch])
return dest

## Two or three part lines. Two part effect everything.
## Three part effect only those key(s) lists.
## pattern=>replacement
Expand Down Expand Up @@ -433,7 +439,7 @@ def __init__(self, configuration):

def join_list(self, key, vallist):
return self.getConfig("join_string_"+key,u", ").replace(SPACE_REPLACE,' ').join(map(unicode, vallist))

def setMetadata(self, key, value, condremoveentities=True):

# keep as list type, but set as only value.
Expand All @@ -445,20 +451,20 @@ def setMetadata(self, key, value, condremoveentities=True):
self.metadata[key]=conditionalRemoveEntities(value)
else:
self.metadata[key]=value

if key == "language":
try:
# getMetadata not just self.metadata[] to do replace_metadata.
self.setMetadata('langcode',langs[self.getMetadata(key)])
except:
self.setMetadata('langcode','en')

if key == 'dateUpdated' and value:
# Last Update tags for Bill.
self.addToList('lastupdate',value.strftime("Last Update Year/Month: %Y/%m"))
self.addToList('lastupdate',value.strftime("Last Update: %Y/%m/%d"))


def do_in_ex_clude(self,which,value,key):
if value and which in self.in_ex_cludes:
include = 'include' in which
Expand Down Expand Up @@ -487,7 +493,7 @@ def do_in_ex_clude(self,which,value,key):
if include and keyfound and not found:
value = None
return value


def doReplacements(self,value,key,return_list=False,seen_list=[]):
value = self.do_in_ex_clude('include_metadata_pre',value,key)
Expand Down Expand Up @@ -526,7 +532,7 @@ def doReplacements(self,value,key,return_list=False,seen_list=[]):
# print("replacement,value:%s,%s->%s"%(replacement,value,regexp.sub(replacement,value)))
value = regexp.sub(replacement,value)
retlist = [value]

for val in retlist:
retlist = map(partial(self.do_in_ex_clude,'include_metadata_post',key=key),retlist)
retlist = map(partial(self.do_in_ex_clude,'exclude_metadata_post',key=key),retlist)
Expand Down Expand Up @@ -610,7 +616,7 @@ def getAllMetadata(self,
self.getMetadata('author', removeallentities, doreplacements)))

self.extendList("extratags",self.getConfigList("extratags"))

if self.getMetadataRaw('seriesUrl'):
self.setMetadata('seriesHTML',linkhtml%('series',self.getMetadata('seriesUrl', removeallentities, doreplacements),
self.getMetadata('series', removeallentities, doreplacements)))
Expand Down Expand Up @@ -703,10 +709,10 @@ def getList(self,listname,
for val in retlist:
newretlist.extend(self.doReplacements(val,listname,return_list=True))
retlist = newretlist

if removeallentities:
retlist = map(removeAllEntities,retlist)

retlist = filter( lambda x : x!=None and x!='' ,retlist)

# reorder ships so b/a and c/b/a become a/b and a/b/c. Only on '/',
Expand Down

0 comments on commit ca6bc7f

Please sign in to comment.