Skip to content

Commit

Permalink
Code Cleaning, fixed a very small bug, better provider config options
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-orange committed Nov 29, 2011
1 parent d437878 commit ca6bb37
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 35 deletions.
24 changes: 23 additions & 1 deletion data/interfaces/default/config_providers.tmpl
Expand Up @@ -77,7 +77,7 @@ var show_nzb_providers = #if $sickbeard.USE_NZBS then "true" else "false"#;
<span class="component-title jumbo">Configure Provider:</span>
<span class="component-desc">
#set $provider_config_list = []
#for $cur_provider in ("nzbs_org", "nzbs_r_us", "newzbin", "nzbmatrix", "tvtorrents", "thepiratebay"):
#for $cur_provider in ("nzbs_org", "nzbs_r_us", "newzbin", "nzbmatrix", "thepiratebay", "tvtorrents"):
#set $cur_provider_obj = $sickbeard.providers.getProviderClass($cur_provider)
#if $cur_provider_obj.providerType == $GenericProvider.NZB and not $sickbeard.USE_NZBS:
#continue
Expand Down Expand Up @@ -196,6 +196,28 @@ var show_nzb_providers = #if $sickbeard.USE_NZBS then "true" else "false"#;
</div>

<div class="providerDiv" id="thepiratebayDiv">
<div class="field-pair">
<input type="checkbox" class="enabler" name="thepiratebay_proxy" id="thepiratebay_proxy" #if $sickbeard.THEPIRATEBAY_PROXY then "checked=\"checked\"" else ""#/>
<label class="clearfix" for="thepiratebay_proxy">
<span class="component-title">Acces TPB via Proxy</span>
<span class="component-desc">Check it if you want to bypass Country Blocking Mechanism</span>
</label>
</div>

<div class="field-pair" id="content_thepiratebay_proxy">
#from sickbeard.providers import thepiratebay
<label class="nocheck clearfix">
<span class="component-title">Proxy url:</span>
<span class="component-desc">
<select name="thepiratebay_proxy_url" id="thepiratebay_proxy_url">
#for $i in $thepiratebay.proxy_dict.keys():
<option value="$thepiratebay.proxy_dict[$i]" #if $thepiratebay.proxy_dict[$i] == $sickbeard.THEPIRATEBAY_PROXY_URL then "selected=\"selected\"" else ""#>$i</option>
#end for
</select>
</span>
</label>
</div>

<div class="field-pair">
<input type="checkbox" name="thepiratebay_trusted" id="thepiratebay_trusted" #if $sickbeard.THEPIRATEBAY_TRUSTED then "checked=\"checked\"" else ""#/>
<label class="clearfix" for="thepiratebay_trusted">
Expand Down
11 changes: 7 additions & 4 deletions sickbeard/__init__.py
Expand Up @@ -149,7 +149,8 @@
TVTORRENTS_HASH = None
THEPIRATEBAY = False
THEPIRATEBAY_TRUSTED = False
THEPIRATEBAY_PROXY = None
THEPIRATEBAY_PROXY = False
THEPIRATEBAY_PROXY_URL = None

TORRENT_DIR = None

Expand Down Expand Up @@ -350,7 +351,7 @@ def initialize(consoleLogging=True):
USE_PLEX, PLEX_NOTIFY_ONSNATCH, PLEX_NOTIFY_ONDOWNLOAD, PLEX_UPDATE_LIBRARY, \
PLEX_SERVER_HOST, PLEX_HOST, PLEX_USERNAME, PLEX_PASSWORD, \
showUpdateScheduler, __INITIALIZED__, LAUNCH_BROWSER, showList, loadingShowList, \
NZBS, NZBS_UID, NZBS_HASH, EZRSS, TVTORRENTS, TVTORRENTS_DIGEST, TVTORRENTS_HASH, THEPIRATEBAY, THEPIRATEBAY_TRUSTED, THEPIRATEBAY_PROXY, TORRENT_DIR, USENET_RETENTION, SOCKET_TIMEOUT, \
NZBS, NZBS_UID, NZBS_HASH, EZRSS, TVTORRENTS, TVTORRENTS_DIGEST, TVTORRENTS_HASH, THEPIRATEBAY, THEPIRATEBAY_TRUSTED, THEPIRATEBAY_PROXY, THEPIRATEBAY_PROXY_URL, TORRENT_DIR, USENET_RETENTION, SOCKET_TIMEOUT, \
SEARCH_FREQUENCY, DEFAULT_SEARCH_FREQUENCY, BACKLOG_SEARCH_FREQUENCY, \
QUALITY_DEFAULT, SEASON_FOLDERS_FORMAT, SEASON_FOLDERS_DEFAULT, STATUS_DEFAULT, \
GROWL_NOTIFY_ONSNATCH, GROWL_NOTIFY_ONDOWNLOAD, TWITTER_NOTIFY_ONSNATCH, TWITTER_NOTIFY_ONDOWNLOAD, \
Expand Down Expand Up @@ -496,7 +497,8 @@ def initialize(consoleLogging=True):

