import logging

import feedparser
import requests

from .. import read_list

log = logging.getLogger(__name__)


class VideoFeed:
    def __init__(self, known_path, url):
        self.url = url
        self.read_list = read_list.ReadList(known_path, url)

    def is_new(self, item):
        return self.read_list.is_new(item.id)

    def fetch(self):
        try:
            response = requests.get(self.url)
            response.raise_for_status()
            feed = feedparser.parse(response.content)
            items = feed.entries
            if len(items) == 0:
                raise Exception("Empty feed. Is site down?")
            new_items = list(filter(self.is_new, items))
            log.info("Feed contains %d items, %d are new." % (len(items), len(new_items)))
            # iterate first, then save new read list
            for i in new_items:
                yield i
            self.read_list.save()
        except Exception:
            log.exception("Unexpected error with %s", self.url, exc_info=True)

    def fetch_video_codes(self):
        raise NotImplementedError

    def append_to_queue(self, queue_path):
        codes = self.fetch_video_codes()
        links = ["\nhttps://www.youtube.com/watch?v=" + v for v in codes]
        f = open(queue_path, "a")
        f.writelines(links)
        f.close()
