2 # -*- coding: utf-8 -*-
4 Консольная заливка на rghost.ru
6 import urllib
, os
, tempfile
, httplib
10 from tempfile
import TemporaryFile
13 USER_AGENT
= 'Mozilla/5.0 (X11; U; Linux i686; ru-RU; rv:1.9.0.2pre) Gecko/2008072703 Firefox/3.0.2pre (Swiftfox)'
14 URL
= 'http://rghost.ru/'
17 def __init__(self
, argv
):
18 self
.curl
= 'curl -b "%s" \
19 -F authenticity_token=%s \
21 -F commit="Отправить" http://phonon.rghost.ru/files'
22 options
, files
= self
.optparse(argv
)
25 self
.email
= options
.email
26 self
.passwd
= options
.passwd
28 #self.guest = self.email and 0 or 1
30 if self
.email
: self
.guest
= 0
33 self
.tmp
= TemporaryFile(prefix
='rghost_')
35 def optparse(self
, argv
):
36 parser
= optparse
.OptionParser()
37 parser
.add_option('-u','--user', action
='store', type='string', dest
='email')
38 parser
.add_option('-p','--password', action
='store', type='string', dest
='passwd')
39 return parser
.parse_args(argv
)
46 print '\n',self
.tmp
.read()
50 s
= urllib
.urlopen(URL
)
51 self
.s_cookie
= s
.headers
.getheader('Set-Cookie')
52 self
.s_token
= findall('input name="authenticity_token" type="hidden" value="(.*?)"',s
.read())[0]
56 'authenticity_token': self
.s_token
,
59 'password':self
.passwd
,
63 conn
= httplib
.HTTPConnection( 'rghost.ru' )
64 conn
.request('POST','/profile/login', urllib
.urlencode(values
) , {'Cookie':self
.s_cookie
})
65 respone
= conn
.getresponse()
66 self
.s_cookie
= respone
.getheader('set-cookie')
69 t = urllib.FancyURLopener()
70 t.addheader('Cookie',self.s_cookie)
71 login = t.open(URL+'profile/login',urllib.urlencode(values))
73 #self.s_cookie = login.headers.getheader('Set-Cookie')
74 #self.s_token = findall('input name="authenticity_token" type="hidden" value="(.*?)"',login.read())[0]
77 def upload(self
,filepath
):
78 tsize
= float(os
.stat(filepath
).st_size
)
81 size
= '%3.2f%s'%((tsize
/1024)/1024 , 'M')
83 size
='%3.2f%s'%( tsize
/1024 , 'K')
86 filename
= os
.path
.split(filepath
)[1]
90 #print os.popen(self.curl%( self.s_cookie, self.s_token, filepath)).read()
91 curl_cmd
= self
.curl
%( self
.s_cookie
, self
.s_token
, filepath
)
94 self
.tmp
.write('%s%s %s %s\n'%\
95 (URL
,findall('"http://rghost.ru/(.*?)"', os
.popen( curl_cmd
).read())[0],
99 if __name__
== '__main__':
100 rg
= Rghost(argv
[1:])