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

59 lines
1.5 KiB
Python
Raw Normal View History

2020-03-19 16:45:31 +01:00
# -*- coding: utf-8 -*-
""" AUTH API """
from __future__ import absolute_import, division, unicode_literals
import json
import logging
import requests
from resources.lib.viervijfzes.content import Program
_LOGGER = logging.getLogger(__name__)
2020-03-19 16:45:31 +01:00
class SearchApi:
""" GoPlay Search API """
2020-03-19 16:45:31 +01:00
API_ENDPOINT = 'https://api.viervijfzes.be/search'
def __init__(self):
""" Initialise object """
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,
"sites": ["vier", "vijf", "zes"],
"page": 0,
"mode": "byDate"
}
)
2020-03-20 13:53:21 +01:00
if response.status_code != 200:
raise Exception('Could not search')
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':
results.append(Program(
channel=hit['_source']['site'],
2020-09-10 21:06:16 +02:00
path=hit['_source']['url'].split('/')[-1],
2020-03-19 16:45:31 +01:00
title=hit['_source']['title'],
description=hit['_source']['intro'],
cover=hit['_source']['img'],
))
return results