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

'''
sarajevo-x.com
'''

import re
from calibre.web.feeds.recipes import BasicNewsRecipe
from calibre.ebooks.BeautifulSoup import Tag, NavigableString


def new_tag(soup, name, attrs=()):
    impl = getattr(soup, 'new_tag', None)
    if impl is not None:
        return impl(name, attrs=dict(attrs))
    return Tag(soup, name, attrs=attrs or None)


class SarajevoX(BasicNewsRecipe):
    title = 'Sarajevo-x.com'
    __author__ = 'Darko Miletic'
    description = 'Sarajevo-x.com - najposjeceniji bosanskohercegovacki internet portal'
    publisher = 'InterSoft d.o.o.'
    category = 'news, politics, Bosnia and Herzegovina,Sarajevo-x.com, internet, portal, vijesti, bosna i hercegovina, sarajevo'
    oldest_article = 2
    delay = 1
    max_articles_per_feed = 100
    no_stylesheets = True
    encoding = 'cp1250'
    use_embedded_content = False
    language = 'bs'
    extra_css = ' @font-face {font-family: "sans1";src:url(res:///opt/sony/ebook/FONT/tt0003m_.ttf)} body{font-family: Arial,Verdana,Helvetica,sans1,sans-serif} .article_description{font-family: sans1, sans-serif} div#fotka{display: block} img{margin-bottom: 0.5em} '  # noqa

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

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

    keep_only_tags = [dict(name='div', attrs={'class': 'content-bg'})]
    remove_tags_after = dict(name='div', attrs={'class': 'izvor'})
    remove_tags = [dict(name=['object', 'link', 'base', 'table'])]
    remove_attributes = ['height', 'width', 'alt', 'border']

    feeds = [

    (u'BIH', u'http://www.sarajevo-x.com/rss/bih'),
    (u'Svijet', u'http://www.sarajevo-x.com/rss/svijet'),
    (u'Biznis', u'http://www.sarajevo-x.com/rss/biznis'),
    (u'Sport', u'http://www.sarajevo-x.com/rss/sport'),
    (u'Showtime', u'http://www.sarajevo-x.com/rss/showtime'),
    (u'Scitech', u'http://www.sarajevo-x.com/rss/scitech'),
    (u'Lifestyle', u'http://www.sarajevo-x.com/rss/lifestyle'),
    (u'Kultura', u'http://www.sarajevo-x.com/rss/kultura'),
    (u'Zanimljivosti', u'http://www.sarajevo-x.com/rss/zanimljivosti')
    ]

    def preprocess_html(self, soup):
        dtag = soup.find('div', attrs={'id': 'fotka'})
        if dtag:
            sp = soup.find('div', attrs={'id': 'opisslike'})
            img = soup.find('img')
            if sp:
                sp
            else:
                mtag = new_tag(soup, 'div', [
                           ("id", "opisslike"), ("class", "opscitech")])
                mopis = NavigableString("Opis")
                mtag.insert(0, mopis)
                img.append(mtag)
        return soup
