"""抓Youtube頻道RSS feed
Youtube沒有etag support，只能定時fetch
"""
import datetime
import json
import logging
import time
from typing import List, Optional

import feedparser
import elasticsearch
from hydra import utils
from omegaconf import DictConfig

from ..store import es
from .rss import RssScraper, NoRssEntries


log = logging.getLogger(__name__)


class YoutubeRssScraper(RssScraper):
    def __init__(self, cfg: DictConfig, proxies: Optional[List[str]] = None):
        super().__init__(cfg, proxies)
        self.entry = cfg.scraper.youtube_rss.entry
        self.force_fetch = cfg.scraper.youtube_rss.force_fetch
        self.fetch_rss_every_n_seconds = cfg.scraper.youtube_rss.fetch_rss_every_n_seconds

    @classmethod
    def parse(cls, from_url: str, resolved_url: str, http_status: int, html: str,
              rss: es.Rss = None, fetch_rss_every_n_seconds: int = None, debug: bool = False) -> List[es.Page]:
        feed = feedparser.parse(html)
        if not debug:
            if len(feed['entries']) == 0:
                raise NoRssEntries(
                    "No entries found: {}".format(str(resolved_url)))
            rss.n_retries = 0
            rss.fetched_at = datetime.datetime.now()
            rss.save()
            log.debug("儲存rss-entry")

        pages = []
        # 處理author（爲author建立page）
        f = feed['feed']
        from_url = f['link']
        try:
            # 已經存在的話就更新
            p = es.Page.get(id=from_url)
            p.entry_meta = json.dumps(f)
            p.entry_published_at = datetime.datetime.fromtimestamp(
                time.mktime(f['published_parsed']))
            p.save()
            log.debug("更新page-entry:author部分")
        except elasticsearch.NotFoundError:
            p = es.Page(
                from_url=from_url,
                entry_meta=json.dumps(f),
                entry_published_at=datetime.datetime.fromtimestamp(
                    time.mktime(f['published_parsed'])),)
            p.save()
            log.debug("新增page-entry:author部分")
        pages.append(p)

        # 處理videos
        log.debug("處理videos")
        for e in feed["entries"]:
            from_url = e["link"]
            try:
                # 若已經有了就不再建立
                p = es.Page.get(id=from_url)
                log.debug("若已經有了就不再建立")
            except elasticsearch.NotFoundError:
                p = es.Page(
                    from_url=from_url,
                    entry_meta=json.dumps(e),
                    entry_published_at=datetime.datetime.fromtimestamp(
                        time.mktime(e['published_parsed'])) if "published_parsed" in e else None,)
                p.save()
                log.debug("新增page-entry:video部分")
            pages.append(p)
        return pages
