Skip to content

Commit

Permalink
rutracker.org: update downloading metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
afedchin committed Jan 9, 2017
1 parent 3c7bc8e commit fddd0ff
Showing 1 changed file with 56 additions and 63 deletions.
119 changes: 56 additions & 63 deletions resources/site-packages/xbmctorrent/scrapers/rutracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@
# Cache TTLs
DEFAULT_TTL = 300 # 5 minutes

IGNORE = [u"Repack", u" PC |", u" РС", u" PC", u" PS", u"XBOX", u"RePack", u"FB2", u"TXT", u"DOC", u" MP3", u" JPG",
u" PNG", u" SCR", u"PDF"]
IGNORE = [u"Repack", u" PC |", u" РС", u" PC", u" PS", u"XBOX", u"RePack", u"FB2", u"TXT", u"DOC", u" MP3", u" JPG", u" PNG", u" SCR", u"PDF"]
IGNORE_EXT = [u".srt", u".ssa", u".txt", u".ac3", u".aac", u".jpg"]

_cookiejar = None


def udec(x):
return x.decode('utf-8')
Expand Down Expand Up @@ -176,7 +177,7 @@ def on_done(future):
[job.cancel() for job in jobs]
return
xbmc.sleep(100)
items = [job.result() for job in jobs]
items = [job.result() for job in jobs if not job.cancelled()]

for item in items:
del item["search"]
Expand Down Expand Up @@ -238,9 +239,7 @@ def rutracker_search_page(catind, page, search=None, search_id=None):
plugin.log.debug("Search id found: " + str(r.group(1)))
search_id = str(r.group(1))

nodes = soup.findAll("a", class_=["topictitle"])

for link in nodes:
for link in soup.findAll("a", class_=["topictitle"]):
try:
title = _rutracker_cleantitle(link.text)
r = topic_id_parser.search(link['href'])
Expand Down Expand Up @@ -286,7 +285,7 @@ def on_done(future):
[job.cancel() for job in jobs]
return
xbmc.sleep(100)
items = [job.result() for job in jobs]
items = [job.result() for job in jobs if not job.cancelled()]

for item in items:
del item["search"]
Expand Down Expand Up @@ -318,7 +317,6 @@ def rutracker_search(catind):
@plugin.route("/rutracker/play/pulsar/<tid>", name="rutracker_play_pulsar", options={"pulsar": "1"})
@ensure_fanart
def rutracker_play(tid, pulsar):
import cookielib
from contextlib import closing
from bencode import bdecode
from urlparse import urljoin
Expand All @@ -330,18 +328,8 @@ def rutracker_play(tid, pulsar):
dialog.create(plugin.name)
dialog.update(percent=0, line1="Получение информации о раздаче...")

torrent_url = urljoin("http://dl.rutracker.org/forum/", "dl.php?t=%s" % tid)
try:

plugin.log.debug("loading data from uri: " + torrent_url)
params = {"t": tid}

cookie = cookielib.Cookie(version=0, name='bb_dl', value=tid, port=None, port_specified=False,
domain='.rutracker.org', domain_specified=False, domain_initial_dot=False,
path='/', path_specified=True, secure=False, expires=None, discard=True,
comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)

data = url_get(torrent_url, headers=HEADERS, post=params, cookie=cookie)
data = url_get(urljoin(BASE_URL, "dl.php?t=%s" % tid))
metadata = bdecode(data)
except:
plugin.log.error("Unexpected error: %s " % format_exc().split('\n')[-2])
Expand Down Expand Up @@ -408,70 +396,75 @@ def _rutracker_cleantitle(title):
return title.strip()


def _rutracker_login(_cookiejar):
def get_cookie_jar():
global _cookiejar
if not _cookiejar:
import os
import xbmcvfs
import cookielib

sid_file = os.path.join(xbmc.translatePath('special://temp/'), plugin.id, 'RuTracker.cookies.dat')
if not xbmcvfs.exists(os.path.dirname(sid_file)):
xbmcvfs.mkdir(os.path.dirname(sid_file))

