
__license__   = 'GPL v3'
__copyright__ = '2010, Darko Miletic <darko.miletic at gmail.com>'
'''
bhdani.com
'''

import re
from calibre import strftime
from calibre.web.feeds.news import BasicNewsRecipe

class BHDani(BasicNewsRecipe):
    title                  = 'Dani'
    __author__             = 'Darko Miletic'
    description            = 'On line izdanje najutjecajnijeg bosanskohercegovackog magazina'
    publisher              = 'd.o.o. CIVITAS'
    category               = 'dani, bh, bhdani, magazin, sarajevo, bosna, novine, mediji, listovi, news, magazines, weekly'
    no_stylesheets         = True
    oldest_article         = 15
    encoding               = 'cp1250'
    needs_subscription     = True
    remove_empty_feeds     = True
    PREFIX                 = 'http://bhdani.com'
    INDEX                  = PREFIX + '/'
    LOGIN                  = PREFIX + '/users/login.asp'
    use_embedded_content   = False
    language               = 'bs'
    publication_type       = 'magazine'
    extra_css              = ' @font-face {font-family: "sans1";src:url(res:///opt/sony/ebook/FONT/tt0003m_.ttf)} body{font-family: Arial, sans1, sans-serif} .article_description{font-family: Arial, sans1, sans-serif} .plv18{font-size: xx-large; font-weight: bold; color: #5261A9} .crn10{font-weight: bold} '

    conversion_options = {
                          'comment'          : description
                        , 'tags'             : category
                        , 'publisher'        : publisher
                        , 'language'         : language
                        , 'linearize_tables' : True
                        }

    preprocess_regexps = [(re.compile(u'\u0110'), lambda match: u'\u00D0')]
    remove_attributes = ['height','width','align']

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        if self.username is not None and self.password is not None:
            br.open(self.INDEX)
            br.select_form(name='form')
            br['username'] = self.username
            br['password'] = self.password
            br.submit()
        return br

    remove_tags       = [dict(name=['link','embed','object','iframe','form'])]
    remove_tags_before= dict(name='div',attrs={'class':'crn10'})

    def get_cover_url(self):
        cover_url = None
        soup = self.index_to_soup(self.INDEX)
        link_item = soup.find('img',attrs={'alt':'Naslovna strana'})
        if link_item:
           cover_url = self.PREFIX + link_item['src'].replace('&slika=slika120&','&slika=slika400&')
        return cover_url

    def print_version(self, url):
        return url.replace('/default.asp?','/print.asp?')

    def parse_index(self):
        articles = []
        soup = self.index_to_soup(self.PREFIX)
        nrtit = soup.find('font',attrs={'class':'broj'})
        nrtitle = 'Dani'
        if nrtit:
           nrtitle += ' ' + self.tag_to_string(nrtit)
        for item in soup.findAll('a',attrs={'class':['naslov12','menilink2']}):
            url   = self.PREFIX + item['href']
            title = self.tag_to_string(item)
            description = ''
            date = strftime(self.timefmt)
            articles.append({
                              'title'      :title
                             ,'date'       :date
                             ,'url'        :url
                             ,'description':description
                            })
        return [(nrtitle, articles)]

