#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals

from datetime import datetime

from calibre.web.feeds.news import BasicNewsRecipe


class PressePortalDE(BasicNewsRecipe):
    # Title of the Recipe
    title = 'Presseportal DE'
    # Author
    __author__ = 'Volker Heggemann, VoHe'
    # oldest article to download (in days)                                  ---- can be edit by user
    oldest_article = 1
    # describes itself,                                                     ---- can be edit by user
    max_articles_per_feed = 100
    # speed up the download on fast computers be careful (I test max.20)
    # ---- can be edit by user
    simultaneous_downloads = 10
    # description, some Reader show this in titlepage
    description = 'Presseportal News Feed DE'
    # add date to description so for dayly downloads you can find them easier
    # ---- can be edit by user
    description = description + ' fetched: ' + datetime.now().strftime('%Y-%m-%d')  # %H:%M:%S")
    # Who published the content?
    publisher = 'Presseportal.de'
    # What is the content of?
    category = 'latest news, national Police, Germany'
    # describes itself,                                                     ---- can be edit by user
    use_embedded_content = False
    # describes itself,                                                     ---- can be edit by user
    language = 'de'
    # encoding of content. e.g. utf-8, None, ...
    # ---- can be edit by user
    encoding = None  # 'utf-8' doesn't work here
    # Removes javascript- why keep this, we only want static content
    remove_javascript = True
    # Removes empty feeds - why keep them!?
    remove_empty_feeds = True

    # remove the rubbish (in ebook)
    auto_cleanup = True
    # now the content description and URL follows
    # feel free to add, wipe out what you need                              ---- can be edit by user
    #
    # some of this are double
    # so if you load "Alle Ressort" you don't need "Wirtschaft, Finanzen, Politik, Vermischtes..." at all.
    #
    #
    # Make some tests, may you first comment all of them out, and step by step you add what you'll need?
    #

    feeds = [  # Ressorts
        # Add or Remove the '#' symbol in front of line, to get or discard this rss-feed
        #
        # ---- each line can be edit by user
        #
        # (u'Alle Ressort',u'http://www.presseportal.de/rss/presseportal.rss2'),
        ('Wirtschaft', 'http://www.presseportal.de/rss/wirtschaft.rss2'),
        ('Finanzen', 'http://www.presseportal.de/rss/finanzen.rss2'),
        ('Politik', 'http://www.presseportal.de/rss/politik.rss2'),
        ('Vermischtes', 'http://www.presseportal.de/rss/vermischtes.rss2'),
        ('Sport', 'http://www.presseportal.de/rss/sport.rss2'),
        ('Kultur', 'http://www.presseportal.de/rss/kultur.rss2'),
        # Branchen
        ('Arbeit', 'http://www.presseportal.de/rss/arbeit.rss2'),
        ('Auto', 'http://www.presseportal.de/rss/auto.rss2'),
        ('Banken', 'http://www.presseportal.de/rss/banken.rss2'),
        ('Bildung', 'http://www.presseportal.de/rss/bildung.rss2'),
        ('Bauwesen', 'http://www.presseportal.de/rss/bauwesen.rss2'),
        ('Celebrities', 'http://www.presseportal.de/rss/celebrities.rss2'),
        ('Chemie', 'http://www.presseportal.de/rss/chemie.rss2'),
        ('Comuter', 'http://www.presseportal.de/rss/computer.rss2'),
        ('Energie', 'http://www.presseportal.de/rss/energie.rss2'),
        ('Fernsehn', 'http://www.presseportal.de/rss/fernsehn.rss2'),
        ('Fussball', 'http://www.presseportal.de/rss/fussball.rss2'),
        ('Gesundheit', 'http://www.presseportal.de/rss/gesundheit.rss2'),
        ('Handel', 'http://www.presseportal.de/rss/handel.rss2'),
        ('Immobilien', 'http://www.presseportal.de/rss/immobilien.rss2'),
        ('Kinder', 'http://www.presseportal.de/rss/kinder.rss2'),
        ('Lebensmittel', 'http://www.presseportal.de/rss/lebensmittel.rss2'),
        ('Livestyle', 'http://www.presseportal.de/rss/livestyle.rss2'),
        ('Logistik', 'http://www.presseportal.de/rss/logistik.rss2'),
        ('Maschinenbau', 'http://www.presseportal.de/rss/maschinenbau.rss2'),
        ('Medien', 'http://www.presseportal.de/rss/medien.rss2'),
        ('Motorsport', 'http://www.presseportal.de/rss/motorsport.rss2'),
        ('Ratgeber', 'http://www.presseportal.de/rss/ratgeber.rss2'),
        ('Recht', 'http://www.presseportal.de/rss/recht.rss2'),
        ('Soziales', 'http://www.presseportal.de/rss/soziales.rss2'),
        ('Telekommunikation', 'http://www.presseportal.de/rss/telekommunikation.rss2'),
        ('Touristik', 'http://www.presseportal.de/rss/touristik.rss2'),
        ('Umwelt', 'http://www.presseportal.de/rss/umwelt.rss2'),
        ('Unterhaltung', 'http://www.presseportal.de/rss/unterhaltung.rss2'),
        ('Versicherung', 'http://www.presseportal.de/rss/versicherung.rss2'),
        ('Wissenschaft', 'http://www.presseportal.de/rss/wissenschaft.rss2'),
        # Themen
        ('Polizei (gesamt)', 'http://www.presseportal.de/rss/polizei.rss2'),
        ('Bilder', 'http://www.presseportal.de/rss/98,306.rss2'),
        ('Video', 'http://www.presseportal.de/rss/videocast.rss2'),
        ('Audio', 'http://www.presseportal.de/rss/podcast.rss2'),
        ('Investor Relations', 'http://www.presseportal.de/rss/ir.rss2'),
        ('Presseschau', 'http://www.presseportal.de/rss/1043.rss2'),
        # Polizeipresse - Kommunal!
        ('Polizei BW', 'http://www.presseportal.de/rss/polizei/laender/1.rss2'),
        ('Polizei BY', 'http://www.presseportal.de/rss/polizei/laender/2.rss2'),
        ('Polizei BE', 'http://www.presseportal.de/rss/polizei/laender/3.rss2'),
        ('Polizei HB', 'http://www.presseportal.de/rss/polizei/laender/4.rss2'),
        ('Polizei HH', 'http://www.presseportal.de/rss/polizei/laender/5.rss2'),
        ('Polizei HE', 'http://www.presseportal.de/rss/polizei/laender/6.rss2'),
        ('Polizei MV', 'http://www.presseportal.de/rss/polizei/laender/7.rss2'),
        ('Polizei NI', 'http://www.presseportal.de/rss/polizei/laender/8.rss2'),
        ('Polizei NW', 'http://www.presseportal.de/rss/polizei/laender/9.rss2'),
        ('Polizei RP', 'http://www.presseportal.de/rss/polizei/laender/10.rss2'),
        ('Polizei SH', 'http://www.presseportal.de/rss/polizei/laender/11.rss2'),
        ('Polizei BUND', 'http://www.presseportal.de/rss/polizei/laender/12.rss2'),
        ('Polizei SL', 'http://www.presseportal.de/rss/polizei/laender/13.rss2'),
        ('Polizei SA', 'http://www.presseportal.de/rss/polizei/laender/14.rss2'),
        ('Polizei TH', 'http://www.presseportal.de/rss/polizei/laender/15.rss2'),
        ('Polizei ST', 'http://www.presseportal.de/rss/polizei/laender/16.rss2'),
    ]

    def print_version(self, url):
        # Separate the last part of the Url : http://www.presseportal.de/pm/70776/3120111
        #  tuple it in partitions (from the right) at the edges of
        #     the '/' symbol : ('http://www.presseportal.de/pm/70776','/','3120111')
        #  take the last of this tuples : '3120111'
        # and split this at the symbol '.' (if there) : ('3120111',)
        # from this take the first tuple. '3120111'
        side = (url.rpartition('/')[-1]).rsplit('.', 1)[0]
        # print('ConvertPrintURL from :  ' + url + '/n to ' + side)
        printurl = 'http://www.presseportal.de/print/' + side + '-print.html'
        return printurl
