Add files via upload
[PyMotherless.git] / pymotherless-dl.py
blobbf88c4f043021e234c5b9e077691ab28b2d38c1e
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: pymotherless-dl.py - Last Update: 6/16/2016 Ver. 0.4.7 RC 1 - Author: cooldude2k $
17 '''
19 from __future__ import division, absolute_import, print_function;
20 import re, os, sys, pymotherless, argparse;
21 import logging as log;
23 __project__ = pymotherless.__project__;
24 __program_name__ = pymotherless.__program_name__;
25 __project_url__ = pymotherless.__project_url__;
26 __version_info__ = pymotherless.__version_info__;
27 __version_date_info__ = pymotherless.__version_date_info__;
28 __version_date__ = pymotherless.__version_date__;
29 __version_date_plusrc__ = pymotherless.__version_date_plusrc__
30 __version__ = pymotherless.__version__;
31 __version_date_plusrc__ = pymotherless.__version_date_plusrc__;
33 geturls_cj = pymotherless.geturls_cj;
34 geturls_ua = pymotherless.geturls_ua;
35 geturls_ua_firefox_windows7 = pymotherless.geturls_ua_firefox_windows7;
36 geturls_ua_seamonkey_windows7 = pymotherless.geturls_ua_seamonkey_windows7;
37 geturls_ua_chrome_windows7 = pymotherless.geturls_ua_chrome_windows7;
38 geturls_ua_chromium_windows7 = pymotherless.geturls_ua_chromium_windows7;
39 geturls_ua_palemoon_windows7 = pymotherless.geturls_ua_palemoon_windows7;
40 geturls_ua_opera_windows7 = pymotherless.geturls_ua_opera_windows7;
41 geturls_ua_vivaldi_windows7 = pymotherless.geturls_ua_chromium_windows7;
42 geturls_ua_internet_explorer_windows7 = pymotherless.geturls_ua_internet_explorer_windows7;
43 geturls_ua_microsoft_edge_windows7 = pymotherless.geturls_ua_microsoft_edge_windows7;
44 geturls_ua_pymotherless_python = pymotherless.geturls_ua_pymotherless_python;
45 geturls_ua_pymotherless_python_alt = pymotherless.geturls_ua_pymotherless_python_alt;
46 geturls_ua_googlebot_google = pymotherless.geturls_ua_googlebot_google;
47 geturls_ua_googlebot_google_old = pymotherless.geturls_ua_googlebot_google_old;
48 geturls_headers = pymotherless.geturls_headers;
49 geturls_headers_firefox_windows7 = pymotherless.geturls_headers_firefox_windows7;
50 geturls_headers_seamonkey_windows7 = pymotherless.geturls_headers_seamonkey_windows7;
51 geturls_headers_chrome_windows7 = pymotherless.geturls_headers_chrome_windows7;
52 geturls_headers_chromium_windows7 = pymotherless.geturls_headers_chromium_windows7;
53 geturls_headers_palemoon_windows7 = pymotherless.geturls_headers_palemoon_windows7;
54 geturls_headers_opera_windows7 = pymotherless.geturls_headers_opera_windows7;
55 geturls_headers_vivaldi_windows7 = pymotherless.geturls_headers_vivaldi_windows7;
56 geturls_headers_internet_explorer_windows7 = pymotherless.geturls_headers_internet_explorer_windows7;
57 geturls_headers_microsoft_edge_windows7 = pymotherless.geturls_headers_microsoft_edge_windows7;
58 geturls_headers_pymotherless_python = pymotherless.geturls_headers_pymotherless_python;
59 geturls_headers_pymotherless_python_alt = pymotherless.geturls_headers_pymotherless_python_alt;
60 geturls_headers_googlebot_google = pymotherless.geturls_headers_googlebot_google;
61 geturls_headers_googlebot_google_old = pymotherless.geturls_headers_googlebot_google_old;
62 geturls_download_sleep = pymotherless.geturls_download_sleep;
64 parser = argparse.ArgumentParser(description="get urls of images/videos from motherless.com", 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="http://motherless.com/", help="specify a custom referer, use if the video access");
71 parser.add_argument("-g", "--get-url", action="store_true", help="simulate, quiet but print URL");
72 parser.add_argument("-p", "--get-pageurl", action="store_true", help="simulate, quiet but print URL");
73 parser.add_argument("-e", "--get-title", action="store_true", help="simulate, quiet but print title");
74 parser.add_argument("-i", "--get-id", action="store_true", help="simulate, quiet but print id");
75 parser.add_argument("-t", "--get-thumbnail", action="store_true", help="simulate, quiet but print thumbnail URL");
76 parser.add_argument("-f", "--get-filename", action="store_true", help="simulate, quiet but print output filename");
77 parser.add_argument("-u", "--get-username", action="store_true", help="simulate, quiet but print uploaders username");
78 parser.add_argument("-i", "--get-views", action="store_true", help="simulate, quiet but print number of views");
79 parser.add_argument("-f", "--get-favorites", action="store_true", help="simulate, quiet but print number of favorites");
80 parser.add_argument("-o", "--output-directory", default=os.path.realpath(os.getcwd()), help="specify a directory to output file to");
81 parser.add_argument("-l", "--use-httplib", default="urllib", help="select library to download file can be urllib or requests or mechanize");
82 parser.add_argument("-b", "--set-buffersize", default=524288, type=int, help="set how big buffersize is in bytes. how much it will download");
83 parser.add_argument("-V", "--verbose", action="store_true", help="print various debugging information");
84 getargs = parser.parse_args();
86 if(not pymotherless.check_httplib_support(getargs.use_httplib)):
87 getargs.use_httplib = "urllib";
89 getargs_cj = geturls_cj;
90 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"};
92 getargs.output_directory = os.path.realpath(getargs.output_directory);
94 if(getargs.verbose==True):
95 log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG);
97 if(getargs.dump_user_agent==True):
98 print(getargs.user_agent);
99 sys.exit();
101 motherless_linktype = pymotherless.get_motherless_link_type(getargs.url);
102 motherless_links = [];
103 motherless_links_dir = [];
104 if(motherless_linktype=="link"):
105 motherless_links.append(getargs.url);
106 if(motherless_linktype=="gallery"):
107 numpages = pymotherless.get_motherless_number_of_pages(getargs.url, getargs_headers, getargs_cj, getargs.use_httplib);
108 numcount = 1;
109 while(numcount <= numpages):
110 getlinks = pymotherless.get_motherless_galleries_links(getargs.url, getargs_headers, getargs_cj, getargs.use_httplib, page=numcount);
111 innumlinks = getlinks['numoflinks'];
112 innumcount = 0;
113 while(innumcount < innumlinks):
114 inmotherless_linktype = pymotherless.get_motherless_link_type(getlinks[innumcount]['url']);
115 if(inmotherless_linktype=="link"):
116 motherless_links_dir.append(getargs.url.rsplit('/', 1)[-1]);
117 motherless_links.append(getlinks[innumcount]['url']);
118 if(inmotherless_linktype=="gallery"):
119 innumpages = pymotherless.get_motherless_number_of_pages(getlinks[innumcount]['url'], getargs_headers, getargs_cj, getargs.use_httplib);
120 innumcountpages = 1;
121 while(innumcountpages <= innumpages):
122 ingetlinks = pymotherless.get_motherless_galleries_links(getlinks[innumcount]['url'], getargs_headers, getargs_cj, getargs.use_httplib, page=innumcountpages);
123 ininnumlinks = ingetlinks['numoflinks'];
124 ininnumcount = 0;
125 while(ininnumcount < ininnumlinks):
126 ininmotherless_linktype = pymotherless.get_motherless_link_type(ingetlinks[ininnumcount]['url']);
127 if(ininmotherless_linktype=="link"):
128 motherless_links_dir.append(getlinks[innumcount]['url'].rsplit('/', 1)[-1]);
129 motherless_links.append(ingetlinks[ininnumcount]['url']);
130 ininnumcount = ininnumcount + 1;
131 innumcountpages = innumcountpages + 1;
132 innumcount = innumcount + 1;
133 numcount = numcount + 1;
134 if(motherless_linktype=="board"):
135 getlinks = pymotherless.get_motherless_boards_links(getargs.url, getargs_headers, getargs_cj, getargs.use_httplib);
136 innumlinks = getlinks['numoflinks'];
137 innumcount = 0;
138 while(innumcount < innumlinks):
139 inmotherless_linktype = pymotherless.get_motherless_link_type(getlinks[innumcount]['url']);
140 if(inmotherless_linktype=="link"):
141 motherless_links_dir.append(getargs.url.rsplit('/', 1)[-1]);
142 motherless_links.append(getlinks[innumcount]['url']);
143 innumcount = innumcount + 1;
145 if(getargs.get_url==True):
146 listsize = len(motherless_links);
147 listcount = 0;
148 while(listcount < listsize):
149 print(pymotherless.get_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib)['url']);
150 listcount = listcount + 1;
152 if(getargs.get_pageurl==True):
153 listsize = len(motherless_links);
154 listcount = 0;
155 while(listcount < listsize):
156 print(motherless_links[listcount]);
157 listcount = listcount + 1;
159 if(getargs.get_filename==True):
160 listsize = len(motherless_links);
161 listcount = 0;
162 while(listcount < listsize):
163 print(pymotherless.get_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib)['fullfilename']);
164 listcount = listcount + 1;
166 if(getargs.get_title==True):
167 listsize = len(motherless_links);
168 listcount = 0;
169 while(listcount < listsize):
170 print(pymotherless.get_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib)['title']);
171 listcount = listcount + 1;
173 if(getargs.get_username==True):
174 listsize = len(motherless_links);
175 listcount = 0;
176 while(listcount < listsize):
177 print(pymotherless.get_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib)['username']);
178 listcount = listcount + 1;
180 if(getargs.get_views==True):
181 listsize = len(motherless_links);
182 listcount = 0;
183 while(listcount < listsize):
184 print(pymotherless.get_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib)['numberofviews']);
185 listcount = listcount + 1;
187 if(getargs.get_favorites==True):
188 listsize = len(motherless_links);
189 listcount = 0;
190 while(listcount < listsize):
191 print(pymotherless.get_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib)['numberoffavorites']);
192 listcount = listcount + 1;
194 if(getargs.get_url==False and getargs.get_pageurl==False and getargs.get_thumbnail==False and getargs.get_filename==False and getargs.get_title==False and getargs.get_username==False and getargs.get_views==False and getargs.get_favorites==False):
195 listsize = len(motherless_links);
196 listcount = 0;
197 while(listcount < listsize):
198 listcountalt = listcount + 1;
199 percentage = str("{0:.2f}".format(float(float(listcountalt / listsize) * 100))).rstrip('0').rstrip('.')+"%";
200 log.info("Downloading URL Number "+str(listcountalt)+" / "+str(listsize)+" "+str(percentage));
201 if(motherless_linktype=="link"):
202 pymotherless.download_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib, buffersize=[getargs.set_buffersize, getargs.set_buffersize], outpath=getargs.output_directory);
203 if(motherless_linktype=="board" or motherless_linktype=="gallery"):
204 pymotherless.download_motherless_links(motherless_links[listcount], getargs_headers, getargs_cj, getargs.use_httplib, buffersize=[getargs.set_buffersize, getargs.set_buffersize], outpath=getargs.output_directory+os.path.sep+motherless_links_dir[listcount]);
205 listcount = listcount + 1;