def download_to_file(url, file, name, download_queue, display=None): """Downloads a URL to a local file. Args: url: the source url file: the destination path name: the user-friendly name of the content download_queue: the download_queue overseeing this download display: (optional) the display to write status updates to """ chunk_size = 1024 chuck_size_label = "KB" response = Net.Get(url, stream=True) handle = open(file, "wb") downloaded = 0 for chunk in response.iter_content(chunk_size=chunk_size): if display is not None: status_str = "Downloading \"%s\": %d%s" % ( name, downloaded / chunk_size, chuck_size_label) if download_queue.length > 1: status_str += " (+%d downloads in queue)" % \ download_queue.length - 1 display.change_status(status_str) if chunk: handle.write(chunk) downloaded += len(chunk) if display is not None: display.change_status("Episode successfully downloaded.") download_queue.next()
def _download_feed(self): """Parses the feed at the provided url or file into _tree. This method checks whether the url is valid and that there is a parse-able XML document at the url, but it does not check that the document is an RSS feed, nor whether the feed has all necessary tags. Raises: FeedParseError: unable to parse text as an XML document FeedDownloadError: (only when retrieving feed using url) did not receive an acceptable status code, or an exception occurred when attempting to download the page FeedLoadError: (only when retrieving feed using file) a feed could not be found at the file, or an exception occurred when attempting to load the file """ if self._url is not None: # handle feed from url try: response = Net.Get(self._url) if response.status_code == 200: try: self._tree = ElementTree.fromstring(response.text) except ElementTree.ParseError: raise FeedParseError( "Unable to parse text as an XML document") else: raise FeedDownloadError( "Did not receive an acceptable status code while" " downloading the page. Expected 200, got: " + str(response.status_code)) except requests.exceptions.RequestException: raise FeedDownloadError( "An exception occurred when attempting to download the" " page") elif self._file is not None: # handle feed from file file = None try: file = open(self._file) text = file.read() try: self._tree = ElementTree.fromstring(text) except ElementTree.ParseError: raise FeedParseError( "Unable to parse text as an XML document") except IOError: raise FeedLoadError( "An exception occurred when attempting to load the file") finally: if file is not None: file.close()
def test_net_get_uses_args(get): arg1 = 'arg1' arg2 = 'arg2' kwarg1 = 'kwarg1' kwarg2 = 'kwarg2' kwarg3 = 'kwarg3' Net.Get(arg1, arg2, kwarg1=kwarg1, kwarg2=kwarg2, kwarg3=kwarg3) args, kwargs = get.call_args assert 'arg1' in args assert 'arg2' in args assert 'kwarg1' in kwargs assert 'kwarg2' in kwargs assert 'kwarg3' in kwargs
def test_net_get_uses_args(get): arg1 = "arg1" arg2 = "arg2" kwarg1 = "kwarg1" kwarg2 = "kwarg2" kwarg3 = "kwarg3" Net.Get(arg1, arg2, kwarg1=kwarg1, kwarg2=kwarg2, kwarg3=kwarg3) args, kwargs = get.call_args assert "arg1" in args assert "arg2" in args assert "kwarg1" in kwargs assert "kwarg2" in kwargs assert "kwarg3" in kwargs
def download_to_file(url, file, name, download_queue, display=None): """Downloads a URL to a local file. :param url: the source url :param file the destination path :param name the user-friendly name of the content :param download_queue the download_queue overseeing this download :param display (optional) the display to write status updates to """ chunk_size = 1024 chuck_size_label = "KB" try: response = Net.Get(url, stream=True) except requests.exceptions.RequestException as e: if display is not None: display.change_status("RequestException: %s" % str(e)) download_queue.next() return else: handle = open(file, "wb") downloaded = 0 for chunk in response.iter_content(chunk_size=chunk_size): if display is not None: status_str = 'Downloading "%s": %d%s' % ( name, downloaded / chunk_size, chuck_size_label) if download_queue.length > 1: status_str += " (+%d downloads in queue)" % ( download_queue.length - 1) display.change_status(status_str) if chunk: handle.write(chunk) downloaded += len(chunk) if display is not None: display.change_status("Episode successfully downloaded.") display.menus_valid = False download_queue.next()
def test_net_get_empty(get): Net.Get() assert get.called