THEPIRATEBAY = bool(check_setting_int(CFG, 'THEPIRATEBAY', 'thepiratebay', 0))
THEPIRATEBAY_TRUSTED = bool(check_setting_int(CFG, 'THEPIRATEBAY', 'thepiratebay_trusted', 0))
THEPIRATEBAY_PROXY = check_setting_str(CFG, 'THEPIRATEBAY', 'thepiratebay_proxy', '')
THEPIRATEBAY_PROXY = bool(check_setting_int(CFG, 'THEPIRATEBAY', 'thepiratebay_proxy', 0))
THEPIRATEBAY_PROXY_URL = check_setting_str(CFG, 'THEPIRATEBAY', 'thepiratebay_proxy_url', '')

NZBS = bool(check_setting_int(CFG, 'NZBs', 'nzbs', 0))
NZBS_UID = check_setting_str(CFG, 'NZBs', 'nzbs_uid', '')
Expand Down Expand Up @@ -983,7 +985,8 @@ def save_config():
new_config['THEPIRATEBAY'] = {}
new_config['THEPIRATEBAY']['thepiratebay'] = int(THEPIRATEBAY)
new_config['THEPIRATEBAY']['thepiratebay_trusted'] = int(THEPIRATEBAY_TRUSTED)
new_config['THEPIRATEBAY']['thepiratebay_proxy'] = THEPIRATEBAY_PROXY
new_config['THEPIRATEBAY']['thepiratebay_proxy'] = int(THEPIRATEBAY_PROXY)
new_config['THEPIRATEBAY']['thepiratebay_proxy_url'] = THEPIRATEBAY_PROXY_URL

new_config['NZBs'] = {}
new_config['NZBs']['nzbs'] = int(NZBS)
Expand Down
101 changes: 73 additions & 28 deletions sickbeard/providers/thepiratebay.py
Expand Up @@ -19,6 +19,7 @@
import re
import datetime
import urllib, urllib2
import sys

import sickbeard
import generic
Expand All @@ -30,6 +31,21 @@
from sickbeard import show_name_helpers
from sickbeard import db
from sickbeard.common import Overview
from sickbeard.exceptions import ex

proxy_dict = {'15aa51.info (US)' : 'http://15aa51.info/',
# 'blewpass.com (US)' : 'http://www.blewpass.com/', #Not Working
'meganprx.info (FR)': 'http://www.meganprx.info/',
'theunblockproxy.com (US)' : 'http://theunblockproxy.com/',
'5tunnel.com (US)' : 'http://www.5tunnel.com/',
'proxite.eu (DE)' :'http://proxite.eu/',
'shieldmagic.com (GB)' : 'http://www.shieldmagic.com/',
'alexandraprx.info (FR)' : 'http://www.alexandraprx.info/',
'skipadmin.com (GB)' : 'http://skipadmin.com/',
'webproxy.cz (CZ)' : 'http://webproxy.cz/',
'experthide.com (US, SSL)' : 'https://www.experthide.com/',
'proxy-hide.com (US, SSL)' : 'https://proxy-hide.com/',
}

class ThePirateBayProvider(generic.TorrentProvider):

Expand All @@ -40,10 +56,14 @@ def __init__(self):
self.supportsBacklog = True

self.cache = ThePirateBayCache(self)


self.proxy = ThePirateBayWebproxy()

self.url = 'http://thepiratebay.org/'

self.searchurl = 'http://thepiratebay.org/search/%s/0/7/200' # order by seed
self.searchurl = 'http://thepiratebay.org/search/%s/0/7/200' # order by seed

self.re_title_url = '<td>.*?".*?/torrent/\d+/(?P<title>.*?)%s".*?<a href="(?P<url>.*?)%s".*?</td>'

def isEnabled(self):
return sickbeard.THEPIRATEBAY
Expand Down Expand Up @@ -121,23 +141,19 @@ def _get_episode_search_strings(self, ep_obj):
def _doSearch(self, search_params, show=None):

results = []
proxy_url = sickbeard.THEPIRATEBAY_PROXY

searchURL = self.searchurl %(urllib.quote(search_params))
re_title_url = '<td>.*?".*?/torrent/\d+/(?P<title>.*?)".*?<a href="(?P<url>.*?)".*?</td>'
searchURL = self.proxy._buildURL(self.searchurl %(urllib.quote(search_params)))

if proxy_url != '':
searchURL = proxy_url + 'browse.php?u=' + searchURL + '&b=32'
re_title_url = '<td>.*?".*?/torrent/\d+/(?P<title>.*?)&amp;b=32".*?<a href="(?P<url>.*?)&amp;b=32".*?</td>'

logger.log(u"Search string: " + searchURL, logger.DEBUG)

data = urllib.unquote(self.getURL(searchURL))
data = self.getURL(searchURL)
if not data:
return []

re_title_url = self.proxy._buildRE(self.re_title_url)

#Extracting torrent information from searchURL
match = re.compile(re_title_url, re.DOTALL ).finditer(data)
match = re.compile(re_title_url, re.DOTALL ).finditer(urllib.unquote(data))
for torrent in match:

