Another small update.
[PyMotherless.git] / pymotherless.py
blobfce9d6e01e8632a6c42fbb9d005fce10433ff65f
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.py - Last Update: 1/14/2016 Ver. 0.2.5 RC 1 - Author: cooldude2k $
17 '''
19 from __future__ import division, absolute_import, print_function;
20 import re, os, sys, urllib, gzip, time, datetime, argparse, cgi, subprocess;
21 if(sys.version[0]=="2"):
22 try:
23 from cStringIO import StringIO;
24 except ImportError:
25 from StringIO import StringIO;
26 import urllib2, urlparse, cookielib;
27 if(sys.version[0]=="3"):
28 from io import StringIO, BytesIO;
29 import urllib.request as urllib2;
30 import urllib.parse as urlparse;
31 import http.cookiejar as cookielib;
32 if(__name__ == "__main__"):
33 sys.tracebacklimit = 0;
34 __program_name__ = "PyMotherless";
35 __version_info__ = (0, 2, 5, "RC 1");
36 __version_date__ = "2016.01.14";
37 if(__version_info__[3]!=None):
38 __version__ = str(__version_info__[0])+"."+str(__version_info__[1])+"."+str(__version_info__[2])+" "+str(__version_info__[3]);
39 if(__version_info__[3]==None):
40 __version__ = str(__version_info__[0])+"."+str(__version_info__[1])+"."+str(__version_info__[2]);
42 geturls_cj = cookielib.CookieJar();
43 geturls_ua_firefox_windows7 = "Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0";
44 geturls_ua_chrome_windows7 = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
45 geturls_ua_internet_explorer_windows7 = "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko";
46 geturls_ua = geturls_ua_firefox_windows7;
47 geturls_headers = [("Referer", "http://motherless.com/"), ("User-Agent", geturls_ua), ("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")];
48 geturls_download_sleep = 0;
50 def add_url_param(url, **params):
51 n=3;
52 parts = list(urlparse.urlsplit(url));
53 d = dict(cgi.parse_qsl(parts[n])); # use cgi.parse_qs for list values
54 d.update(params);
55 parts[n]=urllib.urlencode(d);
56 return urlparse.urlunsplit(parts);
58 os.environ["PATH"] = os.environ["PATH"] + os.pathsep + os.path.dirname(os.path.realpath(__file__)) + os.pathsep + os.getcwd();
59 def which_exec(execfile):
60 for path in os.environ["PATH"].split(":"):
61 if os.path.exists(path + "/" + execfile):
62 return path + "/" + execfile;
64 def listize(varlist):
65 il = 0;
66 ix = len(varlist);
67 ilx = 1;
68 newlistreg = {};
69 newlistrev = {};
70 newlistfull = {};
71 while(il < ix):
72 newlistreg.update({ilx: varlist[il]});
73 newlistrev.update({varlist[il]: ilx});
74 ilx = ilx + 1;
75 il = il + 1;
76 newlistfull = {1: newlistreg, 2: newlistrev, 'reg': newlistreg, 'rev': newlistrev};
77 return newlistfull;
79 def twolistize(varlist):
80 il = 0;
81 ix = len(varlist);
82 ilx = 1;
83 newlistnamereg = {};
84 newlistnamerev = {};
85 newlistdescreg = {};
86 newlistdescrev = {};
87 newlistfull = {};
88 while(il < ix):
89 newlistnamereg.update({ilx: varlist[il][0].strip()});
90 newlistnamerev.update({varlist[il][0].strip(): ilx});
91 newlistdescreg.update({ilx: varlist[il][1].strip()});
92 newlistdescrev.update({varlist[il][1].strip(): ilx});
93 ilx = ilx + 1;
94 il = il + 1;
95 newlistnametmp = {1: newlistnamereg, 2: newlistnamerev, 'reg': newlistnamereg, 'rev': newlistnamerev};
96 newlistdesctmp = {1: newlistdescreg, 2: newlistdescrev, 'reg': newlistdescreg, 'rev': newlistdescrev};
97 newlistfull = {1: newlistnametmp, 2: newlistdesctmp, 'name': newlistnametmp, 'desc': newlistdesctmp}
98 return newlistfull;
100 def arglistize(proexec, *varlist):
101 il = 0;
102 ix = len(varlist);
103 ilx = 1;
104 newarglist = [proexec];
105 while(il < ix):
106 if varlist[il][0] is not None:
107 newarglist.append(varlist[il][0]);
108 if varlist[il][1] is not None:
109 newarglist.append(varlist[il][1]);
110 il = il + 1;
111 return newarglist;
113 def make_http_headers_from_dict(headers={'Referer': "http://motherless.com/", 'User-Agent': geturls_ua, '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"}):
114 returnval = [];
115 for headkey, headvalue in headers.iteritems():
116 returnval.append((headkey, headvalue));
117 return returnval;
119 def download_from_url(httpurl, httpheaders, httpcookie, sleep=-1):
120 global geturls_download_sleep;
121 if(sleep<0):
122 sleep = geturls_download_sleep;
123 geturls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(httpcookie));
124 geturls_opener.addheaders = httpheaders;
125 time.sleep(sleep);
126 geturls_text = geturls_opener.open(httpurl);
127 if(geturls_text.info().get("Content-Encoding")=="gzip" or geturls_text.info().get("Content-Encoding")=="deflate"):
128 if(sys.version[0]=="2"):
129 strbuf = StringIO(geturls_text.read());
130 if(sys.version[0]=="3"):
131 strbuf = BytesIO(geturls_text.read());
132 gzstrbuf = gzip.GzipFile(fileobj=strbuf);
133 if(sys.version[0]=="2"):
134 returnval = gzstrbuf.read()[:];
135 if(sys.version[0]=="3"):
136 returnval = gzstrbuf.read()[:].decode('ascii', 'replace');
137 if(geturls_text.info().get("Content-Encoding")!="gzip" and geturls_text.info().get("Content-Encoding")!="deflate"):
138 returnval = geturls_text.read()[:];
139 return returnval;
141 def download_from_url_file(httpurl, httpheaders, httpcookie, sleep=-1):
142 global geturls_download_sleep;
143 if(sleep<0):
144 sleep = geturls_download_sleep;
145 geturls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(httpcookie));
146 geturls_opener.addheaders = httpheaders;
147 time.sleep(sleep);
148 geturls_text = geturls_opener.open(httpurl);
149 if(geturls_text.info().get("Content-Encoding")=="gzip" or geturls_text.info().get("Content-Encoding")=="deflate"):
150 if(sys.version[0]=="2"):
151 strbuf = StringIO(geturls_text.read());
152 if(sys.version[0]=="3"):
153 strbuf = BytesIO(geturls_text.read());
154 gzstrbuf = gzip.GzipFile(fileobj=strbuf);
155 returnval = gzstrbuf.read()[:];
156 if(geturls_text.info().get("Content-Encoding")!="gzip" and geturls_text.info().get("Content-Encoding")!="deflate"):
157 returnval = geturls_text.read()[:];
158 return returnval;
160 def download_from_url_to_file(httpurl, httpheaders, httpcookie, outfile="-", outpath=os.getcwd(), sleep=-1):
161 global geturls_download_sleep;
162 if(sleep<0):
163 sleep = geturls_download_sleep;
164 if(not outfile=="-"):
165 outpath = outpath.rstrip(os.path.sep);
166 filepath = os.path.realpath(outpath+os.path.sep+outfile);
167 if(not os.path.exists(outpath)):
168 os.makedirs(outpath);
169 if(os.path.exists(outpath) and os.path.isfile(outpath)):
170 return False;
171 if(os.path.exists(filepath) and os.path.isdir(filepath)):
172 return False;
173 with open(filepath, 'wb+') as f:
174 f.write(download_from_url_file(httpurl, httpheaders, httpcookie, sleep));
175 f.closed;
176 returnval = True;
177 if(outfile=="-" and sys.version[0]=="2"):
178 f = StringIO();
179 f.write(download_from_url_file(httpurl, httpheaders, httpcookie, sleep));
180 returnval = f.getvalue();
181 f.closed;
182 if(outfile=="-" and sys.version[0]=="3"):
183 f = BytesIO();
184 f.write(download_from_url_file(httpurl, httpheaders, httpcookie, sleep));
185 returnval = f.getvalue();
186 f.closed;
187 return returnval;
189 def get_motherless_get_number_pages(httpurl, httpheaders, httpcookie):
190 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
191 mregex_getpagenum = re.escape("page=")+"([0-9]+)"+re.escape("\" class=\"pop\" rel=\"")+"([0-9]+)"+re.escape("\">")+"([0-9]+)"+re.escape("</a>");
192 mlesspagenum = re.findall(mregex_getpagenum, mrtext);
193 try:
194 returnval = mlesspagenum[-1][0];
195 except:
196 returnval = 1;
197 return returnval;
199 def get_motherless_get_link_type(httpurl):
200 mlessvidqstr = urlparse.parse_qs(urlparse.urlparse(httpurl).query);
201 mlessvidid_parts = urlparse.urlparse(httpurl);
202 mlessvidid = mlessvidid_parts.path.split("/");
203 returnval = False;
204 if(mlessvidid[1]=="videos" and len(mlessvidid)==3 and (mlessvidid[2]=="recent" or mlessvidid[2]=="favorited" or mlessvidid[2]=="viewed" or mlessvidid[2]=="commented" or mlessvidid[2]=="popular")):
205 returnval = "gallery";
206 if(mlessvidid[1]=="images" and len(mlessvidid)==3 and (mlessvidid[2]=="recent" or mlessvidid[2]=="favorited" or mlessvidid[2]=="viewed" or mlessvidid[2]=="commented" or mlessvidid[2]=="popular")):
207 returnval = "gallery";
208 if(mlessvidid[1]=="galleries" and len(mlessvidid)==3 and (mlessvidid[2]=="recent" or mlessvidid[2]=="favorited" or mlessvidid[2]=="viewed" or mlessvidid[2]=="commented" or mlessvidid[2]=="popular")):
209 returnval = "gallery";
210 if(mlessvidid[1]=="live" and len(mlessvidid)==3 and (mlessvidid[2]=="videos" or mlessvidid[2]=="images")):
211 returnval = "gallery";
212 if(mlessvidid[1]=="u" and len(mlessvidid)==3):
213 returnval = "gallery";
214 if(mlessvidid[1]=="f" and len(mlessvidid)==4 and (mlessvidid[2]=="videos" or mlessvidid[2]=="images" or mlessvidid[2]=="galleries")):
215 returnval = "gallery";
216 if(mlessvidid[1]=="galleries" and len(mlessvidid)==4 and mlessvidid[2]=="member"):
217 returnval = "gallery";
218 if(mlessvidid[1]=="galleries" and len(mlessvidid)==5 and mlessvidid[2]=="member" and (mlessvidid[4]=="created" or mlessvidid[4]=="viewed" or mlessvidid[4]=="favorited" or mlessvidid[4]=="commented")):
219 returnval = "gallery";
220 if(mlessvidid[1]=="gv" and len(mlessvidid)==3):
221 returnval = "gallery";
222 if(mlessvidid[1]=="gi" and len(mlessvidid)==3):
223 returnval = "gallery";
224 if(mlessvidid[1]=="term" and len(mlessvidid)==3 and (mlessvidid[2]=="videos" or mlessvidid[2]=="images" or mlessvidid[2]=="galleries")):
225 returnval = "gallery";
226 if(mlessvidid[1]=="g" and len(mlessvidid)==4):
227 returnval = "file";
228 if(mlessvidid[1]=="random" and len(mlessvidid)==3 and (mlessvidid[2]=="video" or mlessvidid[2]=="image")):
229 returnval = "file";
230 if(re.match("^V", mlessvidid[1]) and len(mlessvidid)==2):
231 returnval = "board";
232 if(mlessvidid[1]=="members" and len(mlessvidid)==2):
233 returnval = "member";
234 if(mlessvidid[1]=="members" and mlessvidid[2]=="search" and len(mlessvidid)==3):
235 returnval = "member";
236 if(mlessvidid[1]=="members" and len(mlessvidid)==4 and (mlessvidid[2]=="uploader" or mlessvidid[2]=="viewed" or mlessvidid[2]=="social" or mlessvidid[2]=="favorited" or mlessvidid[2]=="commented" or mlessvidid[2]=="mentioned" or mlessvidid[2]=="verified")):
237 returnval = "member";
238 if(mlessvidid[1]=="girls" and len(mlessvidid)==2):
239 returnval = "girls";
240 if(mlessvidid_parts.netloc=="cdn.images.motherlessmedia.com" or mlessvidid_parts.netloc=="cdn.videos.motherlessmedia.com" or mlessvidid_parts.netloc=="cdn.thumbs.motherlessmedia.com"):
241 returnval = "download";
242 if(returnval==False and len(mlessvidid)==2):
243 returnval = "file";
244 return returnval;
246 def get_motherless_links(httpurl, httpheaders, httpcookie):
247 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
248 mregex_gettitle = re.escape("<title>")+"(.*)"+re.escape(" - MOTHERLESS.COM</title>");
249 mlesstitle = re.findall(mregex_gettitle, mrtext);
250 mregex_geturlone = re.escape("__fileurl = 'http://cdn.")+"(images|videos)"+re.escape(".motherlessmedia.com/")+"(images|videos)"+re.escape("/")+"([\w\/\?\&\=\.\-]+)"+re.escape("';");
251 mlesslinkone = re.findall(mregex_geturlone, mrtext);
252 mregex_geturltwo = re.escape("<meta property=\"og:image\" content=\"http://cdn.")+"(images|thumbs)"+re.escape(".motherlessmedia.com/")+"(images|thumbs)"+re.escape("/")+"([\w\/\?\&\=\.\-]+)"+re.escape("\">");
253 mlesslinktwo = re.findall(mregex_geturltwo, mrtext);
254 filenameext = os.path.basename(urlparse.urljoin("http://cdn."+mlesslinkone[0][0]+".motherlessmedia.com/"+mlesslinkone[0][1]+"/"+mlesslinkone[0][2], urlparse.urlparse("http://cdn."+mlesslinkone[0][0]+".motherlessmedia.com/"+mlesslinkone[0][1]+"/"+mlesslinkone[0][2]).path));
255 filename, fileextension = os.path.splitext(filenameext);
256 thumbfilenameext = os.path.basename(urlparse.urljoin("http://cdn.images.motherlessmedia.com/"+mlesslinktwo[0][1]+"/"+mlesslinktwo[0][2], urlparse.urlparse("http://cdn.images.motherlessmedia.com/"+mlesslinktwo[0][1]+"/"+mlesslinktwo[0][2]).path));
257 thumbfilename, thumbfileextension = os.path.splitext(thumbfilenameext);
258 if(mlesslinkone[0][1]=="images"):
259 thumbnailaltpart = thumbfilename+"-zoom"+thumbfileextension;
260 thumbnailalt = "http://cdn.thumbs.motherlessmedia.com/thumbs/"+thumbnailaltpart;
261 thumbnailaltfilenameext = os.path.basename(urlparse.urljoin(thumbnailalt, urlparse.urlparse(thumbnailalt).path));
262 thumbnailaltfilename, thumbnailaltfileextension = os.path.splitext(thumbnailaltfilenameext);
263 if(mlesslinkone[0][1]=="videos"):
264 thumbnailaltpart = +thumbfilename+"-small"+thumbfileextension;
265 thumbnailalt = "http://cdn.thumbs.motherlessmedia.com/thumbs/"+thumbnailaltpart;
266 thumbnailaltfilenameext = os.path.basename(urlparse.urljoin(thumbnailalt, urlparse.urlparse(thumbnailalt).path));
267 thumbnailaltfilename, thumbnailaltfileextension = os.path.splitext(thumbnailaltfilenameext);
268 returnval = False;
269 mlesslinktype = get_motherless_get_link_type("http://cdn."+mlesslinkone[0][0]+".motherlessmedia.com/"+mlesslinkone[0][1]+"/"+mlesslinkone[0][2]);
270 if(mlesslinkone[0][1]=="images"):
271 returnval = {'type': mlesslinkone[0][1], 'linktype': mlesslinktype, 'url': "http://cdn."+mlesslinkone[0][0]+".motherlessmedia.com/"+mlesslinkone[0][1]+"/"+mlesslinkone[0][2], 'orginurl': httpurl, 'thumbnail': "http://cdn.thumbs.motherlessmedia.com/thumbs/"+mlesslinktwo[0][2], 'thumbnailalt': thumbnailalt+"?from_helper", 'title': mlesstitle[0], 'fullfilename': filenameext, 'filename': filename, 'extension': fileextension, 'thumbfullfilename': thumbfilenameext, 'thumbfilename': thumbfilename, 'thumbextension': thumbfileextension, 'thumbnailaltfullfilename': thumbnailaltpart, 'thumbnailaltfilename': thumbnailaltfilename, 'thumbnailaltextension': thumbnailaltfileextension};
272 if(mlesslinkone[0][1]=="videos"):
273 returnval = {'type': mlesslinkone[0][1], 'url': "http://cdn."+mlesslinkone[0][0]+".motherlessmedia.com/"+mlesslinkone[0][1]+"/"+mlesslinkone[0][2], 'orginurl': httpurl, 'thumbnail': "http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[0][1]+"/"+mlesslinktwo[0][2], 'thumbnailalt': thumbnailalt+"?from_helper", 'title': mlesstitle[0], 'fullfilename': filenameext, 'filename': filename, 'extension': fileextension, 'thumbfullfilename': thumbfilenameext, 'thumbfilename': thumbfilename, 'thumbextension': thumbfileextension, 'thumbnailaltfullfilename': thumbnailaltpart, 'thumbnailaltfilename': thumbnailaltfilename, 'thumbnailaltextension': thumbnailaltfileextension};
274 return returnval;
276 def get_motherless_galleries_links(httpurl, httpheaders, httpcookie, page=1, getlinks=[0, -1]):
277 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
278 mregex_getpagenum = re.escape("page=")+"([0-9]+)"+re.escape("\" class=\"pop\" rel=\"")+"([0-9]+)"+re.escape("\">")+"([0-9]+)"+re.escape("</a>");
279 mlesspagenum = re.findall(mregex_getpagenum, mrtext);
280 try:
281 lastpage = mlesspagenum[-1][0];
282 except:
283 lastpage = 1;
284 if(page>lastpage):
285 page = lastpage;
286 httpurl = add_url_param(httpurl, page=str(page));
287 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
288 mregex_geturlone = re.escape("<a href=\"/")+"([\w\/]+)"+re.escape("\" class=\"img-container\" target=\"_self\">");
289 mrtext_tmp = re.sub(re.escape("http://motherless.com"), "", mrtext);
290 mrtext_tmp = re.sub(re.escape("http://www.motherless.com"), "", mrtext_tmp);
291 mrtext_tmp = re.sub(re.escape("http://motherless.com"), "", mrtext_tmp);
292 mrtext_tmp = re.sub(re.escape("http://www.motherless.com"), "", mrtext_tmp);
293 mlesslinkone = re.findall(mregex_geturlone, mrtext_tmp);
294 mregex_geturltwo = re.escape("<img class=\"static\" src=\"http://cdn.thumbs.motherlessmedia.com/")+"([\w\/\?\&\=\.\-]+)"+re.escape("\" data-strip-src=\"http://cdn.thumbs.motherlessmedia.com/")+"([\w\/\?\&\=\.\-]+)"+re.escape("\" alt=\"")+"(.*)"+re.escape("\" />");
295 mlesslinktwo = re.findall(mregex_geturltwo, mrtext);
296 if(getlinks[1]>len(mlesslinkone) or getlinks[1]==-1):
297 getlinks[1] = len(mlesslinkone);
298 if(getlinks[0]>getlinks[1] and not getlinks[1]==-1):
299 tmpgetlinks0 = getlinks[0];
300 tmpgetlinks1 = getlinks[1];
301 getlinks[0] = tmpgetlinks1;
302 getlinks[1] = tmpgetlinks0;
303 if(getlinks[0]<0):
304 getlinks[0] = 0;
305 mli = getlinks[0];
306 mlil = getlinks[1];
307 returnval = {'pages': lastpage};
308 returnval.update({'curpage': page});
309 returnval.update({'numoflinks': mlil});
310 returnval.update({'numofalllinks': len(mlesslinkone)});
311 returnval.update({'orginurl': httpurl});
312 mlessrootlinktype = get_motherless_get_link_type(httpurl);
313 returnval.update({'linktype': mlessrootlinktype});
314 while(mli<mlil):
315 stripfilenameext = os.path.basename(urlparse.urljoin("http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[mli][1], urlparse.urlparse("http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[mli][1]).path));
316 stripfilename, stripfileextension = os.path.splitext(stripfilenameext);
317 thumbfilenameext = os.path.basename(urlparse.urljoin("http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[mli][0], urlparse.urlparse("http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[mli][0]).path));
318 thumbfilename, thumbfileextension = os.path.splitext(thumbfilenameext);
319 mlesslinktype = get_motherless_get_link_type("http://motherless.com/"+mlesslinkone[mli]);
320 returnval.update({mli: {'linktype': mlesslinktype, 'url': "http://motherless.com/"+mlesslinkone[mli], 'thumbnail': "http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[mli][0], 'strip': "http://cdn.thumbs.motherlessmedia.com/"+mlesslinktwo[mli][1], 'title': mlesslinktwo[mli][2], 'thumbfullfilename': thumbfilenameext, 'thumbfilename': thumbfilename, 'thumbextension': thumbfileextension, 'stripfullfilename': stripfilenameext, 'stripfilename': stripfilename, 'stripextension': stripfileextension}});
321 mli = mli + 1;
322 return returnval;
324 def get_motherless_boards_links(httpurl, httpheaders, httpcookie, getlinks=[0, -1]):
325 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
326 mregex_geturlone = re.escape("<a href=\"/")+"([\w\/]+)"+re.escape("\" title=\"motherless link\">");
327 mrtext_tmp = re.sub(re.escape("http://motherless.com"), "", mrtext);
328 mrtext_tmp = re.sub(re.escape("http://www.motherless.com"), "", mrtext_tmp);
329 mrtext_tmp = re.sub(re.escape("http://motherless.com"), "", mrtext_tmp);
330 mrtext_tmp = re.sub(re.escape("http://www.motherless.com"), "", mrtext_tmp);
331 mlesslinkone = re.findall(mregex_geturlone, mrtext_tmp);
332 if(getlinks[1]>len(mlesslinkone) or getlinks[1]==-1):
333 getlinks[1] = len(mlesslinkone);
334 if(getlinks[0]>getlinks[1] and not getlinks[1]==-1):
335 tmpgetlinks0 = getlinks[0];
336 tmpgetlinks1 = getlinks[1];
337 getlinks[0] = tmpgetlinks1;
338 getlinks[1] = tmpgetlinks0;
339 if(getlinks[0]<0):
340 getlinks[0] = 0;
341 mli = getlinks[0];
342 mlil = getlinks[1];
343 returnval = {'numoflinks': mlil};
344 returnval.update({'numofalllinks': len(mlesslinkone)});
345 returnval.update({'orginurl': httpurl});
346 mlessrootlinktype = get_motherless_get_link_type(httpurl);
347 returnval.update({'linktype': mlessrootlinktype});
348 while(mli<mlil):
349 mlesslinktype = get_motherless_get_link_type("http://motherless.com/"+mlesslinkone[mli]);
350 returnval.update({mli: {'linktype': mlesslinktype, 'url': "http://motherless.com/"+mlesslinkone[mli]}});
351 mli = mli + 1;
352 return returnval;
354 def get_motherless_search_members(httpurl, httpheaders, httpcookie, page=1, getlinks=[0, -1]):
355 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
356 mregex_getpagenum = re.escape("page=")+"([0-9]+)"+re.escape("\" class=\"pop\" rel=\"")+"([0-9]+)"+re.escape("\">")+"([0-9]+)"+re.escape("</a>");
357 mlesspagenum = re.findall(mregex_getpagenum, mrtext);
358 try:
359 lastpage = mlesspagenum[-1][0];
360 except:
361 lastpage = 1;
362 if(page>lastpage):
363 page = lastpage;
364 httpurl = add_url_param(httpurl, page=str(page));
365 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
366 mregex_getuname = re.escape("<tr rel=\"")+"([\w\/\?\&\=\.\-]+)"+re.escape("\"");
367 mlessuname = re.findall(mregex_getuname, mrtext);
368 mregex_geturlname = re.escape("<a href=\"/m/")+"([\w\/\?\&\=\.\-]+)"+re.escape("\" target=\"_blank\">\n <img");
369 mlessurlname = re.findall(mregex_geturlname, mrtext);
370 mregex_getavatar = re.escape("<img\n src=\"")+"(.*)"+re.escape("\"\n class=\"avatar avatar-small\"");
371 mlessavatar = re.findall(mregex_getavatar, mrtext);
372 if(getlinks[1]>len(mlesslinkone) or getlinks[1]==-1):
373 getlinks[1] = len(mlesslinkone);
374 if(getlinks[0]>getlinks[1] and not getlinks[1]==-1):
375 tmpgetlinks0 = getlinks[0];
376 tmpgetlinks1 = getlinks[1];
377 getlinks[0] = tmpgetlinks1;
378 getlinks[1] = tmpgetlinks0;
379 if(getlinks[0]<0):
380 getlinks[0] = 0;
381 mli = getlinks[0];
382 mlil = getlinks[1];
383 returnval = {'numoflinks': mlil};
384 returnval.update({'numofalllinks': len(mlessuname)});
385 returnval.update({'pages': lastpage});
386 returnval.update({'curpage': page});
387 returnval.update({'orginurl': httpurl});
388 mlessrootlinktype = get_motherless_get_link_type(httpurl);
389 returnval.update({'linktype': mlessrootlinktype});
390 while(mli<mlil):
391 avatarfilenameext = os.path.basename(urlparse.urljoin(mlessavatar[mli], urlparse.urlparse(mlessavatar[mli]).path));
392 avatarfilename, avatarfileextension = os.path.splitext(avatarfilenameext);
393 mlesslinktype = get_motherless_get_link_type("http://motherless.com/"+mlessurlname[mli]);
394 returnval.update({mli: {'linktype': mlesslinktype, 'url': "http://motherless.com/"+mlessurlname[mli], 'username': mlessuname[mli], 'avatarurl': mlessavatar[mli], 'avatarfullfilename': avatarfilenameext, 'avatarfilename': avatarfilename, 'avatarextension': avatarfileextension}});
395 mli = mli + 1;
396 return returnval;
398 def get_motherless_girls(httpurl, httpheaders, httpcookie, getlinks=[0, -1]):
399 mrtext = download_from_url(httpurl, httpheaders, httpcookie);
400 mregex_getuname = re.escape("<a href=\"")+"(.*)"+re.escape("\" rev=\"")+"([\w\/\?\&\=\.\-]+)"+re.escape("\" rel=\"")+"(.*)"+re.escape("\">");
401 mlessuname = re.findall(mregex_getuname, mrtext);
402 mregex_geturlname = re.escape("\n\t\t\t\t\t\t<a href=\"/m/")+"([\w\/\?\&\=\.\-]+)"+re.escape("\" target=\"_blank\">");
403 mlessurlname = re.findall(mregex_geturlname, mrtext);
404 if(getlinks[1]>len(mlesslinkone) or getlinks[1]==-1):
405 getlinks[1] = len(mlesslinkone);
406 if(getlinks[0]>getlinks[1] and not getlinks[1]==-1):
407 tmpgetlinks0 = getlinks[0];
408 tmpgetlinks1 = getlinks[1];
409 getlinks[0] = tmpgetlinks1;
410 getlinks[1] = tmpgetlinks0;
411 if(getlinks[0]<0):
412 getlinks[0] = 0;
413 mli = getlinks[0];
414 mlil = getlinks[1];
415 returnval = {'numoflinks': mlil};
416 returnval.update({'numofalllinks': len(mlessuname)});
417 returnval.update({'orginurl': httpurl});
418 mlessrootlinktype = get_motherless_get_link_type(httpurl);
419 returnval.update({'linktype': mlessrootlinktype});
420 while(mli<mlil):
421 avatarfilenameext = os.path.basename(urlparse.urljoin(mlessuname[mli][0], urlparse.urlparse(mlessuname[mli][0]).path));
422 avatarfilename, avatarfileextension = os.path.splitext(avatarfilenameext);
423 mlesslinktype = get_motherless_get_link_type("http://motherless.com/"+mlessuname[mli][1]);
424 returnval.update({mli: {'linktype': mlesslinktype, 'url': "http://motherless.com/"+mlessuname[mli][1], 'username': mlessuname[mli][1], 'usernamealt': mlessuname[mli][2], 'avatarurl': mlessuname[mli][0], 'avatarfullfilename': avatarfilenameext, 'avatarfilename': avatarfilename, 'avatarextension': avatarfileextension}});
425 mli = mli + 1;
426 return returnval;
428 def get_motherless_get_link_by_type(httpurl, httpheaders, httpcookie, page=1, getlinks=[0, -1]):
429 returnval = False;
430 if(get_motherless_get_link_type(httpurl)=="file"):
431 returnval = get_motherless_links(httpurl, httpheaders, httpcookie);
432 if(get_motherless_get_link_type(httpurl)=="gallery"):
433 returnval = get_motherless_galleries_links(httpurl, httpheaders, httpcookie, page);
434 if(get_motherless_get_link_type(httpurl)=="board"):
435 returnval = get_motherless_boards_links(httpurl, httpheaders, httpcookie);
436 if(get_motherless_get_link_type(httpurl)=="member"):
437 returnval = get_motherless_search_members(httpurl, httpheaders, httpcookie, page);
438 if(get_motherless_get_link_type(httpurl)=="girls"):
439 returnval = get_motherless_girls(httpurl, httpheaders, httpcookie);
440 if(get_motherless_get_link_type(httpurl)=="download"):
441 returnval = httpurl;
442 return returnval;
444 def view_motherless_links(httpurl, httpheaders, httpcookie, viewerpro, prearg=[], proarg=[]):
445 commandlist = [viewerpro] + prearg;
446 commandlist = commandlist + [get_motherless_links(httpurl, httpheaders, httpcookie)['url']];
447 commandlist = commandlist + proarg;
448 mpvplaylistp = subprocess.Popen(commandlist, stdout=subprocess.PIPE, stderr=subprocess.PIPE);
449 mpvplayout, mpvplayerr = mpvplaylistp.communicate();
450 return True;
452 def download_motherless_links(httpurl, httpheaders, httpcookie, sleep=-1, outfile="-", outpath=os.getcwd(), usetitlename=False):
453 global geturls_download_sleep;
454 if(sleep<0):
455 sleep = geturls_download_sleep;
456 mlessurl = get_motherless_links(httpurl, httpheaders, httpcookie);
457 outputname = mlessurl['fullfilename'];
458 outpath = outpath.rstrip(os.path.sep);
459 if(usetitlename==True):
460 outputname = mlessurl['title'];
461 if(usetitlename=="-" and outfile=="-"):
462 outputname = "-";
463 if(usetitlename=="-" and not outfile=="-"):
464 outputname = outfile;
465 returnval = download_from_url_to_file(mlessurl['url'], httpheaders, httpcookie, outputname, outpath, sleep);
466 return returnval;
468 def download_motherless_links_by_type(httpurl, httpheaders, httpcookie, sleep=-1, outfile="-", outpath=os.getcwd(), usetitlename=False, page=1, getlinks=[0, -1]):
469 global geturls_download_sleep;
470 if(sleep<0):
471 sleep = geturls_download_sleep;
472 mlessurl = get_motherless_get_link_by_type(httpurl, httpheaders, httpcookie, page);
473 if(mlessurl['linktype']=="download"):
474 outputname = mlessurl['fullfilename'];
475 outpathname = outpath.rstrip(os.path.sep);
476 if(usetitlename==True):
477 outputname = mlessurl['title'];
478 if(usetitlename=="-" and outfile[mli]=="-"):
479 outputname = "-";
480 if(usetitlename=="-" and not outfile[mli]=="-"):
481 outputname = outfile;
482 returnval = download_from_url_to_file(mlessurl['url'], httpheaders, httpcookie, outputname, outpathname, sleep);
483 if(not mlessurl['linktype']=="download"):
484 returnval = mlessurl;
485 return returnval;
487 def download_motherless_galleries_links(httpurl, httpheaders, httpcookie, sleep=-1, outfile="-", outpath=os.getcwd(), usetitlename=False, page=1, getlinks=[0, -1]):
488 global geturls_download_sleep;
489 if(sleep<0):
490 sleep = geturls_download_sleep;
491 mlessgalleries = get_motherless_galleries_links(httpurl, httpheaders, httpcookie, page, getlinks);
492 mli = 0;
493 mlil = mlessgalleries['numoflinks'];
494 returnval = {'pages': mlessgalleries['pages']};
495 returnval.update({'numoflists': mlessgalleries['numoflinks']});
496 returnval.update({'curpage': mlessgalleries['curpage']});
497 returnval.update({'numoflinks': mlessgalleries['numoflinks']});
498 returnval.update({'numofalllinks': mlessgalleries['numofalllinks']});
499 returnval.update({'orginurl': httpurl});
500 while(mli<mlil):
501 mlesslink = get_motherless_links(mlessgalleries[mli]['url'], httpheaders, httpcookie);
502 outputname = mlesslink['fullfilename'];
503 outpath = outpath.rstrip(os.path.sep);
504 if(usetitlename==True):
505 outputname = mlesslink['title'];
506 if(usetitlename=="-" and outfile=="-"):
507 outputname = "-";
508 if(usetitlename=="-" and not outfile=="-"):
509 outputname = outfile;
510 returnval.update({mli: {'download': download_from_url_to_file(mlesslink['url'], httpheaders, httpcookie, outputname, outpath, sleep), 'linkinfo': mlesslink}});
511 mli = mli + 1;
512 return returnval;
514 def download_get_motherless_boards_links(httpurl, httpheaders, httpcookie, sleep=-1, outfile="-", outpath=os.getcwd(), usetitlename=False, getlinks=[0, -1]):
515 global geturls_download_sleep;
516 if(sleep<0):
517 sleep = geturls_download_sleep;
518 mlessgalleries = get_motherless_boards_links(httpurl, httpheaders, httpcookie, getlinks);
519 mli = 0;
520 mlil = mlessgalleries['numoflinks'];
521 returnval = {'numoflists': mlessgalleries['numoflinks']};
522 returnval.update({'numofalllinks': mlessgalleries['numofalllinks']});
523 returnval.update({'orginurl': httpurl});
524 while(mli<mlil):
525 mlesslink = get_motherless_links(mlessgalleries[mli]['url'], httpheaders, httpcookie);
526 outputname = mlesslink['fullfilename'];
527 outpath = outpath.rstrip(os.path.sep);
528 if(usetitlename==True):
529 outputname = mlesslink['title'];
530 if(usetitlename=="-" and outfile=="-"):
531 outputname = "-";
532 if(usetitlename=="-" and not outfile=="-"):
533 outputname = outfile;
534 returnval.update({mli: {'download': download_from_url_to_file(mlesslink['url'], httpheaders, httpcookie, outputname, outpath, sleep), 'linkinfo': mlesslink}});
535 mli = mli + 1;
536 return returnval;