_cookiejar = cookielib.MozillaCookieJar(sid_file)

if not xbmcvfs.exists(sid_file):
_cookiejar.save()

_cookiejar.load()
return _cookiejar


def _rutracker_login(cookie_handler):
from urlparse import urljoin
from xbmctorrent.utils import url_get as url_get_origin
import urllib2

values = {
"login_username": plugin.get_setting("rutracker_login"),
"login_password": plugin.get_setting("rutracker_passw"),
'login': 'Вход'
"login": "Вход"
}

plugin.log.debug("Login user")
HEADERS["Referer"] = LOGIN_URL
HEADERS["Content-Type"] = "application/x-www-form-urlencoded"

cookie_handler = urllib2.HTTPCookieProcessor(_cookiejar)
headers = HEADERS.copy()
headers["Referer"] = urljoin(BASE_URL, "index.php")
headers["Content-Type"] = "application/x-www-form-urlencoded"

html = url_get_origin(LOGIN_URL, post=values, headers=HEADERS, handlers=[cookie_handler])
html = url_get_origin(urljoin(BASE_URL, "login.php"), post=values, headers=headers, handlers=[cookie_handler])
if html.find("profile.php?mode=sendpassword") >= 0:
plugin.notify("Проверьте настройки авторизации.", delay=15000)
else:
plugin.log.debug("Login sucessfull")
plugin.log.debug("login sucessfull.")
return True
_cookiejar.save()
HEADERS["Referer"] = BASE_URL # restore referer

return False


def url_get(url, params={}, headers={}, post=None, cookie=None):
global _cookiejar
import xbmcvfs
import urllib2
def url_get(url, params={}, headers={}, post=None):
from urllib2 import HTTPCookieProcessor, URLError
from xbmctorrent.utils import url_get as url_get_origin

if not _cookiejar:
import os
import xbmc
import cookielib
_cookiejar = get_cookie_jar()
cookie_handler = HTTPCookieProcessor(_cookiejar)

sid_file = os.path.join(xbmc.translatePath('special://temp/'), plugin.id, 'RuTracker.cookies.dat') #
if not xbmcvfs.exists(os.path.dirname(sid_file)):
xbmcvfs.mkdir(os.path.dirname(sid_file))
plugin.log.debug("loading uri: %s with cookies %s params %s post %s" % (url, _cookiejar, str(params), str(post)))

_cookiejar = cookielib.MozillaCookieJar(sid_file)
try:
result = url_get_origin(url, params, headers, post, handlers=[cookie_handler])
except URLError:
result = "profile.php?mode=sendpassword"
pass

if not xbmcvfs.exists(sid_file):
_cookiejar.save()

_cookiejar.load()

if cookie:
_cookiejar.set_cookie(cookie)
cookie_handler = urllib2.HTTPCookieProcessor(_cookiejar)
plugin.log.debug("Loading uri: %s with cookies %s params %s post %s" % (url, _cookiejar, str(params), str(post)))
result = url_get_origin(url, params, headers, post, handlers=[cookie_handler])
_cookiejar.save()
plugin.log.debug("Uri loaded: " + url)

if result.find("profile.php?mode=sendpassword") >= 0:
if not plugin.get_setting("rutracker_login") or not plugin.get_setting("rutracker_passwd"):
if url.find("dl.php?") >= 0 and result.find("profile.php?mode=sendpassword") >= 0:
if not plugin.get_setting("rutracker_login") or not plugin.get_setting("rutracker_passw"):
plugin.notify("Проверьте настройки авторизации.", delay=15000)
if url.find("dl.php?") == 0: # return page content in any case
return result
if _rutracker_login(_cookiejar):
result = url_get_origin(url, params, headers, post, handlers=[cookie_handler])
_cookiejar.save()
return ""
if not _rutracker_login(cookie_handler):
return ""

_cookiejar.save()
result = url_get_origin(url, params, headers, post, handlers=[cookie_handler])
pass

plugin.log.debug("uri loaded: " + url)
return result

0 comments on commit fddd0ff

Please sign in to comment.