-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.py
185 lines (146 loc) · 7.23 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
""" Streamz utility functions """
from __future__ import absolute_import, division, unicode_literals
import logging
import requests
from requests import HTTPError
from requests.adapters import BaseAdapter
from resources.lib import kodiutils
from resources.lib.streamz.exceptions import InvalidTokenException, LimitReachedException, UnavailableException
_LOGGER = logging.getLogger(__name__)
class StreamzAdapter(BaseAdapter):
""" Fake adapter to handle the calls to streamz:// """
def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
""" Sends PreparedRequest object. Returns Response object. """
response = requests.Response()
response.url = request.url
response.status_code = 200
return response
def close(self):
""" Cleans up adapter specific items. """
# Setup a static session that can be reused for all calls
SESSION = requests.Session()
SESSION.headers = {
'User-Agent': 'STREAMZ/15.231023 (be.vmma.streamz; build:18041; Android 23) okhttp/4.11.0',
'x-app-version': '15',
'x-persgroep-mobile-app': 'true',
'x-persgroep-os': 'android',
'x-persgroep-os-version': '28',
}
PROXIES = kodiutils.get_proxies()
def http_get(url, params=None, token=None, profile=None, headers=None):
""" Make a HTTP GET request for the specified URL.
:param str url: The URL to call.
:param dict params: The query parameters to include to the URL.
:param str token: The token to use in Bearer authentication.
:param str profile: The profile to use in authentication.
:param dict headers: A dictionary with additional headers.
:returns: The HTTP Response object.
:rtype: requests.Response
"""
try:
return _request('GET', url=url, params=params, token=token, profile=profile, headers=headers)
except HTTPError as exc:
if exc.response.status_code == 401:
raise InvalidTokenException(exc)
if exc.response.status_code == 404:
raise UnavailableException(exc)
if exc.response.status_code == 429:
raise LimitReachedException(exc)
raise
def http_post(url, params=None, form=None, data=None, token=None, profile=None, headers=None):
""" Make a HTTP POST request for the specified URL.
:param str url: The URL to call.
:param dict params: The query parameters to include to the URL.
:param dict form: A dictionary with form parameters to POST.
:param dict data: A dictionary with json parameters to POST.
:param str token: The token to use in Bearer authentication.
:param str profile: The profile to use in authentication.
:param dict headers: A dictionary with additional headers.
:returns: The HTTP Response object.
:rtype: requests.Response
"""
try:
return _request('POST', url=url, params=params, form=form, data=data, token=token, profile=profile, headers=headers)
except HTTPError as exc:
if exc.response.status_code == 401:
raise InvalidTokenException(exc)
if exc.response.status_code == 404:
raise UnavailableException(exc)
if exc.response.status_code == 429:
raise LimitReachedException(exc)
raise
def http_put(url, params=None, form=None, data=None, token=None, profile=None, headers=None):
""" Make a HTTP PUT request for the specified URL.
:param str url: The URL to call.
:param dict params: The query parameters to include to the URL.
:param dict form: A dictionary with form parameters to POST.
:param dict data: A dictionary with json parameters to POST.
:param str token: The token to use in Bearer authentication.
:param str profile: The profile to use in authentication.
:param dict headers: A dictionary with additional headers.
:returns: The HTTP Response object.
:rtype: requests.Response
"""
try:
return _request('PUT', url=url, params=params, form=form, data=data, token=token, profile=profile, headers=headers)
except HTTPError as exc:
if exc.response.status_code == 401:
raise InvalidTokenException(exc)
if exc.response.status_code == 404:
raise UnavailableException(exc)
raise
def http_delete(url, params=None, token=None, profile=None, headers=None):
""" Make a HTTP DELETE request for the specified URL.
:param str url: The URL to call.
:param dict params: The query parameters to include to the URL.
:param str token: The token to use in Bearer authentication.
:param str profile: The profile to use in authentication.
:param dict headers: A dictionary with additional headers.
:returns: The HTTP Response object.
:rtype: requests.Response
"""
try:
return _request('DELETE', url=url, params=params, token=token, profile=profile, headers=headers)
except HTTPError as exc:
if exc.response.status_code == 401:
raise InvalidTokenException(exc)
if exc.response.status_code == 404:
raise UnavailableException(exc)
raise
def _request(method, url, params=None, form=None, data=None, token=None, profile=None, headers=None):
""" Makes a request for the specified URL.
:param str method: The HTTP Method to use.
:param str url: The URL to call.
:param dict params: The query parameters to include to the URL.
:param dict form: A dictionary with form parameters to POST.
:param dict data: A dictionary with json parameters to POST.
:param str token: The token to use in Bearer authentication.
:param str profile: The profile to use in authentication.
:param dict headers: A dictionary with additional headers.
:returns: The HTTP Response object.
:rtype: requests.Response
"""
if form or data:
# Make sure we don't log the password
debug_data = {}
debug_data.update(form or data)
if 'password' in debug_data:
debug_data['password'] = '**redacted**'
_LOGGER.debug('Sending %s %s: %s', method, url, debug_data)
else:
_LOGGER.debug('Sending %s %s', method, url)
if headers is None:
headers = {}
if token:
headers['lfvp-auth'] = token
if profile:
headers['x-dpp-profile'] = profile
response = SESSION.request(method, url, params=params, data=form, json=data, headers=headers, proxies=PROXIES)
# Set encoding to UTF-8 if no charset is indicated in http headers (https://github.com/psf/requests/issues/1604)
if not response.encoding:
response.encoding = 'utf-8'
_LOGGER.debug('Got response (status=%s): %s', response.status_code, response.text)
# Raise a generic HTTPError exception when we got an non-okay status code.
response.raise_for_status()
return response