slight refactoring of http api

This commit is contained in:
DYefremov
2019-09-28 21:57:41 +03:00
parent de770169fa
commit 8d6b1303dc
3 changed files with 45 additions and 40 deletions

View File

@@ -273,6 +273,7 @@ def telnet(host, port=23, user="", password="", timeout=5):
def http_request(host, port, user, password):
base_url = "http://{}:{}/api/".format(host, port)
init_auth(user, password, base_url)
while True:
req_type, ref = yield
url = base_url
@@ -285,19 +286,22 @@ def http_request(host, port, user, password):
elif req_type is HttpRequestType.STREAM:
url = base_url + HttpRequestType.STREAM.value
try:
with urlopen(url, timeout=5) as f:
if req_type is HttpRequestType.STREAM:
yield f.read().decode("utf-8")
else:
yield json.loads(f.read().decode("utf-8"))
except (URLError, HTTPError):
yield None
yield from get_json(req_type, url)
def get_json(req_type, url):
try:
with urlopen(url, timeout=5) as f:
if req_type is HttpRequestType.STREAM:
yield f.read().decode("utf-8")
else:
yield json.loads(f.read().decode("utf-8"))
except (URLError, HTTPError):
yield None
# ***************** Connections testing *******************#
def test_ftp(host, port, user, password, timeout=5):
try:
with FTP(host=host, user=user, passwd=password, timeout=timeout) as ftp:

View File

@@ -178,7 +178,6 @@ class Application(Gtk.Application):
# http api
self._http_api = None
self._fav_click_mode = None
self._monitor_signal = False
# Colors
self._use_colors = False
self._NEW_COLOR = None # Color for new services in the main list
@@ -833,7 +832,7 @@ class Application(Gtk.Application):
def open_data(self, data_path=None):
""" Opening data and fill views. """
gen = self.update_data(data_path)
GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_LOW)
GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_DEFAULT_IDLE)
def update_data(self, data_path):
self._wait_dialog.show()
@@ -956,7 +955,7 @@ class Application(Gtk.Application):
yield True
for index, itr in enumerate([row.iter for row in self._services_model]):
self._services_model.remove(itr)
if index % 50 == 0:
if index % 25 == 0:
yield True
yield True
self._services_view.set_model(s_model)
@@ -1426,7 +1425,7 @@ class Application(Gtk.Application):
if not self._bq_selected:
return
YtListImportDialog(self._main_window, Profile(self._profile), self.append_imported_services).show()
YtListImportDialog(self._main_window, Profile(self._profile), self.append_imported_services).show()
def on_import_m3u(self, item):
""" Imports iptv from m3u files. """
@@ -1610,23 +1609,24 @@ class Application(Gtk.Application):
self._player_tool_bar.set_visible(full)
# ************************ HTTP API ****************************#
@run_task
def init_http_api(self):
if self._http_api:
self._http_api.close()
self._http_api = None
prp = self._options.get(self._profile)
self._fav_click_mode = FavClickMode(prp.get("fav_click_mode", FavClickMode.DISABLED))
if prp is Profile.NEUTRINO_MP or not prp.get("http_api_support", False):
self.update_info_boxes_visible(False)
if self._http_api:
self._http_api.close()
self._http_api = None
return
self._http_api = http_request(prp.get("host", "127.0.0.1"), prp.get("http_port", "80"),
prp.get("http_user", ""), prp.get("http_password", ""))
next(self._http_api)
GLib.timeout_add_seconds(1, self.update_receiver_info)
if not self._http_api:
self._http_api = http_request(prp.get("host", "127.0.0.1"), prp.get("http_port", "80"),
prp.get("http_user", ""), prp.get("http_password", ""))
next(self._http_api)
GLib.timeout_add_seconds(1, self.update_receiver_info)
def on_watch(self):
""" Switch to the channel and watch in the player """
@@ -1655,7 +1655,6 @@ class Application(Gtk.Application):
req = self._http_api.send((HttpRequestType.ZAP, ref))
next(self._http_api)
if req and req.get("result", False):
GLib.timeout_add_seconds(2, self.update_service_info)
GLib.idle_add(scroll_to, path, self._fav_view)
if callback is not None:
callback()
@@ -1682,27 +1681,31 @@ class Application(Gtk.Application):
GLib.idle_add(self._receiver_info_box.set_visible, res_info)
if service_info:
GLib.idle_add(self._service_name_label.set_text, service_info.get("name", ""))
GLib.timeout_add_seconds(2, self.update_signal)
gen = self.update_service_info()
GLib.timeout_add_seconds(3, lambda: next(gen, False), priority=GLib.PRIORITY_LOW)
GLib.idle_add(self._signal_box.set_visible, service_info)
def update_signal(self):
if not self._http_api:
return
sig = self._http_api.send((HttpRequestType.SIGNAL, None))
next(self._http_api)
val = sig.get("snr", 0)
self._signal_level_bar.set_value(val if val else 0)
self._signal_level_bar.set_visible(val)
return self._monitor_signal
def update_service_info(self):
info = self._http_api.send((HttpRequestType.INFO, None))
next(self._http_api)
if info:
service_info = info.get("service", None)
if service_info:
GLib.idle_add(self._service_name_label.set_text, service_info.get("name", ""))
GLib.timeout_add_seconds(1, self.update_signal)
while self._http_api:
info = self._http_api.send((HttpRequestType.INFO, None))
next(self._http_api)
if info:
service_info = info.get("service", None)
if service_info:
GLib.idle_add(self._service_name_label.set_text, service_info.get("name", ""))
if service_info.get("onid", None):
self.update_signal()
yield self._http_api
# ***************** Filter and search *********************#

View File

@@ -2686,12 +2686,12 @@ Author: Dmitriy Yefremov
<object class="GtkBox" id="receiver_info_box">
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_left">10</property>
<child>
<object class="GtkLabel" id="receiver_info_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Receiver info</property>
<property name="margin_left">10</property>
<property name="label" translatable="yes">Receiver info</property>
<attributes>
<attribute name="size" value="8000"/>
@@ -2710,7 +2710,7 @@ Author: Dmitriy Yefremov
<property name="position">0</property>
</packing>
</child>
<child>
<child type="center">
<object class="GtkBox" id="ip_status_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
@@ -2752,20 +2752,21 @@ Author: Dmitriy Yefremov
<property name="expand">True</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="position">1</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkBox" id="signal_box">
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="spacing">5</property>
<child>
<object class="GtkLabel" id="service_name_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Current service</property>
<property name="label" translatable="yes">Service name</property>
<property name="justify">fill</property>
<property name="xalign">1</property>
<attributes>
@@ -2781,12 +2782,9 @@ Author: Dmitriy Yefremov
<child>
<object class="GtkLevelBar" id="signal_level_bar">
<property name="width_request">70</property>
<property name="height_request">10</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Tuner signal</property>
<property name="valign">center</property>
<property name="margin_right">10</property>
<property name="max_value">100</property>
</object>
<packing>