Update pywwwget.py
[PyWWW-Get.git] / pywwwget-dl.py
blob6beb2c205b9e75fd214b377a58a28ced7e0c4717
1 #!/usr/bin/env python
3 '''
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the Revised BSD License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 Revised BSD License for more details.
12 Copyright 2016 Cool Dude 2k - http://idb.berlios.de/
13 Copyright 2016 Game Maker 2k - http://intdb.sourceforge.net/
14 Copyright 2016 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
16 $FileInfo: pywwwget-dl.py - Last Update: 6/17/2016 Ver. 0.4.7 RC 1 - Author: cooldude2k $
17 '''
19 from __future__ import division, absolute_import, print_function;
20 import re, os, sys, pywwwget, argparse;
21 import logging as log;
23 __project__ = pywwwget.__project__;
24 __program_name__ = pywwwget.__program_name__;
25 __project_url__ = pywwwget.__project_url__;
26 __version_info__ = pywwwget.__version_info__;
27 __version_date_info__ = pywwwget.__version_date_info__;
28 __version_date__ = pywwwget.__version_date__;
29 __version_date_plusrc__ = pywwwget.__version_date_plusrc__
30 __version__ = pywwwget.__version__;
31 __version_date_plusrc__ = pywwwget.__version_date_plusrc__;
33 geturls_cj = pywwwget.geturls_cj;
34 geturls_ua = pywwwget.geturls_ua;
35 geturls_ua_firefox_windows7 = pywwwget.geturls_ua_firefox_windows7;
36 geturls_ua_seamonkey_windows7 = pywwwget.geturls_ua_seamonkey_windows7;
37 geturls_ua_chrome_windows7 = pywwwget.geturls_ua_chrome_windows7;
38 geturls_ua_chromium_windows7 = pywwwget.geturls_ua_chromium_windows7;
39 geturls_ua_palemoon_windows7 = pywwwget.geturls_ua_palemoon_windows7;
40 geturls_ua_opera_windows7 = pywwwget.geturls_ua_opera_windows7;
41 geturls_ua_vivaldi_windows7 = pywwwget.geturls_ua_chromium_windows7;
42 geturls_ua_internet_explorer_windows7 = pywwwget.geturls_ua_internet_explorer_windows7;
43 geturls_ua_microsoft_edge_windows7 = pywwwget.geturls_ua_microsoft_edge_windows7;
44 geturls_ua_pywwwget_python = pywwwget.geturls_ua_pywwwget_python;
45 geturls_ua_pywwwget_python_alt = pywwwget.geturls_ua_pywwwget_python_alt;
46 geturls_ua_googlebot_google = pywwwget.geturls_ua_googlebot_google;
47 geturls_ua_googlebot_google_old = pywwwget.geturls_ua_googlebot_google_old;
48 geturls_headers = pywwwget.geturls_headers;
49 geturls_headers_firefox_windows7 = pywwwget.geturls_headers_firefox_windows7;
50 geturls_headers_seamonkey_windows7 = pywwwget.geturls_headers_seamonkey_windows7;
51 geturls_headers_chrome_windows7 = pywwwget.geturls_headers_chrome_windows7;
52 geturls_headers_chromium_windows7 = pywwwget.geturls_headers_chromium_windows7;
53 geturls_headers_palemoon_windows7 = pywwwget.geturls_headers_palemoon_windows7;
54 geturls_headers_opera_windows7 = pywwwget.geturls_headers_opera_windows7;
55 geturls_headers_vivaldi_windows7 = pywwwget.geturls_headers_vivaldi_windows7;
56 geturls_headers_internet_explorer_windows7 = pywwwget.geturls_headers_internet_explorer_windows7;
57 geturls_headers_microsoft_edge_windows7 = pywwwget.geturls_headers_microsoft_edge_windows7;
58 geturls_headers_pywwwget_python = pywwwget.geturls_headers_pywwwget_python;
59 geturls_headers_pywwwget_python_alt = pywwwget.geturls_headers_pywwwget_python_alt;
60 geturls_headers_googlebot_google = pywwwget.geturls_headers_googlebot_google;
61 geturls_headers_googlebot_google_old = pywwwget.geturls_headers_googlebot_google_old;
62 geturls_download_sleep = pywwwget.geturls_download_sleep;
64 parser = argparse.ArgumentParser(description="Python libary/module to download files.", conflict_handler="resolve", add_help=True);
65 parser.add_argument("url", help="motherless url");
66 parser.add_argument("-V", "--version", action="version", version=__program_name__+" "+__version__);
67 parser.add_argument("-u", "--update", action="store_true", help="update this program to latest version. Make sure that you have sufficient permissions (run with sudo if needed)");
68 parser.add_argument("-d", "--dump-user-agent", action="store_true", help="display the current browser identification");
69 parser.add_argument("-u", "--user-agent", default=geturls_ua_firefox_windows7, help="specify a custom user agent");
70 parser.add_argument("-r", "--referer", default="https://www.google.com/", help="specify a custom referer, use if the video access");
71 parser.add_argument("-O", "--output-document", default="-", help="specify a file name for output");
72 parser.add_argument("-o", "--output-directory", default=os.path.realpath(os.getcwd()), help="specify a directory to output file to");
73 parser.add_argument("-l", "--use-httplib", default="urllib", help="select library to download file can be urllib or requests or mechanize");
74 parser.add_argument("-b", "--set-buffersize", default=524288, type=int, help="set how big buffersize is in bytes. how much it will download");
75 parser.add_argument("-v", "--verbose", action="store_true", help="print various debugging information");
76 getargs = parser.parse_args();
78 if(not pywwwget.check_httplib_support(getargs.use_httplib)):
79 getargs.use_httplib = "urllib";
81 getargs_cj = geturls_cj;
82 getargs_headers = {'Referer': getargs.referer, 'User-Agent': getargs.user_agent, 'Accept-Encoding': "gzip, deflate", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
84 getargs.output_directory = os.path.realpath(getargs.output_directory);
86 if(getargs.verbose==True):
87 log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG);
89 if(getargs.dump_user_agent==True):
90 print(getargs.user_agent);
91 sys.exit();
93 if(getargs.output_document=="-"):
94 if(sys.version[0]=="2"):
95 precontstr = pywwwget.download_from_url_to_file(getargs.url, getargs_headers, geturls_cj, getargs.use_httplib, buffersize=[getargs.set_buffersize, getargs.set_buffersize], outfile=getargs.output_document, outpath=os.getcwd());
96 print(precontstr['Content']);
97 if(sys.version[0]>="3"):
98 precontstr = pywwwget.download_from_url_to_file(getargs.url, getargs_headers, geturls_cj, getargs.use_httplib, buffersize=[getargs.set_buffersize, getargs.set_buffersize], outfile=getargs.output_document, outpath=os.getcwd());
99 print(precontstr['Content'].decode('ascii', 'replace'));
101 if(getargs.output_document!="-"):
102 pywwwget.download_from_url_to_file(getargs.url, getargs_headers, geturls_cj, getargs.use_httplib, buffersize=[getargs.set_buffersize, getargs.set_buffersize], outfile=getargs.output_document, outpath=getargs.output_directory);