from calibre.web.feeds.recipes import BasicNewsRecipe


class PsychologyToday(BasicNewsRecipe):

    title = 'Psychology Today'
    __author__ = 'Kovid Goyal'

    description = ('This magazine takes information from the latest research'
                   ' in the field of psychology and makes it useful to people in their everyday'
                   ' lives. Its coverage encompasses self-improvement, relationships, the mind-body'
                   ' connection, health, family, the workplace and culture.')
    language = 'en'
    encoding = 'UTF-8'
    no_javascript = True
    no_stylesheets = True

    keep_only_tags = [
        dict(role='main'),
    ]
    remove_tags = [
        dict(attrs={'class': ['pt-social-media', 'fb-like-button']}),
    ]

    def parse_index(self):
        soup = self.index_to_soup('http://www.psychologytoday.com/magazine')
        div = soup.find(id='block-views-magazine-issues-block')
        a = div.findAll(
            'h3', attrs={'class': 'magazine-published-date'})[1].find('a')
        self.timefmt = ' [%s]' % self.tag_to_string(a).capitalize()
        soup = self.index_to_soup('http://www.psychologytoday.com' + a['href'])
        self.cover_url = soup.find(role='main').find(
            'img', src=lambda x: x and '/field_magazine_cover/' in x)['src'].partition('?')[0]
        div = soup.find(id='block-system-main')
        articles = []
        for x in div.findAll(attrs={'class': 'field__item'}):
            h2 = x.find('h2')
            title = self.tag_to_string(h2)
            url = 'http://www.psychologytoday.com' + h2.find('a')['href']
            self.log('\n', title, 'at', url)
            desc = ''
            for y in x.findAll(attrs={'class': ['subtext', 'collection__subtitle']}):
                desc += self.tag_to_string(y) + ' '
            if desc:
                self.log(desc)
            articles.append({'title': title, 'url': url, 'description': desc})

        return [('Current Issue', articles)]
