Small update to motherless-dl.py added support for viewing users image/video urls.
[Python-Scripts.git] / MiniScripts / beeg-dl.py
blob0c890c8334e60f8dacc736e40d4ecebe141b5b5b
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 2013 Cool Dude 2k - http://idb.berlios.de/
13 Copyright 2013 Game Maker 2k - http://intdb.sourceforge.net/
14 Copyright 2013 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
16 $FileInfo: beeg-dl.py - Last Update: 05/11/2013 Ver. 1.0.5 RC 5 - Author: cooldude2k $
17 '''
19 import re, os, sys, httplib, urllib, urllib2, cookielib, StringIO, gzip, time, datetime, argparse;
21 parser = argparse.ArgumentParser();
22 parser.add_argument("url", help="beeg url");
23 getargs = parser.parse_args();
24 mlessvid = getargs.url;
25 mregex_text = re.escape("http://beeg.com/")+"([a-zA-Z0-9\/]+)";
26 if(re.findall(mregex_text, mlessvid)):
27 mlessvid = re.findall(mregex_text, mlessvid);
28 mlessvid = mlessvid[0];
29 fakeua = "Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0";
30 geturls_cj = cookielib.CookieJar();
31 geturls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(geturls_cj));
32 geturls_opener.addheaders = [("Referer", "http://beeg.com/section/long-videos/"), ("User-Agent", fakeua), ("Accept-Encoding", "gzip, deflate"), ("Accept-Language", "en-US,en-CA,en-GB,en-UK,en-AU,en-NZ,en-ZA,en;q=0.5"), ("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")];
33 geturls_text = geturls_opener.open("http://beeg.com/"+mlessvid);
34 if(geturls_text.info().get("Content-Encoding")=="gzip" or geturls_text.info().get("Content-Encoding")=="deflate"):
35 strbuf = StringIO.StringIO(geturls_text.read());
36 gzstrbuf = gzip.GzipFile(fileobj=strbuf);
37 out_text = gzstrbuf.read()[:];
38 if(geturls_text.info().get("Content-Encoding")!="gzip" and geturls_text.info().get("Content-Encoding")!="deflate"):
39 out_text = geturls_text.read()[:];
40 regex_text = re.escape("'file': '")+"(.*)"+re.escape("',");
41 post_text = re.findall(regex_text, out_text);
42 if(post_text>0):
43 mlesslink = post_text[0]+"?start=0";
44 print(mlesslink);
45 '''
46 getvidurls_cj = cookielib.CookieJar();
47 getvidurls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(getvidurls_cj));
48 getvidurls_opener.addheaders = [("Referer", "http://beeg.com/"+mlessvid), ("User-Agent", fakeua), ("Accept-Encoding", "gzip, deflate"), ("Accept-Language", "en-US,en-CA,en-GB,en-UK,en-AU,en-NZ,en-ZA,en;q=0.5"), ("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")];
49 getvidurls_text = getvidurls_opener.open(mlesslink);
50 def chunk_report(bytes_so_far, chunk_size, total_size):
51 percent = float(bytes_so_far) / total_size;
52 percent = round(percent*100, 2);
53 sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" %
54 (bytes_so_far, total_size, percent));
55 if bytes_so_far >= total_size:
56 sys.stdout.write("\n");
57 def chunk_read(response, chunk_size=8192, report_hook=None):
58 total_size = response.info().getheader("Content-Length").strip();
59 total_size = int(total_size);
60 bytes_so_far = 0;
61 while 1:
62 chunk = response.read(chunk_size);
63 bytes_so_far += len(chunk);
64 if not chunk:
65 break;
66 if report_hook:
67 report_hook(bytes_so_far, chunk_size, total_size);
68 return bytes_so_far;
69 chunk_read(getvidurls_text, report_hook=chunk_report);
70 vidfile = open(os.getcwd()+os.sep+os.path.basename(urllib2.urlparse.urlsplit(mlesslink)[2]), "wb");
71 vidfile.write(getvidurls_text.read());
72 vidfile.close();
73 '''