def _get_cover(soup, rdr): ans = None try: ans = soup.find('img', alt=re.compile('cover', flags=re.I))['src'] except TypeError: # meeehh, no handy alt-tag goodness, try some hackery # the basic idea behind this is that in general, the cover image # has a height:width ratio of ~1.25, whereas most of the nav # buttons are decidedly less than that. # what we do in this is work out that ratio, take 1.25 off it and # save the absolute value when we sort by this value, the smallest # one is most likely to be the cover image, hopefully. r = {} for img in soup('img'): try: r[abs( float(re.search(r'[0-9.]+', img['height']).group()) / float(re.search(r'[0-9.]+', img['width']).group()) - 1.25)] = img['src'] except KeyError: # interestingly, occasionally the only image without height # or width attrs is the cover... r[0] = img['src'] except: # Probably invalid width, height aattributes, ignore continue if r: l = sorted(iterkeys(r)) ans = r[l[0]] # this link comes from the internal html, which is in a subdir if ans is not None: try: ans = rdr.GetFile(ans) except: ans = rdr.root + "/" + ans try: ans = rdr.GetFile(ans) except: ans = None if ans is not None: from PIL import Image import io buf = io.BytesIO() try: Image.open(io.BytesIO(ans)).convert('RGB').save(buf, 'JPEG') ans = buf.getvalue() except: ans = None return ans
def _get_cover(soup, rdr): ans = None try: ans = soup.find('img', alt=re.compile('cover', flags=re.I))['src'] except TypeError: # meeehh, no handy alt-tag goodness, try some hackery # the basic idea behind this is that in general, the cover image # has a height:width ratio of ~1.25, whereas most of the nav # buttons are decidedly less than that. # what we do in this is work out that ratio, take 1.25 off it and # save the absolute value when we sort by this value, the smallest # one is most likely to be the cover image, hopefully. r = {} for img in soup('img'): try: r[abs(float(re.search(r'[0-9.]+', img['height']).group())/float(re.search(r'[0-9.]+', img['width']).group())-1.25)] = img['src'] except KeyError: # interestingly, occasionally the only image without height # or width attrs is the cover... r[0] = img['src'] except: # Probably invalid width, height aattributes, ignore continue if r: l = sorted(iterkeys(r)) ans = r[l[0]] # this link comes from the internal html, which is in a subdir if ans is not None: try: ans = rdr.GetFile(ans) except: ans = rdr.root + "/" + ans try: ans = rdr.GetFile(ans) except: ans = None if ans is not None: from PIL import Image import io buf = io.BytesIO() try: Image.open(io.BytesIO(ans)).convert('RGB').save(buf, 'JPEG') ans = buf.getvalue() except: ans = None return ans