예제 #1
0
 def test_new_dir_after_file(self):
     if sys.version_info < (2,7):  # we don't even get skipIf in Python 2.6!
         return
     newdir = os.path.join(self.filename, 'newdir')
     with self.assertRaisesRegexp(OSError, 'Not a directory'):
         ensure_dir(newdir)
     self.assertNotExists(newdir)
예제 #2
0
 def test_new_dir_after_file(self):
     if sys.version_info < (2,7):  # we don't even get skipIf in Python 2.6!
         return
     newdir = os.path.join(self.filename, 'newdir')
     with self.assertRaisesRegexp(OSError, 'Not a directory'):
         ensure_dir(newdir)
     self.assertNotExists(newdir)
예제 #3
0
def download_video(youtube_id,
                   download_path="../content/",
                   download_url=OUTSIDE_DOWNLOAD_URL,
                   format="mp4",
                   callback=None):
    """Downloads the video file to disk (note: this does NOT invalidate any of the cached html files in KA Lite)"""

    ensure_dir(download_path)

    url, thumb_url = get_outside_video_urls(youtube_id,
                                            download_url=download_url,
                                            format=format)
    video_filename = "%(id)s.%(format)s" % {"id": youtube_id, "format": format}
    filepath = os.path.join(download_path, video_filename)

    thumb_filename = "%(id)s.png" % {"id": youtube_id}
    thumb_filepath = os.path.join(download_path, thumb_filename)

    try:
        response = download_file(
            url, filepath, callback_percent_proxy(callback, end_percent=95))
        if (not os.path.isfile(filepath)
                or "content-length" not in response.headers
                or not os.path.getsize(filepath) == int(
                    response.headers['content-length'])):
            raise URLNotFound("Video was not found, tried: {}".format(url))

        response = download_file(
            thumb_url, thumb_filepath,
            callback_percent_proxy(callback, start_percent=95,
                                   end_percent=100))
        if (not os.path.isfile(thumb_filepath)
                or "content-length" not in response.headers
                or not os.path.getsize(thumb_filepath) == int(
                    response.headers['content-length'])):
            raise URLNotFound(
                "Thumbnail was not found, tried: {}".format(thumb_url))

    except DownloadCancelled:
        delete_downloaded_files(youtube_id, download_path)
        raise

    except (socket.timeout, IOError) as e:
        logging.exception(e)
        logging.info("Timeout -- Network UnReachable")
        delete_downloaded_files(youtube_id, download_path)
        raise

    except Exception as e:
        logging.exception(e)
        delete_downloaded_files(youtube_id, download_path)
        raise
예제 #4
0
파일: videos.py 프로젝트: aterry06/ka-lite
def download_video(youtube_id, download_path="../content/", download_url=OUTSIDE_DOWNLOAD_URL, format="mp4", callback=None):
    """Downloads the video file to disk (note: this does NOT invalidate any of the cached html files in KA Lite)"""

    ensure_dir(download_path)

    url, thumb_url = get_outside_video_urls(youtube_id, download_url=download_url, format=format)
    video_filename = "%(id)s.%(format)s" % {"id": youtube_id, "format": format}
    filepath = os.path.join(download_path, video_filename)

    thumb_filename = "%(id)s.png" % {"id": youtube_id}
    thumb_filepath = os.path.join(download_path, thumb_filename)

    try:
        response = download_file(url, filepath, callback_percent_proxy(callback, end_percent=95))
        if (
                not os.path.isfile(filepath) or
                "content-length" not in response.headers or
                not len(open(filepath, "rb").read()) == int(response.headers['content-length'])):
            raise URLNotFound("Video was not found, tried: {}".format(url))

        response = download_file(thumb_url, thumb_filepath, callback_percent_proxy(callback, start_percent=95, end_percent=100))
        if (
                not os.path.isfile(thumb_filepath) or
                "content-length" not in response.headers or
                not len(open(thumb_filepath, "rb").read()) == int(response.headers['content-length'])):
            raise URLNotFound("Thumbnail was not found, tried: {}".format(thumb_url))

    except DownloadCancelled:
        delete_downloaded_files(youtube_id, download_path)
        raise

    except (socket.timeout, IOError) as e:
        logging.exception(e)
        logging.info("Timeout -- Network UnReachable")
        delete_downloaded_files(youtube_id, download_path)
        raise

    except Exception as e:
        logging.exception(e)
        delete_downloaded_files(youtube_id, download_path)
        raise
예제 #5
0
def download_video(
    youtube_id, download_path="../content/", download_url=OUTSIDE_DOWNLOAD_URL, format="mp4", callback=None
):
    """Downloads the video file to disk (note: this does NOT invalidate any of the cached html files in KA Lite)"""

    ensure_dir(download_path)

    url, thumb_url = get_outside_video_urls(youtube_id, download_url=download_url, format=format)
    video_filename = "%(id)s.%(format)s" % {"id": youtube_id, "format": format}
    filepath = os.path.join(download_path, video_filename)

    thumb_filename = "%(id)s.png" % {"id": youtube_id}
    thumb_filepath = os.path.join(download_path, thumb_filename)

    try:
        path, response = download_file(url, filepath, callback_percent_proxy(callback, end_percent=95))
        if not response.type.startswith("video"):
            raise URLNotFound("Video was not found!")

        path, response = download_file(
            thumb_url, thumb_filepath, callback_percent_proxy(callback, start_percent=95, end_percent=100)
        )
        if not response.type.startswith("image"):
            raise URLNotFound("Thumbnail was not found!")

    except DownloadCancelled:
        delete_downloaded_files(youtube_id, download_path)
        raise

    except (socket.timeout, IOError) as e:
        logging.exception(e)
        logging.info("Timeout -- Network UnReachable")
        delete_downloaded_files(youtube_id, download_path)
        raise

    except Exception as e:
        logging.exception(e)
        delete_downloaded_files(youtube_id, download_path)
        raise
예제 #6
0
 def test_new_dir_after_file(self):
     newdir = os.path.join(self.filename, 'newdir')
     with self.assertRaisesRegexp(OSError, 'Not a directory'):
         ensure_dir(newdir)
     self.assertNotExists(newdir)
예제 #7
0
 def test_file(self):
     with self.assertRaisesRegexp(OSError, 'Not a directory'):
         ensure_dir(self.filename)
예제 #8
0
 def test_new_dotted_dir(self):
     newdir = os.path.join(self.dirname, 'new.dir')
     self.assertNotExists(newdir)
     ensure_dir(newdir)
     self.assertDirExists(newdir)
예제 #9
0
 def test_dir(self):
     ensure_dir(self.dirname)
     self.assertDirExists(self.dirname)
예제 #10
0
 def test_file(self):
     if sys.version_info < (2,7):  # we don't even get skipIf in Python 2.6!
         return
     with self.assertRaisesRegexp(OSError, 'Not a directory'):
         ensure_dir(self.filename)
예제 #11
0
 def test_file(self):
     if sys.version_info < (2,
                            7):  # we don't even get skipIf in Python 2.6!
         return
     with self.assertRaisesRegexp(OSError, 'Not a directory'):
         ensure_dir(self.filename)
예제 #12
0
 def test_new_dotted_dir(self):
     newdir = os.path.join(self.dirname, 'new.dir')
     self.assertNotExists(newdir)
     ensure_dir(newdir)
     self.assertDirExists(newdir)
예제 #13
0
 def test_dir(self):
     ensure_dir(self.dirname)
     self.assertDirExists(self.dirname)