102 lines
No EOL
3.6 KiB
Python
Executable file
102 lines
No EOL
3.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import tempfile
|
|
import logging
|
|
import signal
|
|
import sys
|
|
import configparser
|
|
import os
|
|
from time import sleep
|
|
from lib import jamendo
|
|
from lib.player import Player
|
|
from lib.png_renderer import PNG_Renderer
|
|
|
|
# Jamendo Client ID for this project
|
|
CLIENT_ID = '32361b03'
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
prog = 'Jamendo OBS Player',
|
|
description='Plays jamendo music and shows a nice window to include in OBS'
|
|
)
|
|
parser.add_argument('tags', help='Search for tags (Example: chillhop+lofi')
|
|
parser.add_argument('--offset', type=int, default=0, help='Set lookup offset')
|
|
parser.add_argument('--config', type=str, default='jamendo_obs.ini', help='Path to config file')
|
|
args = parser.parse_args()
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger('Jamendo OBS')
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(args.config)
|
|
|
|
track_offset = args.offset
|
|
|
|
jamendo_client = jamendo.JamendoClient(CLIENT_ID)
|
|
renderer = PNG_Renderer(config['renderer.png_renderer'])
|
|
|
|
# remove files if playback is finished
|
|
def play_finished_handler(audio_file, cover_file):
|
|
logger.debug(f'Removing {audio_file} and {cover_file}...')
|
|
os.remove(audio_file)
|
|
os.remove(cover_file)
|
|
player = Player(renderer, play_finished_handler)
|
|
|
|
# we want to cleanup files if CTRL+C is hit
|
|
def sigint_handler(sig, frame):
|
|
print('Cleaning up...')
|
|
player.quit()
|
|
sys.exit(0)
|
|
signal.signal(signal.SIGINT, sigint_handler)
|
|
|
|
# we want to register USR1 signal to skip track
|
|
def sigusr1_handler(sig, frame):
|
|
player.next()
|
|
signal.signal(signal.SIGUSR1, sigusr1_handler)
|
|
|
|
while True:
|
|
tracks = jamendo_client.tracks(
|
|
args.tags,
|
|
offset=track_offset,
|
|
order=config['jamendo']['Order'] if 'Order' in config['jamendo'] else 'relevance',
|
|
audioformat=config['jamendo']['AudioFormat'] if 'AudioFormat' in config['jamendo'] else 'ogg',
|
|
vocalinstrumental=config['jamendo']['VocalInstrumental'] if 'VocalInstrumental' in config['jamendo'] else None,
|
|
acousticelectric=config['jamendo']['AcousticElectric'] if 'AcousticElectric' in config['jamendo'] else None,
|
|
speed=config['jamendo']['Speed'] if 'Speed' in config['jamendo'] else None
|
|
)
|
|
|
|
# Break loop if we cannot get more results
|
|
if len(tracks) == 0:
|
|
logger.error('Search requests had no more results. Please check your settings.')
|
|
break
|
|
|
|
# Download files and put them on the playlist
|
|
for track in tracks:
|
|
with tempfile.NamedTemporaryFile('wb', delete=False) as f:
|
|
audio_url = track['audio']
|
|
audio_file = f.name
|
|
logger.debug(f'Downloading audio {audio_url} to {audio_file}...')
|
|
jamendo_client.download(audio_url, f)
|
|
|
|
with tempfile.NamedTemporaryFile('wb', delete=False) as f:
|
|
cover_url = track['album_image']
|
|
cover_file = f.name
|
|
logger.debug(f'Downloading cover {cover_url} to {cover_file}...')
|
|
jamendo_client.download(cover_url, f)
|
|
|
|
player.add_file(audio_file, track['artist_name'], track['album_name'], track['name'], cover_file)
|
|
|
|
# Wait while we have enough files in queue
|
|
while player.queue_size > 2:
|
|
sleep(1)
|
|
logger.debug('Remaining tracks in queue: ' + str(player.queue_size))
|
|
|
|
track_offset += 5
|
|
|
|
# Wait for last track to finish
|
|
while True:
|
|
sleep(1)
|
|
|
|
if __name__ == '__main__':
|
|
main() |