qBittorrent
nova3.helpers Namespace Reference

Functions

def htmlentitydecode (s)
 
def retrieve_url (url)
 
def download_file (url, referer=None)
 

Variables

string user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0'
 
dictionary headers = {'User-Agent': user_agent}
 
 proxy_str = os.environ["sock_proxy"].strip()
 
 m
 
 socket
 

Function Documentation

◆ download_file()

def nova3.helpers.download_file (   url,
  referer = None 
)
 Download file at url and write it to a file, return the path to the file and the url 

Definition at line 101 of file helpers.py.

101 def download_file(url, referer=None):
102  """ Download file at url and write it to a file, return the path to the file and the url """
103  file, path = tempfile.mkstemp()
104  file = os.fdopen(file, "wb")
105  # Download url
106  req = urllib.request.Request(url, headers=headers)
107  if referer is not None:
108  req.add_header('referer', referer)
109  response = urllib.request.urlopen(req)
110  dat = response.read()
111  # Check if it is gzipped
112  if dat[:2] == b'\x1f\x8b':
113  # Data is gzip encoded, decode it
114  compressedstream = io.BytesIO(dat)
115  gzipper = gzip.GzipFile(fileobj=compressedstream)
116  extracted_data = gzipper.read()
117  dat = extracted_data
118 
119  # Write it to a file
120  file.write(dat)
121  file.close()
122  # return file path
123  return (path + " " + url)
def download_file(url, referer=None)
Definition: helpers.py:101

◆ htmlentitydecode()

def nova3.helpers.htmlentitydecode (   s)

Definition at line 56 of file helpers.py.

56 def htmlentitydecode(s):
57  # First convert alpha entities (such as é)
58  # (Inspired from http://mail.python.org/pipermail/python-list/2007-June/443813.html)
59  def entity2char(m):
60  entity = m.group(1)
61  if entity in html.entities.name2codepoint:
62  return chr(html.entities.name2codepoint[entity])
63  return " " # Unknown entity: We replace with a space.
64  t = re.sub('&(%s);' % '|'.join(html.entities.name2codepoint), entity2char, s)
65 
66  # Then convert numerical entities (such as é)
67  t = re.sub(r'&#(\d+);', lambda x: chr(int(x.group(1))), t)
68 
69  # Then convert hexa entities (such as é)
70  return re.sub(r'&#x(\w+);', lambda x: chr(int(x.group(1), 16)), t)
71 
72 
QString join(const QList< QStringView > &strings, QStringView separator)
Definition: string.cpp:102
def htmlentitydecode(s)
Definition: helpers.py:56

References Utils::String.join().

Referenced by nova3.helpers.retrieve_url().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ retrieve_url()

def nova3.helpers.retrieve_url (   url)
 Return the content of the url page as a string 

Definition at line 73 of file helpers.py.

73 def retrieve_url(url):
74  """ Return the content of the url page as a string """
75  req = urllib.request.Request(url, headers=headers)
76  try:
77  response = urllib.request.urlopen(req)
78  except urllib.error.URLError as errno:
79  print(" ".join(("Connection error:", str(errno.reason))))
80  return ""
81  dat = response.read()
82  # Check if it is gzipped
83  if dat[:2] == b'\x1f\x8b':
84  # Data is gzip encoded, decode it
85  compressedstream = io.BytesIO(dat)
86  gzipper = gzip.GzipFile(fileobj=compressedstream)
87  extracted_data = gzipper.read()
88  dat = extracted_data
89  info = response.info()
90  charset = 'utf-8'
91  try:
92  ignore, charset = info['Content-Type'].split('charset=')
93  except Exception:
94  pass
95  dat = dat.decode(charset, 'replace')
96  dat = htmlentitydecode(dat)
97  # return dat.encode('utf-8', 'replace')
98  return dat
99 
100 
def retrieve_url(url)
Definition: helpers.py:73

References nova3.helpers.htmlentitydecode(), and Utils::String.join().

Here is the call graph for this function:

Variable Documentation

◆ headers

dictionary nova3.helpers.headers = {'User-Agent': user_agent}

◆ m

nova3.helpers.m
Initial value:
1 = re.match(r"^(?:(?P<username>[^:]+):(?P<password>[^@]+)@)?(?P<host>[^:]+):(?P<port>\w+)$",
2  proxy_str)

Definition at line 48 of file helpers.py.

Referenced by AppController.setPreferencesAction().

◆ proxy_str

nova3.helpers.proxy_str = os.environ["sock_proxy"].strip()

Definition at line 47 of file helpers.py.

◆ socket

nova3.helpers.socket

Definition at line 53 of file helpers.py.

Referenced by QtLocalPeer.receiveConnection(), and QtLocalPeer.sendMessage().

◆ user_agent

string nova3.helpers.user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0'

Definition at line 43 of file helpers.py.

Referenced by BitTorrent::Session.initializeNativeSession().