HiveNetCore.utils.wget module

Download utility as an easy way to get file from the net

HiveNetCore.utils.wget.bar_adaptive(current, total, width=80)[源代码]
Return progress bar string for given values in one of three

styles depending on available width:

[.. ] downloaded / total downloaded / total [.. ]

if total value is unknown or <= 0, show bytes counter using two adaptive styles:

%s / unknown %s

if there is not enough space on the screen, do not display anything

returned string doesn’t include control characters like

used to

place cursor at the beginning of the line to erase previous content.

this function leaves one free character at the end of string to avoid automatic linefeed on Windows.

HiveNetCore.utils.wget.bar_thermometer(current, total, width=80)[源代码]
Return thermometer style progress bar string. total argument

can not be zero. The minimum size of bar returned is 3. Example:

[………. ]

Control and trailing symbols (

and spaces) are not included.

See bar_adaptive for more information.

HiveNetCore.utils.wget.callback_progress(blocks, block_size, total_size, bar_function)[源代码]

callback function for urlretrieve that is called when connection is created and when once for each block

draws adaptive progress bar in terminal/console

use sys.stdout.write() instead of “print,”, because it allows one more symbol at the line end without linefeed on Windows

参数
  • blocks – number of blocks transferred so far

  • block_size – in bytes

  • total_size – in bytes, can be -1 if server doesn’t return it

  • bar_function – another callback function to visualize progress

HiveNetCore.utils.wget.detect_filename(url=None, out=None, headers=None, default='download.wget')[源代码]

Return filename for saving file. If no filename is detected from output argument, url or headers, return default (download.wget)

HiveNetCore.utils.wget.download(url, out=None, bar=<function bar_adaptive>, headers=None, proxy=None)[源代码]

High level function, which downloads URL into tmp file in current directory and then renames it to filename autodetected from either URL or HTTP headers.

参数
  • bar – function to track download progress (visualize etc.)

  • out – output filename or directory

  • headers – 支持传入http头

  • proxy – 支持传入代理字典, 格式如下{‘http’: ‘127.0.0.1:80’}

返回

filename where URL is downloaded to

HiveNetCore.utils.wget.filename_fix_existing(filename)[源代码]

Expands name portion of filename with numeric ‘ (x)’ suffix to return filename that doesn’t exist already.

HiveNetCore.utils.wget.filename_from_headers(headers)[源代码]

Detect filename from Content-Disposition headers if present. http://greenbytes.de/tech/tc2231/

Param

headers as dict, list or string

返回

filename from content-disposition header or None

HiveNetCore.utils.wget.filename_from_url(url)[源代码]
返回

detected filename as unicode or None

HiveNetCore.utils.wget.get_console_width()[源代码]

Return width of available window area. Autodetection works for Windows and POSIX platforms. Returns 80 for others

Code from http://bitbucket.org/techtonik/python-pager

HiveNetCore.utils.wget.to_unicode(filename)[源代码]
返回

filename decoded from utf-8 to unicode

HiveNetCore.utils.wget.win32_unicode_console()[源代码]
HiveNetCore.utils.wget.win32_utf8_argv()[源代码]

Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode strings.

Versions 2.x of Python don’t support Unicode in sys.argv on Windows, with the underlying Windows API instead replacing multi-byte characters with ‘?’.