#!/usr/bin/python3 import subprocess import re import time # simul_vol.py 20220608 # default mpc volume mpc_vol = 10 subprocess.run("mpc volume " + str(mpc_vol), shell=True) # default pulseaudio(vol) pulse_vol = 20 # default radio_dir radio_dir = "/home/db650/radio/" # pulseaudio(radiko) pulse = "ps ax | grep pulseaudio | grep -v grep | awk '{print $NF}'" pulse = (subprocess.Popen(pulse, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') pulse = pulse.strip() if pulse == '--start': subprocess.run("pactl set-sink-volume 0 " + str(pulse_vol) + "%", shell=True) radiko_id = "ps ax | grep 'streamlink -p ffplay' | grep -v grep | awk -F / '{print $NF}' | awk '{print $1}'" radiko_id = (subprocess.Popen(radiko_id, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') radiko_id = radiko_id.strip() radiko_id = "radiko/" + radiko_id time.sleep(1) with open(radio_dir+"vol.list", 'r') as f: for id in f: if re.search((radiko_id), id): v = id[-4:] v = v.strip() subprocess.run("pactl set-sink-volume 0 " + str(v) + "%", shell=True) # mpc command = ("mpc playlist") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() http = ("http" in proc) # moode audio def moode(): time.sleep(1) moodeid = (subprocess.Popen("cat /tmp/moode_id", stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') moodeid = moodeid.strip() if len(moodeid) != 0: subprocess.run("mpc volume " + str(mpc_vol), shell=True) with open(radio_dir+"vol.list", 'r') as f: for id in f: if re.search((moodeid), id): v = id[-4:] subprocess.run("mpc volume " + v, shell=True) break # http True if http == True: csra = ("jp/30" in proc) nhk = ("live/2023" in proc) subprocess.run("mpc volume " + str(mpc_vol), shell=True) # CSRA if csra == True: command = ("mpc playlist | grep -oP 'jp/30.{0,3}' | cut -b 4-") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() with open(radio_dir+"vol.list", 'r') as f: for id in f: if re.search(proc, id): v = id[6:9] subprocess.run("mpc volume " + v, shell=True) break # NHK if nhk == True: command = ("mpc playlist | grep -oP 'live.{0,7}' | awk '{print substr($0,6,6)}'") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() with open(radio_dir+"vol.list", 'r') as f: for id in f: if re.search(proc, id): v = id[8:11] subprocess.run("mpc volume " + v, shell=True) break #else: if csra == False and nhk == False: time.sleep(0.5) moode() # cfg_radio.csv if http == False: csv_moode = "" with open(radio_dir+"cfg_radio.csv", 'r') as f: for csvid in f: if re.search((proc), csvid): csv_moode = csvid if len(csv_moode) != 0 and len(proc) != 0: csv_moode = csv_moode.strip() command = ("echo " + csv_moode + " | awk -F',' '{print $2}'") url = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') url = url.strip() csra = ("jp/30" in url) nhk = ("live/2023" in url) # CSRA if csra == True: command = ("echo " + url + " | grep -oP 'jp/30.{0,3}' | cut -b 4-") csraid = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') csraid = csraid.strip() if len(csraid) != 0: with open(radio_dir+"vol.list", 'r') as f: for id in f: if re.search((csraid), id): v = id[6:9] subprocess.run("mpc volume " + v, shell=True) break # NHK if nhk == True: command = ("echo " + url + " | grep -oP 'live/2023.{0,3}' | cut -b 6-") nhkid = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') nhkid = nhkid.strip() if len(nhkid) != 0: with open(radio_dir+"vol.list", 'r') as f: for id in f: if re.search((nhkid), id): v = id[8:11] subprocess.run("mpc volume " + v, shell=True) break if csra == False and nhk == False: time.sleep(0.5) moode() else: time.sleep(0.5) moode()