'Extracts main article content from \'html\', cleans up and returns as a (article_html, extracted_title) tuple.
Based on the original readability algorithm by Arc90.'

def extract_readable_article(self, html, url):

    from calibre.ebooks.readability import readability
    from lxml.html import fragment_fromstring, tostring, document_fromstring
    doc = readability.Document(html, self.log, url=url, keep_elements=self.auto_cleanup_keep)
    article_html = doc.summary()
    extracted_title = doc.title()
    try:
        frag = fragment_fromstring(article_html)
    except:
        doc = document_fromstring(article_html)
        frag = doc.xpath('//body')[(-1)]
    if (frag.tag == 'html'):
        root = frag
    elif (frag.tag == 'body'):
        root = document_fromstring((u'<html><head><title>%s</title></head></html>' % extracted_title))
        root.append(frag)
    else:
        root = document_fromstring((u'<html><head><title>%s</title></head><body/></html>' % extracted_title))
        root.xpath('//body')[0].append(frag)
    body = root.xpath('//body')[0]
    has_title = False
    for x in body.iterdescendants():
        if (x.text == extracted_title):
            has_title = True
    inline_titles = body.xpath('//h1|//h2')
    if ((not has_title) and (not inline_titles)):
        heading = body.makeelement('h2')
        heading.text = extracted_title
        body.insert(0, heading)
    raw_html = tostring(root, encoding=unicode)
    return raw_html
