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'
23 options
, files
= self
.optparse(argv
)
26 self
.email
= options
.email
27 self
.passwd
= options
.passwd
29 #self.guest = self.email and 0 or 1
31 if self
.email
: self
.guest
= 0
34 self
.tmp
= TemporaryFile(prefix
='rghost_')
36 def optparse(self
, argv
):
37 parser
= optparse
.OptionParser()
38 parser
.add_option('-u','--user', action
='store', type='string', dest
='email')
39 parser
.add_option('-p','--password', action
='store', type='string', dest
='passwd')
40 return parser
.parse_args(argv
)
47 print '\n',self
.tmp
.read()
51 s
= urllib
.urlopen(URL
)
52 self
.s_cookie
= s
.headers
.getheader('Set-Cookie')
53 self
.s_token
= findall('input name="authenticity_token" type="hidden" value="(.*?)"',s
.read())[0]
57 'authenticity_token': self
.s_token
,
60 'password':self
.passwd
,
64 conn
= httplib
.HTTPConnection( 'rghost.ru' )
65 conn
.request('POST','/profile/login', urllib
.urlencode(values
) , {'Cookie':self
.s_cookie
})
66 respone
= conn
.getresponse()
67 self
.s_cookie
= respone
.getheader('set-cookie')
70 t = urllib.FancyURLopener()
71 t.addheader('Cookie',self.s_cookie)
73 login = t.open(URL+'profile/login',urllib.urlencode(values))
75 #self.s_cookie = login.headers.getheader('Set-Cookie')
76 #self.s_token = findall('input name="authenticity_token" type="hidden" value="(.*?)"',login.read())[0]
79 def upload(self
,filepath
):
80 tsize
= float(os
.stat(filepath
).st_size
)
83 size
= '%3.2f%s'%((tsize
/1024)/1024 , 'M')
85 size
='%3.2f%s'%( tsize
/1024 , 'K')
88 filename
= os
.path
.split(filepath
)[1]
90 self
.tmp
.write('%s%s %s %s'%\
91 (URL
,findall('"http://phonon.rghost.ru/(.*?)"', os
.popen(self
.curl
%( self
.s_cookie
, self
.s_token
, filepath
)).read())[0],
95 if __name__
== '__main__':