updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / smartsvn / downloader.py
blob142e20bbaae5f11779a5c25619d2e36e44794836
1 #!/bin/environment python3
3 """
4 A short hacked script to download smartsvn.
5 Copyright (C) 2011 Peinthor Rene
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """
20 import sys
21 import http.client, urllib, urllib.request
22 import xml.dom.minidom
23 import subprocess
25 def wellform(xhtmldata):
26 #call htmltidy and repair the html, might be broken
27 ptidy = subprocess.Popen(['/usr/bin/tidy', '-q', '-asxml'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
28 (stdoutdata, stderrdata) = ptidy.communicate(xhtmldata)
29 return stdoutdata.decode('UTF-8')
32 class Downloader():
33 filename = ''
34 host = 'www.syntevo.com'
35 path = '/smartsvn/download.html'
36 header = {}
38 def __init__(self, filename):
39 self.filename = filename
41 def suck(self):
42 conn = http.client.HTTPConnection(self.host, timeout=5)
44 refer = "http://" + self.host + self.path + '?file=smartsvn/' + self.filename
45 conn.request("GET", self.path + '?file=smartsvn/' + self.filename)
46 response = conn.getresponse()
47 #get our cookie
48 cookie = response.getheader('Set-Cookie')[:response.getheader('Set-Cookie').find(';')] + ';'
50 self.header["Cookie"] = cookie
51 self.header["Referer"] = refer
53 conn = http.client.HTTPConnection(self.host, timeout=5)
54 #redo request with cookie
55 conn.request("GET", self.path + '?file=smartsvn/' + self.filename, headers=self.header)
56 response = conn.getresponse()
57 xmldata = response.read()
58 #wellform
59 xhtmldata = wellform(xmldata)
60 dom = xml.dom.minidom.parseString(xhtmldata)
61 formElem = dom.getElementsByTagName("form")[1]
62 action = formElem.getAttribute('action')[2:]
63 idvar = ''
64 for inputElem in formElem.getElementsByTagName("input"):
65 if inputElem.getAttribute('type') == 'hidden':
66 idvar = inputElem.getAttribute('name')
68 conn.close()
70 data = { idvar: '', 'accept': 'on' }
71 data = urllib.parse.urlencode(data)
72 header = self.header.copy()
73 header["Content-type"] = "application/x-www-form-urlencoded"
74 conn = http.client.HTTPConnection(self.host, timeout=5)
75 conn.request("POST", action, data, header)
76 response = conn.getresponse()
77 if response.status == 302:
78 conn = http.client.HTTPConnection(self.host, timeout=5)
79 #redo request with cookie
80 conn.request("GET", response.getheader('Location'), headers=self.header)
81 response = conn.getresponse()
82 xhtmldata = wellform(response.read())
83 dom = xml.dom.minidom.parseString(xhtmldata)
84 meta_refresh = dom.getElementsByTagName("meta")[2]
85 content = meta_refresh.getAttribute('content')
86 fileurl = content[len("1; URL="):]
88 print('Downloading ' + self.filename )
89 url = 'http://' + self.host + fileurl
90 urllib.request.urlretrieve( url, self.filename)
91 print('Finished')
92 else:
93 print( 'Not moved :(')
94 conn.close()
96 if __name__ == '__main__':
97 downloader = Downloader(sys.argv[1])
98 sys.exit(downloader.suck())