Skip to content

Commit

Permalink
less manual work with cookies - moved rt_browser to urllib and cookielib
Browse files Browse the repository at this point in the history
  • Loading branch information
serejkus committed Dec 18, 2012
1 parent 5488ce1 commit 0777430
Showing 1 changed file with 31 additions and 76 deletions.
107 changes: 31 additions & 76 deletions rt_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
It allows getting .torrent files from RuTracker.Org.'''

import httplib
import cookielib
import re
import urllib
import urlparse
import urllib2


class RutrackerBrowserError(RuntimeError):
Expand All @@ -31,74 +31,61 @@ def __init__(self, login, password):

self._login = login
self._password = password
self._cookie_auth = None
self._cookie_jar = cookielib.CookieJar()
self._url_opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self._cookie_jar)
)


def login(self):
'''Logs in RuTracker.Org.
This method should be called before any attempts to get .torrent files.'''
This method should be called before any attempts to get some
.torrent files.'''

data = {
'login_username': self._login,
'login_password': self._password
'login_password': self._password,
'login' : '%C2%F5%EE%E4'
}
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Firefox/17.0',
'Referer': 'http://rutracker.org/forum/index.php',
'Content-Type': 'application/x-www-form-urlencoded'
}
body = urllib.urlencode(data) + '&login=%C2%F5%EE%E4'

conn = httplib.HTTPConnection('login.rutracker.org')
#conn.set_debuglevel(2)
conn.request('POST', '/forum/login.php', body=body, headers=headers)
resp = conn.getresponse()
conn.close()
if resp.status != 302 and not resp.getheader('Set-Cookie', None):
raise RutrackerBrowserError('could not login: %s' % resp.read())
resp = self._url_opener.open(
'http://login.rutracker.org/forum/login.php',
data=urllib.urlencode(data)
)

self._cookie_auth = self._get_auth_cookie(resp, 'bb_data')
if not resp:
raise RutrackerBrowserError('could not login')
if 'login.php' in resp.geturl():
raise RutrackerBrowserError('could not login: %s\n\n%s' %
(resp.geturl(), resp.read()))


@staticmethod
def check_topic_url(topic_url):
'''Raises if topic_url is not valid.'''
'''Raises exception if topic_url is not valid.'''

match = RutrackerBrowser._topic_re.search(topic_url)
if not match:
mess = "'%s' is not a valid topic url" % topic_url
raise RutrackerBrowserError(mess)


def open_topic_url(self, topic_url):
'''Returns filelike object on succesfull topic getting.
topic_url - url with viewtopic.php?t=3656915.'''
topic_url - url with viewtopic.php?t=3656915.
Not necessary method.'''

self.check_topic_url(topic_url)

headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Firefox/17.0',
'Referer': 'http://rutracker.org/forum/index.php',
'Cookie' : self._cookie_auth

}
parsed_url = urlparse.urlparse(topic_url)

conn = httplib.HTTPConnection('rutracker.org')
#conn.set_debuglevel(2)
path = '?'.join((parsed_url.path, parsed_url.query))
conn.request('GET', path, headers=headers)
req = urllib2.Request(topic_url)
req.add_header('Referer', 'http://rutracker.org/forum/index.php')

res = conn.getresponse()
conn.close()

if res.status != 200:
mess = "Could not open url \'%s\': status %d" % (topic_url, res.status)
raise RutrackerBrowserError(mess)
res = self._url_opener.open(req)

return res


def get_torrent(self, topic_url):
'''Returns filelike object with .torrent as its contents.
Expand All @@ -114,46 +101,14 @@ def get_torrent(self, topic_url):
match = RutrackerBrowser._topic_re.search(topic_url)
topic_id = match.group(1)

cookie_dl = '%s; bb_dl=%s' % (self._cookie_auth, topic_id)
dl_url = 'http://dl.rutracker.org/forum/dl.php?t=%s' % topic_id
req = urllib2.Request(dl_url)
req.add_header('Referer', topic_url)

headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Firefox/17.0',
'Referer': topic_url,
'Cookie': cookie_dl
}

conn = httplib.HTTPConnection('dl.rutracker.org')
#conn.set_debuglevel(2)
conn.request('GET', '/forum/dl.php?t=%s' % topic_id, headers=headers)

res = conn.getresponse()
#conn.close()

if res.status == 302:
mess = "Could not get torrent from \'%s\': status 302: %s" % (topic_url, res.getheader('Location'))
raise RutrackerBrowserError(mess)
elif res.status != 200:
mess = "Could not get torrent from \'%s\': status %d" % (topic_url, res.status)
raise RutrackerBrowserError(mess)
res = self._url_opener.open(req)

return res

@staticmethod
def _get_auth_cookie(resp, name):
'''Returns cookie in format name=value from response.
resp - response after HTTPConnection made request;
name - name of a cookie to extract.'''

cookie = resp.getheader('Set-Cookie')
start = cookie.find(name)
if start == -1:
raise RutrackerBrowserError("no '%s' cookie. login failed" % name)
finish = cookie.find(';', start)
if (finish == -1):
finish = len(cookie)

return cookie[start:finish]

#example
#if __name__ == '__main__':
Expand Down

0 comments on commit 0777430

Please sign in to comment.