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
|
|
|
|
|
2020-10-26 10:25:57 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SearchApi:
|
2020-03-21 20:46:14 +01:00
|
|
|
""" VIER/VIJF/ZES 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')
|
|
|
|
|
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':
|
|
|
|
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
|