updated on Wed Jan 25 00:20:47 UTC 2012
[aur-mirror.git] / smartgit-ea / downloader.py
blob9ad241b9d8bae2737d95371ee5d6e6ebd17f48c0
1 #!/bin/environment python3
3 """
4 A short hacked script to download syntevo products.
5 Copyright (C) 2011 Peinthor Rene
6 Adopted for smartgit download by Alexey Stukalov
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21 import os.path
22 import sys
23 import re
24 import http.client, urllib, urllib.request
26 class Downloader():
27 host = 'www.syntevo.com'
28 filename = ''
29 download_request = ''
30 header = {}
32 def __init__(self, request_path, file_param):
33 self.filename = os.path.basename(file_param)
34 sys.stderr.write( 'Initiating ' + self.filename + ' download from ' + self.host + '...\n' )
35 self.download_request = '/' + request_path + '?file=' + file_param
37 def suck(self):
38 sys.stderr.write( 'Connecting to ' + self.host + '...\n' )
39 conn = http.client.HTTPConnection(self.host, timeout=5)
41 refer = "http://" + self.host + self.download_request
42 sys.stderr.write( 'Request URL: ' + refer + '\n' )
43 conn.request("GET", self.download_request)
44 response = conn.getresponse()
45 #get our cookie
46 cookie = response.getheader('Set-Cookie')[:response.getheader('Set-Cookie').find(';')] + ';'
48 self.header["Cookie"] = cookie
49 self.header["Referer"] = refer
51 conn = http.client.HTTPConnection(self.host, timeout=5)
52 #redo request with cookie
53 conn.request("GET", self.download_request, headers=self.header)
54 response = conn.getresponse()
55 htmlstring = response.read().decode('UTF-8')
56 conn.close()
58 sys.stderr.write( 'Agreeing with the license...\n' )
59 form_match = re.search( '\<form action\="\.\.(\/\?[^"]+)" .+\<input type\="hidden" name\="([^"]+)"',
60 htmlstring, re.MULTILINE + re.DOTALL )
61 if not form_match:
62 raise RuntimeError( 'Error parsing license agreement html\n' )
63 action = form_match.group(1)
64 idvar = form_match.group(2)
65 data = { idvar: '', 'accept': 'on' }
66 data = urllib.parse.urlencode(data)
67 header = self.header.copy()
68 header["Content-type"] = "application/x-www-form-urlencoded"
69 conn = http.client.HTTPConnection(self.host, timeout=5)
70 conn.request("POST", action, data, header)
72 sys.stderr.write( 'Getting file URL...\n' )
73 response = conn.getresponse()
74 if response.status == 302:
75 conn = http.client.HTTPConnection(self.host, timeout=5)
76 #redo request with cookie
77 conn.request("GET", response.getheader('Location'), headers=self.header)
78 response = conn.getresponse()
79 htmlstring = response.read().decode('UTF-8')
80 conn.close()
82 meta_match = re.search( '\<meta http-equiv="refresh" content="1\; URL\=([^"]+)"',
83 htmlstring, re.MULTILINE + re.DOTALL )
84 if not meta_match:
85 raise RuntimeError( 'Error parsing file download location\n' )
86 fileurl = meta_match.group(1)
88 sys.stderr.write('Downloading ' + self.filename + '...\n' )
89 urllib.request.urlretrieve( 'http://' + self.host + fileurl, self.filename)
90 sys.stderr.write('Download finished\n')
91 else:
92 sys.stderr.write( 'Not moved :(\n')
93 conn.close()
95 if __name__ == '__main__':
96 downloader = Downloader(sys.argv[1],sys.argv[2])
97 sys.exit(downloader.suck())