nya =3
[archive.git] / Apkawa / for_torrents.ru / torrents.ru.py
blobe2eb22ac1481497194e947dddb72c6ce485f81de
1 import urllib2
2 import urllib2_file
3 import httplib
4 import torrent_parser
5 import BeautifulSoup
6 #from .. import spamavert.com
8 def get_src( url, post = None, headers=None, noredirect=False):
10 class NoRedirect( urllib2.HTTPRedirectHandler ):
11 def http_error_302(self, req, fp, code, msg, headers):
12 pass
14 httplib.HTTPConnection.debuglevel = 0
15 request = urllib2.Request(url, post, unverifiable=True)
16 request.add_header('Accept-encoding', 'gzip')
17 request.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8')
18 if headers:
19 for key, val in headers.items():
20 request.add_header(key, val)
21 opener = urllib2.build_opener(NoRedirect)
22 try:
23 f = opener.open(request)
24 except urllib2.HTTPError, e:
25 f = e
26 mode = f.headers.get('Content-Encoding')
27 print mode
28 src = f.read()
29 print len(src)
30 if mode:
31 from StringIO import StringIO
32 compressstream = StringIO( src)
33 import gzip
34 src = gzip.GzipFile(fileobj= compressstream ).read()
35 print len(src)
36 else:
37 pass
38 return type('Response',(),{'headers' :f.headers.dict, 'src': src})
40 class Torrents:
41 def __init__(self):
42 self.cookie = None
43 self.site = 'http://torrents.ru/forum/'
46 pass
47 def auth(self, login, password):
48 stage_1 = get_src(self.site+'login.php')
49 temp_cookie = stage_1.headers.get('set-cookie')
50 send_auth_form = {
51 'redirect':'index.php',
52 'cookie_test':temp_cookie.split('=')[1],
53 'login_username':login,
54 'login_password':password,
55 'autologin':'on',
56 'login':'',
58 stage_2 = get_src('http://torrents.ru/forum/login.php', send_auth_form, {'Cookie':temp_cookie})
59 self.cookie = stage_2.headers.get('set-cookie')
60 #print self.cookie
63 def soup_load(self, src, fromEncoding='CP1251'):
64 src2 = re.sub('<script(|.*?)>[\S\s]*?</script>','',src)
65 return BeautifulSoup.BeautifulStoneSoup(src2, fromEncoding=fromEncoding)
67 def find_torrents(self, word=''):
68 'find in tracker'
69 search_form = {
70 'prev_allw':'0',
71 'prev_my':'0',
72 'prev_new':'0',
73 'prev_oop':'1',
74 'prev_da':'0',
75 'prev_df':'0',
76 'prev_ds':'0',
77 'f[]':'-1',
78 'o':'10',
79 's':'1',
80 'tm':'-1',
81 'oop':'1',
82 'pn':'',
83 'nm':'',
84 'submit':'',
86 src = get_src(self.site+'tracker.php', search_form, {'Cookie':self.cookie})
87 #print src.src
88 soup = self.soup_load( src.src)
89 list_topic = []
90 for t in soup.findAll('tr', {'class':"tCenter"}):
91 theme_link= t.find('a', {'class':"genmed tLink"})
92 torrent_link = t.find('a' ,{'class':"small tr-dl dl-stub"})
93 if theme_link and torrent_link:
94 list_topic.append( (self.site+theme_link.get('href')[2:], self.site+torrent_link.get('href')) )
95 return list_topic
97 def get_torrents_info_from_page(self, url):
98 'seeds (Nick, profile), leech, url_torrent_file,'
99 src = get_src( url+'&spmode=full',headers={'Cookie': self.cookie} )
100 soup = self.soup_load( src.src)
101 for s in soup.findAll('a',{'class':'seedmed'} ):
102 name = s.find(text=True)
103 profile = s.get('href')
104 print name,self.site+profile
106 for s in soup.findAll('a',{'class':'leechmed'} ):
107 name = s.find(text=True)
108 profile = s.get('href')
109 print name,self.site+profile
111 pass
113 def load_torrent(self, url=''):
114 'info_hash'
115 pass
118 class PasskeyCheck:
119 def __init__(self):
120 self.cookie = None
122 pass
123 def __load_torrent(self):
124 pass
126 def auth(self, user, password):
127 pass
128 def __parse():
129 pass
131 from tempfile import _RandomNameSequence as RandName
132 import re
133 def reg(usersyffix='nyaka_2009_1',password='qazqaz'):
134 email = RandName().next()+'@spamavert.com'
136 request = urllib2.Request('http://torrents.ru/forum/profile.php?mode=register', {'reg_agreed':'1'})
137 #request.add_header('Accept-encoding', 'gzip')
138 request.add_header('Cookie','spylog_test=1')
139 request.add_header('Referer','http://torrents.ru/forum/profile.php?mode=register')
140 opener = urllib2.build_opener()
141 f = opener.open(request)
142 src = f.read()
143 print f.headers.dict
144 #print src
145 captcha_id = re.findall('<input type="hidden" name="confirm_id" value="(.*?)" />', src)[0]
146 reg_form = {
147 'mode': 'register',
148 'reg_agreed':'1',
149 'sid':'',
150 'confirm_id': captcha_id ,
151 'username': 'nyaka_1004',
152 'email': email,
153 'new_password': password,
154 'password_confirm': password,
155 'cfmcd' : 'yg1q', #captcha
156 'user_flag_id': '195',
157 'user_timezone_x2': '6',
159 captcha_img = 'http://torrents.ru/forum/profile.php?mode=confirm&id='+captcha_id
160 print reg_form
161 print captcha_img
162 if __name__ == '__main__':
163 #bot = Torrents()
164 #bot.auth('nyaka_1002','qazqaz')
165 #bot.find_torrents()
166 import os
167 #Unix
168 a = os.popen('python captcha.py http://www.google.ru/images/nav_logo4.png').read()
169 print a[:-1]
170 #bot.get_torrents_info_from_page('http://torrents.ru/forum/viewtopic.php?t=200486')
172 #reg()
175 #print src