mirror of http://git.simp.i2p/simp/i2music.git
135 lines
5.6 KiB
Python
135 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
import configparser, os, re, string, secrets
|
|
|
|
def random_string(size:int)->str:
|
|
'''takes an int, gives a random string of that length'''
|
|
letters = string.ascii_lowercase+string.ascii_uppercase+string.digits
|
|
return ''.join(secrets.choice(letters) for i in range(size))
|
|
|
|
def base_url(long_url:str)->str:
|
|
p = '(?:http.*://)?(?P<host>[^:/ ]+).?(?P<port>[0-9]*).*'
|
|
m = re.search(p,long_url)
|
|
short = m.group('host')
|
|
port = m.group('port')
|
|
if port != '':
|
|
x = f':{port}'
|
|
else:
|
|
x = ''
|
|
url = f'{short}{x}'
|
|
return url
|
|
|
|
def is_valid_path(path:str)->bool:
|
|
'''does path exist'''
|
|
return os.path.exists(path)
|
|
|
|
def string_to_list(input:str)->list:
|
|
x:list = input.split(',')
|
|
for i in range(0, len(x)):
|
|
if x[i][:1] == ' ':
|
|
x[i] = x[i][1:]
|
|
if x[i][-1:] == ' ':
|
|
x[i] = x[i][:-1]
|
|
print(x[i][1:])
|
|
return x
|
|
|
|
def get_playlist_directories(rootdir):
|
|
found_dirs = []
|
|
for file in os.listdir(rootdir):
|
|
d = os.path.join(rootdir, file)
|
|
if os.path.isdir(d):
|
|
source_file_base = os.path.basename(d)
|
|
found_dirs.append(source_file_base)
|
|
return found_dirs
|
|
|
|
def config_load(config_path:str)->tuple:
|
|
'''loads settings from config.txt'''
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
try:
|
|
hostname:str = (config['settings']['hostname'])
|
|
gunicorn_port:str = (config['settings']['gunicorn_port'])
|
|
secret_key:str = (config['settings']['secret_key'])
|
|
site_port:str = (config['settings']['site_port'])
|
|
stream_port:str = (config['settings']['stream_port'])
|
|
gunicorn_stream_port:str = (config['settings']['gunicorn_stream_port'])
|
|
changelog_txt:str = (config['settings']['changelog'])
|
|
default_theme:str = (config['appearance']['default_theme'])
|
|
theme_list:list = string_to_list((config['appearance']['theme_list']))
|
|
site_title:str = (config['appearance']['site_title'])
|
|
image_resize_kb:int = int((config['appearance']['image_resize_kb']))
|
|
log_file_location:str = (config['settings']['log_file_location'])
|
|
default_image:str = (config['audio_settings']['default_image'])
|
|
default_delay:int = int((config['audio_settings']['default_delay']))
|
|
bitrate:str = (config['audio_settings']['bitrate'])
|
|
subpath:str = (config['playlists']['subpath'])
|
|
playlists:list = get_playlist_directories(os.path.join(script_directory, 'static' ,subpath))
|
|
exclude:list = string_to_list((config['playlists']['exclude']))
|
|
make_playlist_torrents:bool = usr_str_to_bool((config['torrent']['make_playlist_torrents']))[0]
|
|
wait_before_adding_torrent:int = int(config['torrent']['wait_before_adding_torrent'])
|
|
get_peercounts:bool = usr_str_to_bool((config['torrent']['get_peercounts']))[0]
|
|
scraper_hostname:str = (config['torrent']['scraper_hostname'])
|
|
scraper_http_proxy:str = (config['torrent']['scraper_http_proxy'])
|
|
scrape_interval:int = int((config['torrent']['scrape_interval']))
|
|
torrent_directory:str = (config['torrent']['torrent_directory'])
|
|
left_footer:str = (config['footer']['left_footer'])
|
|
right_footer:str = (config['footer']['right_footer'])
|
|
ah:str = (config['footer']['ah'])
|
|
b32_raw:list = list_ini(settings_path, 'b32streams', 'b32s')
|
|
b32s_:list = []
|
|
for item in b32_raw:
|
|
b32s_.append(f'http://{base_url(item)}')
|
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError, KeyError) as e:
|
|
if is_valid_path(config_path):
|
|
print(f"Error reading config file: {e}")
|
|
else:
|
|
print(f"The 'config.txt' file was not found{e}")
|
|
except (ValueError) as e:
|
|
print(f"Cannot accept value: {e}")
|
|
|
|
return hostname, secret_key, site_port, changelog_txt, log_file_location, ah, gunicorn_port, playlists, default_image, default_delay, subpath, bitrate, make_playlist_torrents, exclude, get_peercounts, scraper_hostname, scraper_http_proxy, scrape_interval, left_footer, right_footer, default_theme, site_title, theme_list, torrent_directory, image_resize_kb, wait_before_adding_torrent, b32s_, stream_port, gunicorn_stream_port
|
|
|
|
def list_ini(config_path:str, section_name:str, key_name:str)->list:
|
|
'''from config.txt loads a key from a section as a list, with one entry per line.'''
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
try:
|
|
my_list_str = config.get(section_name, key_name)
|
|
my_list = [item.strip() for item in my_list_str.split('\n') if item.strip()]
|
|
return my_list
|
|
except (configparser.NoSectionError, configparser.NoOptionError) as e:
|
|
print(e)
|
|
return []
|
|
|
|
def usr_str_to_bool(string:str) -> tuple:
|
|
'''convert string to bool'''
|
|
x = string.upper()
|
|
fail = False
|
|
if x == 'TRUE' or x == 'T' or x =='YES' or x == 'Y' or x == 'ON':
|
|
bool_converted = True
|
|
elif x == 'FALSE' or x == 'F' or x =='NO' or x == 'N' or x == 'OFF':
|
|
bool_converted = False
|
|
else:
|
|
bool_converted = False
|
|
fail = True
|
|
return bool_converted, fail
|
|
|
|
script_directory = os.path.dirname(os.path.abspath(__file__))
|
|
settings_path = os.path.join(script_directory, 'config.txt')
|
|
hostname, secret_key, site_port, changelog_txt, log_file_location, ah, gunicorn_port, playlists, default_image, default_delay, subpath, bitrate, make_playlist_torrents, exclude, get_peercounts, scraper_hostname, scraper_http_proxy, scrape_interval, left_footer, right_footer, default_theme, site_title, theme_list, torrent_directory, image_resize_kb, wait_before_adding_torrent, b32s_, stream_port, gunicorn_stream_port = config_load(settings_path)
|
|
music_paths = []
|
|
for item in playlists:
|
|
music_paths.append(os.path.join(script_directory, f'static/{subpath}/{item}'))
|
|
|
|
b32_dict = {}
|
|
if len(b32s_) > 0:
|
|
for i in range(0, len(b32s_)):
|
|
b32_dict[i] = {
|
|
'b32': b32s_[i],
|
|
'timestamps': [],
|
|
}
|
|
else:
|
|
b32_dict[0] = {
|
|
'b32': random_string(4),
|
|
'timestamps': [],
|
|
} |