New config (Ignore download times: yes/no)
[enigma2-plugins.git] / rsdownloader / src / RSDownloader.py
1 ##\r
2 ## RS Downloader\r
3 ## by AliAbdul\r
4 ##\r
5 from base64 import encodestring\r
6 from twisted.internet import reactor\r
7 from twisted.web.client import HTTPDownloader\r
8 from urlparse import urlparse, urlunparse
9 import urllib2, re\r
10 \r
11 ##############################################################################\r
12 \r
13 def _parse(url):\r
14         url = url.strip()\r
15         parsed = urlparse(url)\r
16         scheme = parsed[0]\r
17         path = urlunparse(('','') + parsed[2:])\r
18         \r
19         host, port = parsed[1], 80\r
20         \r
21         if '@' in host:\r
22                 username, host = host.split('@')\r
23                 if ':' in username:\r
24                         username, password = username.split(':')\r
25                 else:\r
26                         password = ""\r
27         else:\r
28                 username = ""\r
29                 password = ""\r
30         \r
31         if ':' in host:\r
32                 host, port = host.split(':')\r
33                 port = int(port)\r
34         \r
35         if path == "":\r
36                 path = "/"\r
37         \r
38         return scheme, host, port, path, username, password\r
39 \r
40 def downloadPage(url, file, contextFactory=None, *args, **kwargs):\r
41         scheme, host, port, path, username, password = _parse(url)\r
42         \r
43         if username and password:\r
44                 url = scheme + '://' + host + ':' + str(port) + path\r
45                 basicAuth = encodestring("%s:%s" % (username, password))\r
46                 authHeader = "Basic " + basicAuth.strip()\r
47                 AuthHeaders = {"Authorization": authHeader}\r
48                 \r
49                 if kwargs.has_key("headers"):\r
50                         kwargs["headers"].update(AuthHeaders)\r
51                 else:\r
52                         kwargs["headers"] = AuthHeaders\r
53         \r
54         factory = HTTPDownloader(url, file, *args, **kwargs)\r
55         reactor.connectTCP(host, port, factory)\r
56         return factory.deferred
57 \r
58 ##############################################################################
59
60 def GET(url):
61         try:
62                 data = urllib2.urlopen(url)
63                 return data.read()
64         except:
65                 return ""
66    
67 def POST(url, data):
68         try:
69                 return urllib2.urlopen(url, data).read()
70         except:
71                 return ""
72
73 def matchGet(rex, string):
74         ret = False
75         
76         try:
77                 match = re.search(rex, string)
78                 if match:
79                         if len(match.groups()) == 0:
80                                 return string[match.span()[0]:match.span()[1]]
81                         if len(match.groups()) == 1:
82                                 return match.groups()[0]
83                 else:
84                         return match.groups()
85         except:
86                 pass
87         
88         return ret
89