__author__ = 'krawallmieze'

from baseparser import BaseParser
from BeautifulSoup import BeautifulSoup, Tag


class TAZParser(BaseParser):
    domains = ['www.taz.de']

    feeder_pat   = '.+\/!\d{7}'
    feeder_pages = ['http://www.taz.de/']

    def _parse(self, html):
        soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES,
                             fromEncoding='utf-8')
        self.meta = soup.findAll('meta')
        self.source = ', '.join(self.domains)
        #article headline
        elt = soup.find('meta', {'property': 'og:title'})
        if elt is None:
            self.real_article = False
            return
        else:
            self.title = elt['content']
        # tags from meta-keywords and title
        meta_keywords = soup.find('meta', {'name': 'keywords'})['content'] if soup.find('meta', {'name': 'keywords'}) else ""
        self.tags = self.extract_keywords(meta_keywords)
        self.tags += self.extract_keywords(self.title)
        # byline / author
        author = soup.find('meta', {'name': 'author'})
        self.byline = author['content'] if (author and not ('taz' in author)) else ''
        self._cleanByline()
        # article date
        created_at = soup.find('span', {'class': 'date'})
        if created_at is None:
            self.real_article = False
            return
        self.date = ''.join(created_at.getText().replace("\n", "").split()) if created_at else ''
        # category
        self.category = self.compute_category(meta_keywords if meta_keywords else '')
        #article content
        div = soup.find('div', {'class': 'odd sect sect_article news report'})
        if div is None:
            self.real_article = False
            return
        div = div.find('div', {'class': 'sectbody'})
        if div is None:
            self.real_article = False
            return
        div = self.remove_non_content(div)
        map(lambda x: x.extract(), div.findAll('p', {'class':'caption'}))
        self.body = '\n'+'\n\n'.join([x.getText() for x in div.childGenerator()
                                      if isinstance(x, Tag) and x.name == 'p'])
@@ -1010,16 +1010,33 @@
  author%0A
+        try:%0A    
         
@@ -1081,16 +1081,55 @@
 uthor'%7D)
+%5B'content'%5D%0A        except: author = ''
 %0A       
@@ -1153,34 +1153,11 @@
 thor
-%5B'content'%5D if (author and
+ if
  not
@@ -1174,17 +1174,16 @@
  author)
-)
  else ''

extract author without 'taz'