#!/usr/bin/env python2
# vim:fileencoding=utf-8

from __future__ import (
    unicode_literals,
    division,
    absolute_import,
    print_function
)
import re
from calibre.web.feeds.news import BasicNewsRecipe


class BloombergRecipe(BasicNewsRecipe):
    title = "Bloomberg"
    oldest_article = 2
    max_articles_per_feed = 50
    auto_cleanup = True
    use_embedded_content = False
    timefmt = ""
    feeds = [
        (
            "Markets",
            "https://www.bloomberg.com/markets"
        )
    ]

    def parse_index(self):
        root_url = "https://www.bloomberg.com"
        outfeeds = []
        for feed_title, feed_url in self.feeds:
            soup = (
                self.index_to_soup(feed_url)
            )
            soup_arts = (
                soup.findAll(
                    "a",
                    attrs={
                        "data-resource-type": "article"
                    },
                    href=True
                )
            )
            articles = []
            for tag in soup_arts:
                title = (
                    self.tag_to_string(
                        tag,
                        use_alt=False,
                        normalize_whitespace=True
                    )
                )
                if title == "":
                    continue
                href = tag["href"]
                url = "".join([root_url, href])
                datematch = re.search(r"/([0-9]{4}-[0-9]{2}-[0-9]{2})/", href)
                date = (
                    strftime("%Y-%m-%d") if
                    (datematch is None) else
                    datematch.groups()[0]
                )
                article = {
                    "title": title,
                    "url": url,
                    "date": date,
                    "description": title,
                    "content": ""
                }
                articles.append(article)
            outfeeds.append((
                feed_title,
                articles
            ))
        #print(outfeeds)
        return outfeeds
