Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix response handling #331

Merged
merged 4 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions nova3/engines/piratebay.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# VERSION: 3.3
# VERSION: 3.4
biskweet marked this conversation as resolved.
Show resolved Hide resolved
# AUTHORS: Fabien Devaux (fab@gnux.info)
# CONTRIBUTORS: Christophe Dumez (chris@qbittorrent.org)
# Arthur (custparasite@gmx.se)
Expand Down Expand Up @@ -28,11 +28,16 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import gzip
import html
import io
import json
import urllib.error
import urllib.request
from urllib.parse import urlencode, unquote

from helpers import getBrowserUserAgent
from novaprinter import prettyPrinter
from helpers import retrieve_url


class piratebay(object):
Expand Down Expand Up @@ -70,8 +75,10 @@ def search(self, what, cat='all'):
params = {'q': what}
if category != '0':
params['cat'] = category
response = retrieve_url(base_url % urlencode(params))
response_json = json.loads(response)

# Calling custom `retrieve_url` function with adequate escaping
data = self.retrieve_url(base_url % urlencode(params))
response_json = json.loads(data)

# check empty response
if len(response_json) == 0:
Expand All @@ -96,3 +103,31 @@ def search(self, what, cat='all'):
def download_link(self, result):
return "magnet:?xt=urn:btih:{}&{}&{}".format(
result['info_hash'], urlencode({'dn': result['name']}), self.trackers)

def retrieve_url(self, url):
# Request data from API
request = urllib.request.Request(url, None, {'User-Agent': getBrowserUserAgent()})

try:
response = urllib.request.urlopen(request)
except urllib.error.HTTPError:
return ""

data = response.read()

if data[:2] == b'\x1f\x8b':
# Data is gzip encoded, decode it
with io.BytesIO(data) as stream, gzip.GzipFile(fileobj=stream) as gzipper:
data = gzipper.read()

charset = 'utf-8'
try:
charset = response.getheader('Content-Type', '').split('charset=', 1)[1]
except IndexError:
pass

dataStr = data.decode(charset, 'replace')
dataStr = dataStr.replace('"', '\\"') # Manually escape " before
dataStr = html.unescape(dataStr)

return dataStr
2 changes: 1 addition & 1 deletion nova3/engines/versions.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
eztv: 1.16
jackett: 4.0
limetorrents: 4.10
piratebay: 3.3
piratebay: 3.4
solidtorrents: 2.4
torlock: 2.24
torrentproject: 1.5
Expand Down