#Accept Torrent only from Good People
Expand All @@ -160,13 +176,19 @@ def _get_title_and_url(self, item):

def getURL(self, url, headers=None):

proxy_url = sickbeard.THEPIRATEBAY_PROXY
import socket
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",1080)
socket.socket = socks.socksocket

if not headers:
headers = []

headers.append(('Referer', proxy_url))

# Glype Proxies does not support Direct Linking.
# We have to fake a search on the proxy site to get data
if self.proxy.isEnabled():
headers.append(('Referer', self.proxy.getProxyURL()))

result = None

try:
Expand All @@ -188,16 +210,12 @@ def __init__(self, provider):

def updateCache(self):

re_title_url = '<td>.*?".*?/torrent/\d+/(?P<title>.*?)".*?<a href="(?P<url>.*?)".*?</td>'

proxy_url = sickbeard.THEPIRATEBAY_PROXY
if proxy_url != '':
re_title_url = '<td>.*?".*?/torrent/\d+/(?P<title>.*?)&amp;b=32".*?<a href="(?P<url>.*?)&amp;b=32".*?</td>'
re_title_url = self.provider.proxy._buildRE(self.provider.re_title_url)

if not self.shouldUpdate():
return

data = urllib.unquote(self._getData())
data = self._getData()

# as long as the http request worked we count this as an update
if data:
Expand All @@ -209,7 +227,7 @@ def updateCache(self):
logger.log(u"Clearing "+self.provider.name+" cache and updating with new information")
self._clearCache()

match = re.compile(re_title_url, re.DOTALL).finditer(data)
match = re.compile(re_title_url, re.DOTALL).finditer(urllib.unquote(data))
if not match:
logger.log(u"The Data returned from the ThePirateBay is incomplete, this result is unusable", logger.ERROR)
return []
Expand All @@ -226,15 +244,11 @@ def updateCache(self):

def _getData(self):

url = 'http://thepiratebay.org/tv/latest/' #url for the last 50 tv-show
proxy_url = sickbeard.THEPIRATEBAY_PROXY.strip()

if proxy_url != '':
url = proxy_url + 'browse.php?u=' + url + '&b=32'

url = self.provider.proxy._buildURL('http://thepiratebay.org/tv/latest/') #url for the last 50 tv-show

logger.log(u"ThePirateBay cache update URL: "+ url, logger.DEBUG)

data = self.provider.getURL(url,[('Referer', proxy_url)])
data = self.provider.getURL(url)

return data

Expand All @@ -249,4 +263,35 @@ def _parseItem(self, item):

self._addCacheEntry(title, url)

class ThePirateBayWebproxy:

def __init__(self):
self.Type = 'GlypeProxy'
self.param = 'browse.php?u='
self.option = '&b=32'

def isEnabled(self):
""" Return True if we Choose to call TPB via Proxy """
return sickbeard.THEPIRATEBAY_PROXY

def getProxyURL(self):
""" Return the Proxy URL Choosen via Provider Setting """
return str(sickbeard.THEPIRATEBAY_PROXY_URL)

def _buildURL(self,url):
""" Return the Proxyfied URL of the page """
if self.isEnabled():
url = self.getProxyURL() + self.param + url + self.option

return url

def _buildRE(self,re):
""" Return the Proxyfied RE string """
if self.isEnabled():
re = re %('&amp;b=32','&amp;b=32')
else:
re = re %('','')

return re

provider = ThePirateBayProvider()
12 changes: 10 additions & 2 deletions sickbeard/webserve.py
Expand Up @@ -1023,7 +1023,7 @@ def saveProviders(self, nzbs_org_uid=None, nzbs_org_hash=None,
nzbmatrix_username=None, nzbmatrix_apikey=None,
nzbs_r_us_uid=None, nzbs_r_us_hash=None, newznab_string=None,
tvtorrents_digest=None, tvtorrents_hash=None,
thepiratebay_trusted=None, thepiratebay_proxy=None,
thepiratebay_trusted=None, thepiratebay_proxy=None, thepiratebay_proxy_url=None,
newzbin_username=None, newzbin_password=None,
provider_order=None):

Expand Down Expand Up @@ -1103,7 +1103,15 @@ def saveProviders(self, nzbs_org_uid=None, nzbs_org_hash=None,
thepiratebay_trusted = 0

sickbeard.THEPIRATEBAY_TRUSTED = thepiratebay_trusted
sickbeard.THEPIRATEBAY_PROXY = thepiratebay_proxy.strip()

if thepiratebay_proxy == "on":
thepiratebay_proxy = 1
sickbeard.THEPIRATEBAY_PROXY_URL = thepiratebay_proxy_url.strip()
else:
thepiratebay_proxy = 0
sickbeard.THEPIRATEBAY_PROXY_URL = ""

sickbeard.THEPIRATEBAY_PROXY = thepiratebay_proxy

sickbeard.NZBS_UID = nzbs_org_uid.strip()
sickbeard.NZBS_HASH = nzbs_org_hash.strip()
Expand Down

0 comments on commit ca6bb37

Please sign in to comment.