def test_givenRssWithVodcastsWhenDownloadingThenOnlyNewFilesWillBeDownloaded(self):
        entries = self.rss_feed.entries
        vodcast = parse_video_item(entries[0])
        
        vodcast_downloader = VodcastDownloader()

        self.assertTrue(vodcast_downloader.should_be_downloaded(vodcast, as_local_datetime(datetime(2010, 10, 25, 11, 53, 49))), vodcast.updated)
        self.assertFalse(vodcast_downloader.should_be_downloaded(vodcast, as_local_datetime(datetime(2010, 10, 27, 11, 53, 49))), vodcast.updated)
    def test_reference_data_in_local_time(self):
        entries = self.rss_feed.entries
        vodcast = parse_video_item(entries[0])
        
        vodcast_downloader = VodcastDownloader()

        self.assertEqual(vodcast.updated, datetime(2010, 10, 26, 9, 53, 49))
        berlin = pytz.timezone("Europe/Berlin")
        self.assertTrue(vodcast_downloader.should_be_downloaded(vodcast, berlin.localize(datetime(2010, 10, 26, 10, 53, 49))), vodcast.updated)
    def test_should_dete_file_during_exception(self):
        testfile = tempfile.mktemp()

        vodcast_downloader = VodcastDownloader()
        vodcast = Vodcast(ItemMock('Extra 3 three', (2010, 10, 28, 9, 53, 49), 'http://media.ndr.de/download/podcasts/extradrei196/TV-20101028-2220-5801.h264.mp4'))
        vodcast.local_filename = testfile
        
        def read_throwing_exception(url, filename, hoock):
            fd = open(filename, 'w')
            fd.close()
            self.assertFilePresent(None, filename)
            raise KeyboardInterrupt
        
        vodcast_downloader.url_retriever = read_throwing_exception
        
        self.assertRaisesRegexp(Exception, 'User interrupted' ,vodcast_downloader.download, vodcast)
        self.assertFileNotPresent(None, testfile)
    def test_givenFeedWithSomeVodcastsWhenReferenceDateGivenThenAllNewerFeedsWillBeDownloaded(self):
        feed_content = r'''<?xml version='1.0' encoding='UTF-8'?>
                            <rss version='2.0'>
                            <channel>
                            <title>Extra3</title>
                            <item>
                                <title>Extra 3 one</title>
                                <description>Tatort Taliban; Jasmin trifft: Frau zu Guttenberg</description>
                                <pubDate>Tue, 26 Oct 2010 11:53:49 +0200</pubDate>
                                <enclosure url='http://media.ndr.de/download/podcasts/extradrei196/TV-20101063-2220-5801.h264.mp4' type='video/mp4' />
                                <guid isPermaLink='false'>TV-20101026-2220-5801-V</guid>
                                <link>http://media.ndr.de/download/podcasts/extradrei196/TV-20101026-2220-5801.h264.mp4</link>
                            </item>
                            <item>
                                <title>Extra 3 two</title>
                                <description>Tatort Taliban; Jasmin trifft: Frau zu Guttenberg</description>
                                <pubDate>Tue, 27 Oct 2010 11:53:49 +0200</pubDate>
                                <enclosure url='http://media.ndr.de/download/podcasts/extradrei196/TV-20101027-2220-5801.h264.mp4' type='video/mp4' />
                                <guid isPermaLink='false'>TV-20101027-2220-5801-V</guid>
                                <link>http://media.ndr.de/download/podcasts/extradrei196/TV-20101027-2220-5801.h264.mp4</link>
                            </item>
                            <item>
                                <title>Extra 3 three</title>
                                <description>Tatort Taliban; Jasmin trifft: Frau zu Guttenberg</description>
                                <pubDate>Tue, 28 Oct 2010 11:53:49 +0200</pubDate>
                                <enclosure url='http://media.ndr.de/download/podcasts/extradrei196/TV-20101028-2220-5801.h264.mp4' type='video/mp4' />
                                <guid isPermaLink='false'>TV-20101028-2220-5801-V</guid>
                                <link>http://media.ndr.de/download/podcasts/extradrei196/TV-20101028-2220-5801.h264.mp4</link>
                            </item>
                            </channel></rss>'''

        vodcast_download_manager = VodcastDownloadManager(feed_content, None)
        
        vocast_collector = []
        downloader = VodcastDownloader(None)
        downloader.download = lambda vodcast: vocast_collector.append(vodcast)
        vodcast_download_manager.downloader = downloader

        vodcast_download_manager.download_all_newer(as_local_datetime(datetime(2010, 10, 27, 0, 0, 0)))
        self.assertNotIn(Vodcast(ItemMock('Extra 3 one', (2010, 10, 26, 9, 53, 49), 'http://media.ndr.de/download/podcasts/extradrei196/TV-20101026-2220-5801.h264.mp4'))
                      , vocast_collector)
        self.assertIn(Vodcast(ItemMock('Extra 3 two', (2010, 10, 27, 9, 53, 49), 'http://media.ndr.de/download/podcasts/extradrei196/TV-20101027-2220-5801.h264.mp4'))
                      , vocast_collector)
        self.assertIn(Vodcast(ItemMock('Extra 3 three', (2010, 10, 28, 9, 53, 49), 'http://media.ndr.de/download/podcasts/extradrei196/TV-20101028-2220-5801.h264.mp4'))
                      , vocast_collector)
    def test_givenUrlPointingToLocalResourceWhenDownloadedThenContentWillBeStored(self):
        entries = self.rss_feed.entries
        vodcast = parse_video_item(entries[0])
        
        tempdir = tempfile.mkdtemp()

        vodcast_downloader = VodcastDownloader(tempdir)
        
        testfile = self.__create_tempfile('vodcast content')
        
        vodcast.url = 'file://' + testfile.name

        vodcast_downloader.download(vodcast)
        
        self.assertFilePresent(tempdir, vodcast.local_filename, remove_after_check = True)
        
        os.remove(testfile.name)
        os.rmdir(tempdir)