added pygooglesearch.py and commit modify
[archive.git] / Apkawa / rghost-0.02.py
blob84b434dc7e402d5b447d167d9df8b857b9d3db3e
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'
22 options, files = self.optparse(argv)
23 #print options, files
24 self.files = files
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
31 else: self.guest = 1
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)
41 def main(self):
42 for f in self.files:
43 self.upload(f)
45 self.tmp.seek(0)
46 print '\n',self.tmp.read()
49 def session(self):
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]
54 if not self.guest:
55 values = {
56 'authenticity_token': self.s_token,
57 'return_to':URL,
58 'email':self.email,
59 'password':self.passwd,
60 'commit':'Войти',
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')
68 '''
69 t = urllib.FancyURLopener()
70 t.addheader('Cookie',self.s_cookie)
71 login = t.open(URL+'profile/login',urllib.urlencode(values))
72 print login.info()
73 #self.s_cookie = login.headers.getheader('Set-Cookie')
74 #self.s_token = findall('input name="authenticity_token" type="hidden" value="(.*?)"',login.read())[0]
75 '''
77 def upload(self,filepath):
78 tsize = float(os.stat(filepath).st_size)
79 if tsize > 1024:
80 if tsize/1024 > 1024:
81 size = '%3.2f%s'%((tsize/1024)/1024 , 'M')
82 else:
83 size ='%3.2f%s'%( tsize/1024 , 'K')
84 else:
85 size ='%3.0f'% tsize
86 filename = os.path.split(filepath)[1]
87 #print self.s_cookie
88 #print self.s_token
89 #print filepath
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)
92 #print curl_cmd
94 self.tmp.write('%s%s %s %s\n'%\
95 (URL,findall('"http://rghost.ru/(.*?)"', os.popen( curl_cmd ).read())[0],
96 filename,
97 size))
99 if __name__ == '__main__':
100 rg = Rghost(argv[1:])
101 rg.session()
102 rg.main()