Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #import sys
- #import logging, traceback
- import urllib, Cookie
- from urlparse import urlparse
- import wsgiref.handlers
- from google.appengine.api import mail
- from google.appengine.api import memcache
- from google.appengine.api import urlfetch
- from google.appengine.ext import webapp
- class GetTorrentFile(webapp.RequestHandler):
- def get(self):
- url = self.request.get('url')
- id = dict([part.split('=') for part in urlparse(url)[4].split('&')])['t']
- content = memcache.get(id)
- if content is not None:
- self.response.out.write(content)
- return
- params = urllib.urlencode({'login_username': 'XXX', 'login_password': 'YYY', 'login': 'Вход'})
- loginUrl = "http://login.rutracker.org/forum/login.php"
- torrentUrl = "http://dl.rutracker.org/forum/dl.php?t=" + id
- headers = {'User-Agent': "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"}
- cookie = Cookie.SimpleCookie()
- # login
- response = urlfetch.fetch(loginUrl, payload = params, method = urlfetch.POST, headers = headers, follow_redirects = False, deadline = 20)
- cookie.load(response.headers.get('set-cookie', ''))
- # fetch torrent file
- cookie['bb_dl'] = id
- headers['Cookie'] = cookie.output(attrs = [], header = "", sep = ";")
- response = urlfetch.fetch(torrentUrl, headers = headers, deadline = 20)
- content = response.content
- memcache.add(id, content, 4 * 60 * 60) # cache for 4 hours
- self.response.out.write(content)
- application = webapp.WSGIApplication([('.*', GetTorrentFile)], debug=True)
- wsgiref.handlers.CGIHandler().run(application)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement