Skip to content

Commit 1a9f683

Browse files
committed
Added retrieve_url function to eztv plugin so that it works for everybody
1 parent 42fbcc1 commit 1a9f683

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

nova3/engines/eztv.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,47 @@
22
# AUTHORS: nindogo
33
# CONTRIBUTORS: Diego de las Heras (ngosang@hotmail.es)
44

5+
import io
6+
import gzip
7+
import urllib.error
8+
import urllib.parse
9+
import urllib.request
510
from html.parser import HTMLParser
611

712
from novaprinter import prettyPrinter
8-
from helpers import retrieve_url
13+
from helpers import htmlentitydecode
14+
15+
# Some sites blocks default python User-agent
16+
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0'}
17+
18+
# We must implement our own retrieve_url because helpers.py versions prior to 1.49 did not
19+
# support POST requests. That version is taken from helpers.py 1.45
20+
def retrieve_url(url, data=None):
21+
""" Return the content of the url page as a string """
22+
req = urllib.request.Request(url, data, headers)
23+
try:
24+
response = urllib.request.urlopen(req)
25+
except urllib.error.URLError as errno:
26+
print(" ".join(("Connection error:", str(errno.reason))))
27+
return ""
28+
dat = response.read()
29+
# Check if it is gzipped
30+
if dat[:2] == b'\x1f\x8b':
31+
# Data is gzip encoded, decode it
32+
compressedstream = io.BytesIO(dat)
33+
gzipper = gzip.GzipFile(fileobj=compressedstream)
34+
extracted_data = gzipper.read()
35+
dat = extracted_data
36+
info = response.info()
37+
charset = 'utf-8'
38+
try:
39+
ignore, charset = info['Content-Type'].split('charset=')
40+
except Exception:
41+
pass
42+
dat = dat.decode(charset, 'replace')
43+
dat = htmlentitydecode(dat)
44+
# return dat.encode('utf-8', 'replace')
45+
return dat
946

1047

1148
class eztv(object):

0 commit comments

Comments
 (0)