parent
757e4929a6
commit
7cb34df4fa
2 changed files with 35 additions and 22 deletions
|
@ -10,7 +10,7 @@ import sys
|
||||||
import time
|
import time
|
||||||
import urllib.request, urllib.error, urllib.parse
|
import urllib.request, urllib.error, urllib.parse
|
||||||
import requests
|
import requests
|
||||||
import socket
|
import socket
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
|
@ -67,6 +67,16 @@ api_config['api_base'] = 'api'
|
||||||
api_config['bin_dir'] = '/usr/lib/airtime/api_clients/'
|
api_config['bin_dir'] = '/usr/lib/airtime/api_clients/'
|
||||||
api_config['update_metadata_on_tunein'] = 'update-metadata-on-tunein/api_key/%%api_key%%'
|
api_config['update_metadata_on_tunein'] = 'update-metadata-on-tunein/api_key/%%api_key%%'
|
||||||
|
|
||||||
|
def get_protocol(config):
|
||||||
|
port = config.get('general', 'base_port', fallback=80)
|
||||||
|
if config.getboolean('general', 'force_ssl', fallback=False):
|
||||||
|
protocol = 'https'
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
protocol = config.get(CONFIG_SECTION, 'protocol')
|
||||||
|
except NoOptionError as e:
|
||||||
|
protocol = str(("http", "https")[int(port) == 443])
|
||||||
|
return protocol
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
|
@ -169,9 +179,11 @@ class RequestProvider(object):
|
||||||
self.requests = {}
|
self.requests = {}
|
||||||
if self.config["general"]["base_dir"].startswith("/"):
|
if self.config["general"]["base_dir"].startswith("/"):
|
||||||
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
||||||
|
protocol = get_protocol(self.config)
|
||||||
|
|
||||||
self.url = ApcUrl("%s://%s:%s/%s%s/%s" \
|
self.url = ApcUrl("%s://%s:%s/%s%s/%s" \
|
||||||
% (str(("http", "https")[int(self.config["general"]["base_port"]) == 443]),
|
% (protocol, self.config["general"]["base_url"],
|
||||||
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
str(self.config["general"]["base_port"]),
|
||||||
self.config["general"]["base_dir"], self.config["api_base"],
|
self.config["general"]["base_dir"], self.config["api_base"],
|
||||||
'%%action%%'))
|
'%%action%%'))
|
||||||
# Now we must discover the possible actions
|
# Now we must discover the possible actions
|
||||||
|
@ -208,7 +220,7 @@ class AirtimeApiClient(object):
|
||||||
def __get_airtime_version(self):
|
def __get_airtime_version(self):
|
||||||
try: return self.services.version_url()['airtime_version']
|
try: return self.services.version_url()['airtime_version']
|
||||||
except Exception: return -1
|
except Exception: return -1
|
||||||
|
|
||||||
def __get_api_version(self):
|
def __get_api_version(self):
|
||||||
try: return self.services.version_url()['api_version']
|
try: return self.services.version_url()['api_version']
|
||||||
except Exception: return -1
|
except Exception: return -1
|
||||||
|
@ -331,8 +343,9 @@ class AirtimeApiClient(object):
|
||||||
# TODO : Make other methods in this class use this this method.
|
# TODO : Make other methods in this class use this this method.
|
||||||
if self.config["general"]["base_dir"].startswith("/"):
|
if self.config["general"]["base_dir"].startswith("/"):
|
||||||
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
||||||
|
protocol = get_protocol(self.config)
|
||||||
url = "%s://%s:%s/%s%s/%s" % \
|
url = "%s://%s:%s/%s%s/%s" % \
|
||||||
(str(("http", "https")[int(self.config["general"]["base_port"]) == 443]),
|
(protocol,
|
||||||
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
||||||
self.config["general"]["base_dir"], self.config["api_base"],
|
self.config["general"]["base_dir"], self.config["api_base"],
|
||||||
self.config[config_action_key])
|
self.config[config_action_key])
|
||||||
|
@ -343,9 +356,9 @@ class AirtimeApiClient(object):
|
||||||
"""Constructs the base url for RESTful requests"""
|
"""Constructs the base url for RESTful requests"""
|
||||||
if self.config["general"]["base_dir"].startswith("/"):
|
if self.config["general"]["base_dir"].startswith("/"):
|
||||||
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
||||||
|
protocol = get_protocol(self.config)
|
||||||
url = "%s://%s:@%s:%s/%s/%s" % \
|
url = "%s://%s:@%s:%s/%s/%s" % \
|
||||||
(str(("http", "https")[int(self.config["general"]["base_port"]) == 443]),
|
(protocol, self.config["general"]["api_key"],
|
||||||
self.config["general"]["api_key"],
|
|
||||||
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
||||||
self.config["general"]["base_dir"],
|
self.config["general"]["base_dir"],
|
||||||
self.config[config_action_key])
|
self.config[config_action_key])
|
||||||
|
|
|
@ -67,14 +67,14 @@ class PypoFile(Thread):
|
||||||
CONFIG_SECTION = "general"
|
CONFIG_SECTION = "general"
|
||||||
username = self._config.get(CONFIG_SECTION, 'api_key')
|
username = self._config.get(CONFIG_SECTION, 'api_key')
|
||||||
baseurl = self._config.get(CONFIG_SECTION, 'base_url')
|
baseurl = self._config.get(CONFIG_SECTION, 'base_url')
|
||||||
try:
|
port = self._config.get(CONFIG_SECTION, 'base_port', 80)
|
||||||
port = self._config.get(CONFIG_SECTION, 'base_port')
|
if self._config.getboolean(CONFIG_SECTION, 'force_ssl', fallback=False):
|
||||||
except NoOptionError as e:
|
protocol = 'https'
|
||||||
port = 80
|
else:
|
||||||
try:
|
try:
|
||||||
protocol = self._config.get(CONFIG_SECTION, 'protocol')
|
protocol = self._config.get(CONFIG_SECTION, 'protocol')
|
||||||
except NoOptionError as e:
|
except NoOptionError as e:
|
||||||
protocol = str(("http", "https")[int(port) == 443])
|
protocol = str(("http", "https")[int(port) == 443])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
host = [protocol, baseurl, port]
|
host = [protocol, baseurl, port]
|
||||||
|
@ -84,15 +84,15 @@ class PypoFile(Thread):
|
||||||
media_item["id"])
|
media_item["id"])
|
||||||
with open(dst, "wb") as handle:
|
with open(dst, "wb") as handle:
|
||||||
response = requests.get(url, auth=requests.auth.HTTPBasicAuth(username, ''), stream=True, verify=False)
|
response = requests.get(url, auth=requests.auth.HTTPBasicAuth(username, ''), stream=True, verify=False)
|
||||||
|
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
self.logger.error(response)
|
self.logger.error(response)
|
||||||
raise Exception("%s - Error occurred downloading file" % response.status_code)
|
raise Exception("%s - Error occurred downloading file" % response.status_code)
|
||||||
|
|
||||||
for chunk in response.iter_content(1024):
|
for chunk in response.iter_content(1024):
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
|
|
||||||
handle.write(chunk)
|
handle.write(chunk)
|
||||||
|
|
||||||
#make file world readable and owner writable
|
#make file world readable and owner writable
|
||||||
|
@ -160,11 +160,11 @@ class PypoFile(Thread):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Remove this media_item from the dictionary. On the next iteration
|
Remove this media_item from the dictionary. On the next iteration
|
||||||
(from the main function) we won't consider it for prioritization
|
(from the main function) we won't consider it for prioritization
|
||||||
anymore. If on the next iteration we have received a new schedule,
|
anymore. If on the next iteration we have received a new schedule,
|
||||||
it is very possible we will have to deal with the same media_items
|
it is very possible we will have to deal with the same media_items
|
||||||
again. In this situation, the worst possible case is that we try to
|
again. In this situation, the worst possible case is that we try to
|
||||||
copy the file again and realize we already have it (thus aborting the copy).
|
copy the file again and realize we already have it (thus aborting the copy).
|
||||||
"""
|
"""
|
||||||
del schedule[highest_priority]
|
del schedule[highest_priority]
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@ class PypoFile(Thread):
|
||||||
logging.debug("Failed to open config file at %s: %s" % (config_path, e.strerror))
|
logging.debug("Failed to open config file at %s: %s" % (config_path, e.strerror))
|
||||||
sys.exit()
|
sys.exit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.debug(e.strerror)
|
logging.debug(e.strerror)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue