add API v2
This commit is contained in:
parent
f809c3a8ff
commit
2df0189a90
71 changed files with 2740 additions and 315 deletions
|
@ -28,7 +28,7 @@ from configobj import ConfigObj
|
|||
|
||||
# custom imports
|
||||
#from util import *
|
||||
from api_clients import *
|
||||
from api_clients import version1 as api_client
|
||||
|
||||
LOG_LEVEL = logging.INFO
|
||||
LOG_PATH = '/var/log/airtime/pypo/notify.log'
|
||||
|
|
|
@ -4,7 +4,7 @@ import os
|
|||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from api_clients.api_client import AirtimeApiClient
|
||||
from api_clients.version1 import AirtimeApiClient
|
||||
|
||||
def generate_liquidsoap_config(ss):
|
||||
data = ss['msg']
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
from api_clients import *
|
||||
from api_clients import version1 as api_client
|
||||
import sys
|
||||
|
||||
api_clients = api_client.AirtimeApiClient()
|
||||
|
|
|
@ -12,7 +12,7 @@ import sys
|
|||
import telnetlib
|
||||
import time
|
||||
|
||||
from api_clients import api_client
|
||||
from api_clients import version1 as api_client
|
||||
from configobj import ConfigObj
|
||||
from datetime import datetime
|
||||
from optparse import OptionParser
|
||||
|
|
|
@ -7,7 +7,7 @@ import traceback
|
|||
import logging
|
||||
import time
|
||||
|
||||
from api_clients import api_client
|
||||
from api_clients import version1 as api_client
|
||||
|
||||
class ListenerStat(Thread):
|
||||
|
||||
|
|
|
@ -16,7 +16,8 @@ from queue import Empty
|
|||
from threading import Thread, Timer
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from api_clients import api_client
|
||||
from api_clients import version1 as v1_api_client
|
||||
from api_clients import version2 as api_client
|
||||
from .timeout import ls_timeout
|
||||
|
||||
|
||||
|
@ -28,7 +29,7 @@ signal.signal(signal.SIGINT, keyboardInterruptHandler)
|
|||
|
||||
logging.captureWarnings(True)
|
||||
|
||||
POLL_INTERVAL = 480
|
||||
POLL_INTERVAL = 400
|
||||
|
||||
class PypoFetch(Thread):
|
||||
|
||||
|
@ -38,6 +39,7 @@ class PypoFetch(Thread):
|
|||
#Hacky...
|
||||
PypoFetch.ref = self
|
||||
|
||||
self.v1_api_client = v1_api_client.AirtimeApiClient()
|
||||
self.api_client = api_client.AirtimeApiClient()
|
||||
self.fetch_queue = pypoFetch_q
|
||||
self.push_queue = pypoPush_q
|
||||
|
@ -150,7 +152,7 @@ class PypoFetch(Thread):
|
|||
def set_bootstrap_variables(self):
|
||||
self.logger.debug('Getting information needed on bootstrap from Airtime')
|
||||
try:
|
||||
info = self.api_client.get_bootstrap_info()
|
||||
info = self.v1_api_client.get_bootstrap_info()
|
||||
except Exception as e:
|
||||
self.logger.exception('Unable to get bootstrap info.. Exiting pypo...')
|
||||
|
||||
|
@ -255,7 +257,7 @@ class PypoFetch(Thread):
|
|||
stream_id = info[0]
|
||||
status = info[1]
|
||||
if(status == "true"):
|
||||
self.api_client.notify_liquidsoap_status("OK", stream_id, str(fake_time))
|
||||
self.v1_api_client.notify_liquidsoap_status("OK", stream_id, str(fake_time))
|
||||
|
||||
|
||||
@ls_timeout
|
||||
|
@ -343,7 +345,7 @@ class PypoFetch(Thread):
|
|||
media_item = media[key]
|
||||
if (media_item['type'] == 'file'):
|
||||
fileExt = self.sanity_check_media_item(media_item)
|
||||
dst = os.path.join(download_dir, "{}{}".format(media_item['id'], fileExt))
|
||||
dst = os.path.join(download_dir, f'{media_item["id"]}{fileExt}')
|
||||
media_item['dst'] = dst
|
||||
media_item['file_ready'] = False
|
||||
media_filtered[key] = media_item
|
||||
|
@ -434,10 +436,14 @@ class PypoFetch(Thread):
|
|||
self.logger.exception("Problem removing file '%s'" % f)
|
||||
|
||||
def manual_schedule_fetch(self):
|
||||
success, self.schedule_data = self.api_client.get_schedule()
|
||||
if success:
|
||||
try:
|
||||
self.schedule_data = self.api_client.get_schedule()
|
||||
self.process_schedule(self.schedule_data)
|
||||
return success
|
||||
return True
|
||||
except Exception as e:
|
||||
self.logger.error('Unable to fetch schedule')
|
||||
self.logger.exception(e)
|
||||
return False
|
||||
|
||||
def persistent_manual_schedule_fetch(self, max_attempts=1):
|
||||
success = False
|
||||
|
@ -452,7 +458,7 @@ class PypoFetch(Thread):
|
|||
# push metadata to TuneIn. We have to do this because TuneIn turns
|
||||
# off metadata if it does not receive a request every 5 minutes.
|
||||
def update_metadata_on_tunein(self):
|
||||
self.api_client.update_metadata_on_tunein()
|
||||
self.v1_api_client.update_metadata_on_tunein()
|
||||
Timer(120, self.update_metadata_on_tunein).start()
|
||||
|
||||
def main(self):
|
||||
|
|
|
@ -16,6 +16,7 @@ import configparser
|
|||
import json
|
||||
import hashlib
|
||||
from requests.exceptions import ConnectionError, HTTPError, Timeout
|
||||
from api_clients import version2 as api_client
|
||||
|
||||
CONFIG_PATH = '/etc/airtime/airtime.conf'
|
||||
|
||||
|
@ -31,6 +32,7 @@ class PypoFile(Thread):
|
|||
self.media = None
|
||||
self.cache_dir = os.path.join(config["cache_dir"], "scheduler")
|
||||
self._config = self.read_config_file(CONFIG_PATH)
|
||||
self.api_client = api_client.AirtimeApiClient()
|
||||
|
||||
def copy_file(self, media_item):
|
||||
"""
|
||||
|
@ -44,6 +46,8 @@ class PypoFile(Thread):
|
|||
dst_exists = True
|
||||
try:
|
||||
dst_size = os.path.getsize(dst)
|
||||
if dst_size == 0:
|
||||
dst_exists = False
|
||||
except Exception as e:
|
||||
dst_exists = False
|
||||
|
||||
|
@ -63,41 +67,16 @@ class PypoFile(Thread):
|
|||
|
||||
if do_copy:
|
||||
self.logger.info("copying from %s to local cache %s" % (src, dst))
|
||||
|
||||
CONFIG_SECTION = 'general'
|
||||
username = self._config[CONFIG_SECTION].get('api_key')
|
||||
baseurl = self._config[CONFIG_SECTION].get('base_url')
|
||||
port = self._config[CONFIG_SECTION].get('base_port', 80)
|
||||
positive_values = ['Yes', 'yes', 'True', 'true', True]
|
||||
force_ssl = self._config[CONFIG_SECTION].get('force_ssl', False)
|
||||
if force_ssl in positive_values:
|
||||
protocol = 'https'
|
||||
self.logger.debug('protocol set to https from force_ssl configuration setting')
|
||||
else:
|
||||
try:
|
||||
protocol = self._config[CONFIG_SECTION]['protocol']
|
||||
self.logger.debug('protocol set to %s from configuration setting' % (protocol))
|
||||
except (NoOptionError, KeyError) as e:
|
||||
protocol = str(("http", "https")[int(port) == 443])
|
||||
self.logger.debug('guessing protocol as %s from port configuration' % (protocol))
|
||||
|
||||
try:
|
||||
host = [protocol, baseurl, port]
|
||||
url = "%s://%s:%s/rest/media/%s/download" % (host[0],
|
||||
host[1],
|
||||
host[2],
|
||||
media_item["id"])
|
||||
with open(dst, "wb") as handle:
|
||||
response = requests.get(url, auth=requests.auth.HTTPBasicAuth(username, ''), stream=True, verify=False)
|
||||
self.logger.info(media_item)
|
||||
response = self.api_client.services.file_download_url(id=media_item['id'])
|
||||
|
||||
if not response.ok:
|
||||
self.logger.error(response)
|
||||
raise Exception("%s - Error occurred downloading file" % response.status_code)
|
||||
|
||||
for chunk in response.iter_content(1024):
|
||||
if not chunk:
|
||||
break
|
||||
|
||||
for chunk in response.iter_content(chunk_size=1024):
|
||||
handle.write(chunk)
|
||||
|
||||
#make file world readable and owner writable
|
||||
|
|
|
@ -20,7 +20,7 @@ from queue import Empty, Queue
|
|||
|
||||
from threading import Thread
|
||||
|
||||
from api_clients import api_client
|
||||
from api_clients import version1 as api_client
|
||||
from .timeout import ls_timeout
|
||||
|
||||
logging.captureWarnings(True)
|
||||
|
|
|
@ -21,14 +21,15 @@ from threading import Thread
|
|||
|
||||
import mutagen
|
||||
|
||||
from api_clients import api_client as apc
|
||||
from api_clients import version1 as v1_api_client
|
||||
from api_clients import version2 as api_client
|
||||
|
||||
def api_client(logger):
|
||||
"""
|
||||
api_client returns the correct instance of AirtimeApiClient. Although there is only one
|
||||
instance to choose from at the moment.
|
||||
"""
|
||||
return apc.AirtimeApiClient(logger)
|
||||
return v1_api_client.AirtimeApiClient(logger)
|
||||
|
||||
# loading config file
|
||||
try:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue