64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
import os
|
|
import requests
|
|
import logging
|
|
|
|
# Configuration
|
|
MUSIC_BASE_DIRECTORY = "/home/pi/music/"
|
|
SOUND_SCANNING = "/home/pi/piplayer2/sounds/scanning.mp3"
|
|
SOUND_OK = "/home/pi/piplayer2/sounds/ok-05.mp3"
|
|
SOUND_SCAN_FAIL = "/home/pi/piplayer2/sounds/fail-05.mp3"
|
|
SOUND_PLAYBACK_ERROR = "/home/pi/piplayer2/sounds/error.mp3"
|
|
|
|
MOPIDY_URL = 'http://127.0.0.1:6680/mopidy/rpc'
|
|
MUSIC_EXTENSIONS = ['.aac', '.ac3', '.aiff', '.amr', '.au', '.flac', '.m4a', '.mid', '.mka', '.mp3', '.ogg', '.ra', '.voc', '.wav', '.wma']
|
|
HEADERS = { "Content-Type": "application/json" }
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s - %(message)s')
|
|
|
|
|
|
def call(method, params=None):
|
|
if (params is None):
|
|
json = '{"jsonrpc": "2.0", "id": 1, "method": "' + method + '"}'
|
|
else:
|
|
json = '{"jsonrpc": "2.0", "id": 1, "method": "' + method + '" , "params": ' + params + '}'
|
|
|
|
response = requests.post(MOPIDY_URL, data=json, headers=HEADERS)
|
|
logging.debug(response.text)
|
|
|
|
def clear_playlist():
|
|
call('core.tracklist.clear')
|
|
|
|
def add_uri(filename):
|
|
logging.debug('Adding ' + filename)
|
|
call('core.tracklist.add', '{"uri": "' + filename + '"}')
|
|
|
|
def start_playing():
|
|
return True
|
|
|
|
def play_sound(filename):
|
|
clear_playlist()
|
|
add_file(filename)
|
|
start_playing()
|
|
|
|
def list_files(dir):
|
|
r = []
|
|
for path, dirs, files in os.walk(dir):
|
|
dirs.sort()
|
|
files.sort()
|
|
for name in files:
|
|
r.append(os.path.join(path, name))
|
|
return r
|
|
|
|
|
|
|
|
|
|
clear_playlist();
|
|
|
|
filenames = list_files('/home/tilman/Musik')
|
|
|
|
for filename in filenames:
|
|
file_uri = 'file://' + filename
|
|
add_uri(file_uri)
|
|
|
|
|