From fca079587d4d54bf60b828e9acadd3489c619575 Mon Sep 17 00:00:00 2001 From: samedamci Date: Sat, 18 Sep 2021 19:41:15 +0200 Subject: [PATCH] Add `search` method to main class --- mattebox/app.py | 19 +++++++++++++++++++ mattebox/types.py | 19 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/mattebox/app.py b/mattebox/app.py index 3333c56..5045207 100644 --- a/mattebox/app.py +++ b/mattebox/app.py @@ -110,3 +110,22 @@ class MatteBOX: variants = m3u8.load(main_playlist_uri) playlist = variants.playlists[0] return playlist.uri + + def search( + self, query: str, over_18: bool = True, results_count: int = 200 + ) -> List[Program]: + res = self.__get("/sws/subscription/search/search.json", params={ + "audience": "OVER_18" if over_18 else "", # TODO + "entityCount": results_count, + "firstEntityOffset": 0, + "deviceType": "STB", + "facetAll": "true", + "isp": "1", + "language": "eng", + "tariff": "FERO Middleware", + "search": query, + }) + + filtered_results = [e for e in res["entities"] if e["type"] == "EPG"] + programs = [Program.from_search_result(p) for p in filtered_results] + return programs diff --git a/mattebox/types.py b/mattebox/types.py index ee34fe9..e12fb99 100644 --- a/mattebox/types.py +++ b/mattebox/types.py @@ -4,8 +4,8 @@ from __future__ import annotations class Program: """Class for single TV program or recording with basic information about it. - Methods `from_channel_program` and `from_recording` returns class object - from data received from API.""" + Methods `from_channel_program`, `from_recording` and `from_search_result` returns + class object from data received from API.""" name: str channel: str @@ -45,5 +45,20 @@ class Program: } return obj + @classmethod + def from_search_result(cls, data: dict) -> Program: + obj = cls() + obj.__dict__ = { + "name": data["name"], + "channel": data["channelKey"], + "epg_id": data["id"], + "content_id": data["id"], + "description": data["description"], + "ts_start": data["startTimestamp"], + "ts_stop": data["endTimestamp"], + "type": "program" + } + return obj + def __repr__(self) -> str: return f"{self.__class__.__name__}({self.__dict__})"