Small update
[Python-Scripts.git] / MiniScripts / beeg-dl.py
blobd6cabd9b347872cc3da4e3af57b3bf49de8ed48b
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: 10/07/2013 Ver. 1.0.5 RC 6 - Author: cooldude2k $
17 '''
19 import re
20 import os
21 import sys
22 import urllib
23 import urllib2
24 import cookielib
25 import StringIO
26 import gzip
27 import time
28 import datetime
29 import argparse
30 import urlparse
32 parser = argparse.ArgumentParser()
33 parser.add_argument("url", help="beeg url")
34 getargs = parser.parse_args()
35 mlessvid = getargs.url
36 mregex_text = re.escape("http://beeg.com/")+"([a-zA-Z0-9\/]+)"
37 if(re.findall(mregex_text, mlessvid)):
38 mlessvid = re.findall(mregex_text, mlessvid)
39 mlessvid = mlessvid[0]
40 fakeua = "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0""
41 geturls_cj = cookielib.CookieJar()
42 geturls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(geturls_cj))
43 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"),
44 ("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")]
45 geturls_text = geturls_opener.open("http://beeg.com/"+mlessvid)
46 if(geturls_text.info().get("Content-Encoding") == "gzip" or geturls_text.info().get("Content-Encoding") == "deflate"):
47 strbuf = StringIO.StringIO(geturls_text.read())
48 gzstrbuf = gzip.GzipFile(fileobj=strbuf)
49 out_text = gzstrbuf.read()[:]
50 if(geturls_text.info().get("Content-Encoding") != "gzip" and geturls_text.info().get("Content-Encoding") != "deflate"):
51 out_text = geturls_text.read()[:]
52 regex_text = re.escape("'file': '")+"(.*)"+re.escape("',")
53 post_text = re.findall(regex_text, out_text)
54 if(post_text > 0):
55 mlesslink = post_text[0]+"?start=0"
56 print(mlesslink)
57 '''
58 getvidurls_cj = cookielib.CookieJar();
59 getvidurls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(getvidurls_cj));
60 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")];
61 getvidurls_text = getvidurls_opener.open(mlesslink);
62 def chunk_report(bytes_so_far, chunk_size, total_size):
63 percent = float(bytes_so_far) / total_size;
64 percent = round(percent*100, 2);
65 sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" %
66 (bytes_so_far, total_size, percent));
67 if bytes_so_far >= total_size:
68 sys.stdout.write("\n");
69 def chunk_read(response, chunk_size=8192, report_hook=None):
70 total_size = response.info().getheader("Content-Length").strip();
71 total_size = int(total_size);
72 bytes_so_far = 0;
73 while 1:
74 chunk = response.read(chunk_size);
75 bytes_so_far += len(chunk);
76 if not chunk:
77 break;
78 if report_hook:
79 report_hook(bytes_so_far, chunk_size, total_size);
80 return bytes_so_far;
81 chunk_read(getvidurls_text, report_hook=chunk_report);
82 vidfile = open(os.getcwd()+os.sep+os.path.basename(urllib2.urlparse.urlsplit(mlesslink)[2]), "wb");
83 vidfile.write(getvidurls_text.read());
84 vidfile.close();
85 '''