добавляю переписаную с нуля 2ю версию заливальщика файлов.
[archive.git] / Apkawa / rghost-0.02.py
bloba72833d721097602355fa2b517147b4674d3c213
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 '''
4 Консольная заливка на rghost.ru
5 '''
6 import urllib, os, tempfile, httplib
7 import optparse
8 from re import findall
9 from sys import argv
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/'
16 class Rghost:
17 def __init__(self, argv):
18 self.curl = 'curl -b "%s" \
19 -F authenticity_token=%s \
20 -F file=@%s \
21 -F commit="Отправить" http://phonon.rghost.ru/files'
23 options, files = self.optparse(argv)
24 #print options, files
25 self.files = files
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
32 else: self.guest = 1
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)
42 def main(self):
43 for f in self.files:
44 self.upload(f)
46 self.tmp.seek(0)
47 print '\n',self.tmp.read()
50 def session(self):
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]
55 if not self.guest:
56 values = {
57 'authenticity_token': self.s_token,
58 'return_to':URL,
59 'email':self.email,
60 'password':self.passwd,
61 'commit':'Войти',
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')
69 '''
70 t = urllib.FancyURLopener()
71 t.addheader('Cookie',self.s_cookie)
73 login = t.open(URL+'profile/login',urllib.urlencode(values))
74 print login.info()
75 #self.s_cookie = login.headers.getheader('Set-Cookie')
76 #self.s_token = findall('input name="authenticity_token" type="hidden" value="(.*?)"',login.read())[0]
77 '''
79 def upload(self,filepath):
80 tsize = float(os.stat(filepath).st_size)
81 if tsize > 1024:
82 if tsize/1024 > 1024:
83 size = '%3.2f%s'%((tsize/1024)/1024 , 'M')
84 else:
85 size ='%3.2f%s'%( tsize/1024 , 'K')
86 else:
87 size ='%3.0f'% tsize
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],
92 filename,
93 size))
95 if __name__ == '__main__':
96 rg = Rghost(argv[1:])
97 rg.session()
98 rg.main()