#!/usr/bin/env  python2
__license__ = 'GPL v3'
__author__ = 'Lorenzo Vigentini (updated by rrrrrrrrrrrryan at gmail.com)'
__copyright__ = '2009, Lorenzo Vigentini <l.vigentini at gmail.com>'
description = 'News from Orange county - v1.02 (10, August 2014)'

'''
http://www.ocregister.com/
'''

from calibre.web.feeds.news import BasicNewsRecipe


class ocRegister(BasicNewsRecipe):
    author = 'Lorenzo Vigentini'
    description = 'News from the Orange county'

    cover_url = 'http://images.onset.freedom.com/ocregister/logo.gif'
    title = u'Orange County Register'
    publisher = 'Orange County Register Communication'
    category = 'News, finance, economy, politics'

    language = 'en'
    timefmt = '[%a, %d %b, %Y]'

    oldest_article = 1
    max_articles_per_feed = 25
    use_embedded_content = False
    recursion = 10

    # remove_javascript     = True
    no_stylesheets = True

    needs_subscription = "optional"

    use_javascript_to_login = True

    def javascript_login(self, browser, username, password):
        browser.visit('http://www.ocregister.com/sections/login')
        form = browser.select_form(nr=1)  # Select the second form on the page
        form['username'] = username
        form['password_temp'] = password
        # Submit the form and wait at most two minutes for loading to complete
        browser.submit(timeout=120)

    def print_version(self, url):
        printUrl = 'http://www.ocregister.com/common/printer/view.php?db=ocregister&id='
        segments = url.split('/')
        subSegments = (segments[4]).split('.')
        subSubSegments = (subSegments[0]).split('-')
        myArticle = (subSubSegments[1])
        myURL = printUrl + myArticle
        return myURL

    keep_only_tags = [
        dict(name='div', attrs={'id': 'ArticleContentWrap'})
    ]

    remove_tags = [
        dict(name='div', attrs={'class': 'hideForPrint'}),
        dict(name='div', attrs={'id': 'ContentFooter'})
    ]

    feeds = [
        (u'News', u'http://www.ocregister.com/common/rss/rss.php?catID=18800'),
        (u'Top Stories',
         u'http://www.ocregister.com/common/rss/rss.php?catID=23541'),
        (u'Business', u'http://www.ocregister.com/common/rss/rss.php?catID=18909'),
        (u'Cars', u'http://www.ocregister.com/common/rss/rss.php?catID=20128'),
        (u'Entertainment',
         u'http://www.ocregister.com/common/rss/rss.php?catID=18926'),
        (u'Home', u'http://www.ocregister.com/common/rss/rss.php?catID=19142'),
        (u'Life', u'http://www.ocregister.com/common/rss/rss.php?catID=18936'),
        (u'Opinion', u'http://www.ocregister.com/common/rss/rss.php?catID=18963'),
        (u'Sports', u'http://www.ocregister.com/common/rss/rss.php?catID=18901'),
        (u'Travel', u'http://www.ocregister.com/common/rss/rss.php?catID=18959')
    ]
