plugin.video.viervijfzes/resources/lib/viervijfzes/search.py

63 lines
1.7 KiB
Python
Raw Normal View History

2020-03-19 16:45:31 +01:00
# -*- coding: utf-8 -*-
""" Search API """
2020-03-19 16:45:31 +01:00
from __future__ import absolute_import, division, unicode_literals
import json
import logging
import requests
from resources.lib import kodiutils
from resources.lib.viervijfzes.content import Program, ContentApi, CACHE_ONLY
2020-03-19 16:45:31 +01:00
_LOGGER = logging.getLogger(__name__)
2020-03-19 16:45:31 +01:00
class SearchApi:
""" GoPlay Search API """
API_ENDPOINT = 'https://api.goplay.be/search'
2020-03-19 16:45:31 +01:00
def __init__(self):
""" Initialise object """
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,
"mode": "programs"
2020-03-19 16:45:31 +01:00
}
)
_LOGGER.debug(response.content)
response.raise_for_status()
2020-03-20 13:53:21 +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':
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