2020-03-19 16:45:31 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-02-09 20:54:40 +01:00
|
|
|
""" Search API """
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
|
|
|
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2021-02-09 20:54:40 +01:00
|
|
|
from resources.lib import kodiutils
|
|
|
|
from resources.lib.viervijfzes.content import Program, ContentApi, CACHE_ONLY
|
2020-03-19 16:45:31 +01:00
|
|
|
|
2020-10-26 10:25:57 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SearchApi:
|
2021-02-01 08:53:13 +01:00
|
|
|
""" GoPlay Search API """
|
2021-02-09 20:54:40 +01:00
|
|
|
API_ENDPOINT = 'https://api.goplay.be/search'
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
""" Initialise object """
|
2021-02-09 20:54:40 +01:00
|
|
|
self._api = ContentApi(None, cache_path=kodiutils.get_cache_path())
|
2020-03-19 16:45:31 +01:00
|
|
|
self._session = requests.session()
|
|
|
|
|
|
|
|
def search(self, query):
|
|
|
|
""" Get the stream URL to use for this video.
|
|
|
|
:type query: str
|
|
|
|
:rtype list[Program]
|
|
|
|
"""
|
2020-03-20 13:53:21 +01:00
|
|
|
if not query:
|
|
|
|
return []
|
|
|
|
|
2020-03-19 16:45:31 +01:00
|
|
|
response = self._session.post(
|
|
|
|
self.API_ENDPOINT,
|
|
|
|
json={
|
|
|
|
"query": query,
|
|
|
|
"page": 0,
|
2021-02-09 20:54:40 +01:00
|
|
|
"mode": "programs"
|
2020-03-19 16:45:31 +01:00
|
|
|
}
|
|
|
|
)
|
2021-02-09 20:54:40 +01:00
|
|
|
_LOGGER.debug(response.content)
|
|
|
|
response.raise_for_status()
|
2020-03-20 13:53:21 +01:00
|
|
|
|
2020-03-22 15:37:15 +01:00
|
|
|
data = json.loads(response.text)
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
results = []
|
|
|
|
for hit in data['hits']['hits']:
|
|
|
|
if hit['_source']['bundle'] == 'program':
|
2021-02-09 20:54:40 +01:00
|
|
|
path = hit['_source']['url'].split('/')[-1]
|
|
|
|
program = self._api.get_program(path, cache=CACHE_ONLY)
|
|
|
|
if program:
|
|
|
|
results.append(program)
|
|
|
|
else:
|
|
|
|
results.append(Program(
|
|
|
|
path=path,
|
|
|
|
title=hit['_source']['title'],
|
|
|
|
description=hit['_source']['intro'],
|
|
|
|
cover=hit['_source']['img'],
|
|
|
|
))
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
return results
|