Ejemplo n.º 1
0
def test_calculate_time_based_score():
    podcast = Podcast("name", "feed", DummyEpisodeStorage())
    episode = Episode(podcast, 0, "a", "b", "c", "2016-03-03 10:00:00", False)
    score = episode.score
    episode.published = "2016-03-04 10:00:00"
    new_score = episode.score
    assert new_score - score == 1
    score = episode.score
    episode.published = "2016-04-04 11:00:00"
    new_score = episode.score
    assert new_score - score == 31
Ejemplo n.º 2
0
def test_calculate_time_based_score():
    podcast = Podcast('name', 'feed', DummyEpisodeStorage())
    episode = Episode(podcast, 0, "a", "b", "c", '2016-03-03 10:00:00', False)
    score = episode.score
    episode.published = '2016-03-04 10:00:00'
    new_score = episode.score
    assert new_score - score == 1
    score = episode.score
    episode.published = '2016-04-04 11:00:00'
    new_score = episode.score
    assert new_score - score == 31
Ejemplo n.º 3
0
    def handle(self, podcast_name=None):
        file_storage = FileStorage()

        if podcast_name is None:
            podcasts = file_storage.get_podcasts().values()
        else:
            podcasts = [file_storage.get_podcasts()[podcast_name]]

        for podcast in podcasts:
            print(podcast.name)
            feed = feedparser.parse(podcast.feed)
            episodes = []
            for entry in feed.entries:
                enclosures = getattr(entry, 'enclosures', [])
                if len(enclosures) == 0:
                    continue
                # Use an empty string when no link is available
                link = getattr(entry, 'link', '')
                media_href = entry.enclosures[0].href
                episodes.append(
                    Episode(podcast, entry.guid, entry.title, link, media_href,
                            entry.published_parsed, False))
            for episode in episodes:
                if episode not in podcast.episodes:
                    podcast.episodes.append(episode)
            FileEpisodeStorage().save_episodes(podcast)
Ejemplo n.º 4
0
def test_score_podcast_priority():
    ep_storage = DummyEpisodeStorage()

    p1 = Podcast('name', 'feed', ep_storage)
    e1 = Episode(p1, 1, 'a', 'b', 'c', '2016-03-15 00:00:00', False)
    e2 = Episode(p1, 2, 'a', 'b', 'c', '2016-03-30 00:00:00', False)
    assert p1.priority == 5

    p2 = Podcast('name', 'feed', ep_storage, 6)
    e3 = Episode(p2, 3, 'a', 'b', 'c', '2016-03-20 00:00:00', False)

    assert p2.score > p1.score
    # The second podcast has higher rank
    assert e2.published > e3.published > e1.published
    # Episode 3 was published between 2 and 1
    assert e3.score > e2.score > e1.score
Ejemplo n.º 5
0
def test_podcast_and_episode():
    episode_storage = DummyEpisodeStorage()
    name = 'name'
    feed = 'http://example.com/feed.xml'

    podcast = Podcast(name, feed, episode_storage)
    assert podcast.name == name
    assert podcast.feed == feed
    assert podcast._episode_storage is episode_storage

    assert hasattr(podcast, '_episode_storage')
    assert not hasattr(podcast, '_episodes')
    episodes = podcast.episodes
    assert not hasattr(podcast, '_episode_storage')
    assert hasattr(podcast, '_episodes')

    assert episodes[0].guid == 1
    assert episodes[0].title == 2
    assert episodes[0].link == 3
    assert episodes[0].media_href == 4
    assert episodes[0].published == struct_time((
        2012, 5, 6, 7, 8, 9, 6, 127, -1))
    assert episodes[0].downloaded == True
    assert episodes[1].guid == 6
    assert episodes[1].title == 7
    assert episodes[1].link == 8
    assert episodes[1].media_href == 9
    assert episodes[1].published == struct_time((
        2012, 6, 7, 7, 8, 9, 3, 159, -1))
    assert episodes[1].downloaded == False

    assert not podcast.modified
    assert not episodes.modified
    assert not episodes[0].modified
    assert not episodes[1].modified
    episodes[0].guid = 11
    assert not podcast.modified
    assert episodes.modified
    assert episodes[0].modified
    assert not episodes[1].modified

    episodes.modified = False
    episodes[0].modified = False

    assert not podcast.modified
    assert not episodes.modified
    assert not episodes[0].modified
    assert not episodes[1].modified
    assert len(episodes) == 2
    episodes.append(Episode(podcast, 11, 12, 13, 14, '2012-12-12 12:12:12', True))
    assert len(episodes) == 3
    assert not podcast.modified
    assert episodes.modified
    assert not episodes[0].modified
    assert not episodes[1].modified
    assert not episodes[2].modified
Ejemplo n.º 6
0
 def get_episodes(self, podcast):
     path = self._get_episode_history_file_path(podcast)
     if not os.path.exists(path):
         return []
     with open(path, newline='') as f:
         rows = list(csv.reader(f))
         has_header = len(rows) > 0 and rows[0] == Episode.columns
         if has_header:
             rows = rows[1:]
         episodes = reversed([Episode.from_tuple(podcast, e) for e in rows])
         return episodes
Ejemplo n.º 7
0
 def get_episodes(self, podcast):
     path = self._get_episode_history_file_path(podcast)
     if not os.path.exists(path):
         return []
     with open(path, newline='') as f:
         rows = list(csv.reader(f))
         has_header = len(rows) > 0 and rows[0] == Episode.columns
         if has_header:
             rows = rows[1:]
         episodes = reversed([Episode.from_tuple(podcast, e) for e in rows])
         return episodes
Ejemplo n.º 8
0
def test_save_podcast(tmpdir, monkeypatch):
    file_storage = FileStorage()
    monkeypatch.setattr('riley.storage.user_data_dir',
                        lambda x, y: tmpdir.strpath)

    podcast = Podcast('abc', 'def', FileEpisodeStorage())
    podcast.episodes.append(
        Episode(podcast, 1, 2, 3, 4,
                strptime('2015-11-12 01:02:03', '%Y-%m-%d %H:%M:%S'), True))

    file_storage.save_podcast(podcast)

    config_path = tmpdir.join('config.yml')
    data = yaml.load(config_path.read())
    assert len(data) == 2
    assert data['podcasts'] == {'abc': {'feed': 'def', 'priority': 5}}
    history_path = tmpdir.join('abc_history.csv')
    assert history_path.read() == \
        'guid,title,link,media_href,published,downloaded\n' \
        '1,2,3,4,2015-11-12 01:02:03,True\n'
Ejemplo n.º 9
0
 def get_episodes(self, podcast):
     return [
         Episode(podcast, 1, 2, 3, 4, '2012-05-06 07:08:09', True),
         Episode(podcast, 6, 7, 8, 9, '2012-06-07 07:08:09', False),
     ]
Ejemplo n.º 10
0
def test_score_already_downloaded_episode():
    podcast = Podcast('name', 'feed', DummyEpisodeStorage())
    episode = Episode(podcast, 0, "a", "b", "c", '2016-03-03 10:00:00', True)
    score = episode.score
    assert score == 0