#!/usr/bin/python3 from tkinter import * from tkinter import ttk from tkinter.ttk import * from tkinter import messagebox from tkinter.font import Font import subprocess import time import re import os # --- default set --- # default programs dir bin_dir = "/home/pi/bin/" # default radio_dir radio_dir = "/home/pi/radio/" # default favorite_dir favorite_dir = "/home/pi/radio/FAVORITE/" # default area t = favorite_dir+"favorite1" # jcb_play #jcb_play = "" # on air station std= "                               " st = "           ☆  Simul Radio  ☆          " # default player #player = "mpv" player = "'ffplay -nodisp'" # default radiko_vol radiko_vol = 20 # default mpc vol vo = 10 # cfg_radio_csv command = ("test -e " + radio_dir + "cfg_radio.csv" + " ; " + "echo $?") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() if proc == str(1): subprocess.run("touch " + radio_dir + "cfg_radio.csv", shell=True) # --- run --- # play def play_click(): subprocess.run(bin_dir + "killall.sh", shell=True) play_selection() # vol edit def vol_click(): subprocess.run("xterm -geometry 25x35 -e vi " + radio_dir + "vol &", shell=True) # area edit def area_click(): subprocess.run("xterm -geometry 130x15 -e vi " + t + " &", shell=True) # cfg_radio.csv def cfg_radio_click(): proc = messagebox.askokcancel('cfg_radio.csv', "cfg_radio.csv GET") if proc == True: subprocess.run("sqlite3 -csv /var/local/www/db/moode-sqlite3.db 'SELECT * from cfg_radio ;' > " + radio_dir + "cfg_radio.csv", shell=True) # cfg_radio.csv Empty def cfg_empty_click(): proc = messagebox.askyesno('cfg_radio.csv Empty', "cfg_radio.csv Empty ?") if proc == True: subprocess.run(": > " + radio_dir + "cfg_radio.csv", shell=True) # favorite def favorite1_click(): global t; t=favorite_dir+"favorite1"; area_selection();x();y() def favorite2_click(): global t; t=favorite_dir+"favorite2"; area_selection();x();y() def favorite3_click(): global t; t=favorite_dir+"favorite3"; area_selection();x();y() def favorite4_click(): global t; t=favorite_dir+"favorite4"; area_selection();x();y() def favorite5_click(): global t; t=favorite_dir+"favorite5"; area_selection();x();y() def moode_click(): global t; t=favorite_dir+"moode.list"; area_selection();x();y() def favorite7_click(): global vo vo = "-5"; vol_control() def favorite8_click(): global vo vo = "-2"; vol_control() def favorite9_click(): command = ("ps ax | grep pulseaudio | grep -v grep | awk '{print substr($6,1)}' | cut -b 3-8") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() if proc == 'start': # pulseaudio subprocess.run("pactl set-sink-volume 0 " + str(radiko_vol) + "%", shell=True) else: subprocess.run(bin_dir + "simul-vol.py", shell=True) def favorite10_click(): global vo vo = "+2"; vol_control() def favorite11_click(): global vo vo = "+5"; vol_control() def vol_control(): command = ("ps ax | grep pulseaudio | grep -v grep | awk '{print $NF}'") pulse = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') pulse = pulse.strip() if pulse == "--start": subprocess.run("pactl set-sink-volume 0 " + str(vo) + "%", shell=True) else: subprocess.run("mpc volume " + str(vo), shell=True) # Button ON AIR def onair_click(): on_air_selection() # stop def stop_click(): global st; stp() subprocess.run(bin_dir + "killall.sh", shell=True) # Exit def exit_click(): root.quit() # --- CSRA --- def csra1_click(): global t; t=radio_dir+"CSRA/csra1"; area_selection();x();y() def csra2_click(): global t; t=radio_dir+"CSRA/csra2"; area_selection();x();y() def csra3_click(): global t; t=radio_dir+"CSRA/csra3"; area_selection();x();y() def csra4_click(): global t; t=radio_dir+"CSRA/csra4"; area_selection();x();y() def csra5_click(): global t; t=radio_dir+"CSRA/csra5"; area_selection();x();y() def csra6_click(): global t; t=radio_dir+"CSRA/csra6"; area_selection();x();y() def csra7_click(): global t; t=radio_dir+"CSRA/csra7"; area_selection();x();y() def csra8_click(): global t; t=radio_dir+"CSRA/csra8"; area_selection();x();y() # --- radiko --- def radiko_click(): global t; t=radio_dir+"RADIKO/radiko.list"; area_selection();x();y() # --- NHK --- def nhk_click(): global t; t=radio_dir+"NHK/NHK.list"; area_selection();x();y() # --- JCBA --- def jcba1_click(): global t; t=radio_dir+"JCBA/jcba1"; area_selection();x();y() def jcba2_click(): global t; t=radio_dir+"JCBA/jcba2"; area_selection();x();y() def jcba3_click(): global t; t=radio_dir+"JCBA/jcba3"; area_selection();x();y() def jcba4_click(): global t; t=radio_dir+"JCBA/jcba4"; area_selection();x();y() def jcba5_click(): global t; t=radio_dir+"JCBA/jcba5"; area_selection();x();y() def jcba6_click(): global t; t=radio_dir+"JCBA/jcba6"; area_selection();x();y() def jcba7_click(): global t; t=radio_dir+"JCBA/jcba7"; area_selection();x();y() def jcba8_click(): global t; t=radio_dir+"JCBA/jcba8"; area_selection();x();y() # Scrollbar x def x(): scrollbar = ttk.Scrollbar(frame1,orient=HORIZONTAL,command=lb.xview) lb['xscrollcommand'] = scrollbar.set scrollbar.grid(row=1,column=0,sticky=(E,W)) # Scrollbar y def y(): scrollbar = ttk.Scrollbar(frame1,orient=VERTICAL,command=lb.yview) lb['yscrollcommand'] = scrollbar.set scrollbar.grid(row=0,column=1,sticky=(N,S)) # --- selection --- # PLAY def play_selection(): global jcb_play,st,a,stname jcb_play = "" station = "" # pulseaudio command = ("ps ax | grep pulseaudio | grep -v grep | awk '{print $NF}'") pulse = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') pulse = pulse.strip() # ListBOX for i in lb.curselection(): a = (lb.get(i)); b = (lb.get(i)); c = (lb.get(i)); command = ("echo " + a + " | awk '{print $NF}'") a = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') a = a.strip() mms = (a[0:6]) command = ("echo " + b + " | grep radiko") b = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') b = b.strip() rad = (b[0:6]) if mms == ("mms://") or rad == ("radiko"): if (pulse) == "": subprocess.run("sudo runuser -l www-data -c 'pulseaudio --start'", shell=True) if mms == ("mms://"): subprocess.Popen(player + " " + c, shell=True) on_mms() break if rad == ("radiko"): command = ("echo " + c + " | grep radiko | awk '{print $NF}'") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() #subprocess.run(bin_dir + "radiko.sh -p " + proc + " 1>/dev/null 2>/dev/null &", shell=True) subprocess.run("~/.local/bin/streamlink -p " + player + " http://radiko.jp/#!/live/" + proc + " best &", shell=True) subprocess.run("pactl set-sink-volume 0 " + str(radiko_vol) + "%", shell=True) st = std; stb(); st = b; stb() break elif (pulse) == "--start": subprocess.run("sudo killall pulseaudio", shell=True) jcba = ("JCB" in a) csra = ("jp/3" in a) nhk = ("live/5" in a) stname = "" cfg_moode = "" if csra == True or jcba == True or nhk == True: if (pulse) == "--start": subprocess.run("sudo killall pulseaudio", shell=True) # CSRA if csra == True: command = ("echo " + a + " | grep -oP 'jp/30.{0,3}'") if jcba == True: command = ("echo " + a + " | grep -oP 'JCB.{0,3}'") if nhk == True: command = ("echo " + a + " | grep -oP 'live/5.{0,5}'") cfg_id = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') cfg_id = cfg_id.strip() with open(radio_dir+"cfg_radio.csv", 'r') as f: for id in f: if re.search(cfg_id, id): cfg_moode = id if len(cfg_moode) != 0: cfg_moode = cfg_moode.strip() command = ("echo " + cfg_moode + " | awk -F',' '{print $3}'") stname = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') stname = stname.strip() stname = ("'"+stname+"'" + ".pls") # cfg_radio_csv play subprocess.run("mpc load RADIO/" + stname, shell=True) subprocess.Popen("mpc play &", shell=True) # radio list url play else: subprocess.run("mpc add " + a, shell=True) subprocess.run("mpc play &", shell=True) # other if (pulse) == "--start": subprocess.run("sudo killall pulseaudio", shell=True) if csra == False and jcba == False and nhk == False: subprocess.run("mpc add " + a, shell=True) subprocess.run("mpc play &", shell=True) # On Station on_air_selection() # simul-vol.py command = ("ps ax | grep pulseaudio | grep -v grep | awk '{print $NF}'") pulse = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') pulse = pulse.strip() if not(pulse): # run simul-vol.py subprocess.run(bin_dir + "simul-vol.py", shell=True) # ON AIR def on_air_selection(): global jcb_play,st,a # playlist command = ("mpc playlist") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() http = ("http" in proc) # http if http == True: jcba = ("JCB" in proc) csra = ("jp/3" in proc) nhk = ("live/5" in proc) # 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() st = std; sta() with open(radio_dir+"CSRA/ListenRadio", 'r') as f: for id in f: if re.search(proc, id): st=id; st=st.strip(); sta() # JCBA if jcba == True: with open(radio_dir+"jcb_play", 'r') as f: jcb_play = f.read() jcb_play = jcb_play.strip() st = std; stb() with open(radio_dir+"JCBA/JCB", 'r') as f: for id in f: if re.search(jcb_play, id): st=id; st=st.strip(); stb() # 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() st = std; sta() with open(radio_dir+"NHK/NHK.txt", 'r') as f: for id in f: if re.search((proc), id): st=id; st=st.strip(); sta() # moOde if csra == False and jcba == False and nhk == 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: st = std; stc() csv_moode = csv_moode.strip() command = ("echo " + csv_moode + " | awk -F',' '{print $3}'") st = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') st = st.strip() stc() # OnAir cfg_radio_csv else: 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() jcba = ("JCB" in url) csra = ("jp/3" in url) nhk = ("live/5" in url) # CSRA if csra == True: st = std; sta() with open(radio_dir+"CSRA/ListenRadio", 'r') as f: for id in f: if re.search(proc, id): st=id; st=st.strip(); sta() # JCBA if jcba == True: st = std; stb() with open(radio_dir+"JCBA/JCB", 'r') as f: for id in f: if re.search(proc, id): st=id; st=st.strip(); stb() # NHK if nhk == True: st = std; sta() with open(radio_dir+"NHK/NHK.txt", 'r') as f: for id in f: if re.search(proc, id): st=id; st=st.strip(); sta() # moOde (ON AIR Button) else: command = ("echo " + '"'+proc+'"' + " | head -1 | cut -f 1 -d ':' | cut -f 1 -d '-'") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() if not(proc): st = std; stp() else: st = std; stc() st = proc; stc() # on station radiko #command = ("ps ax | grep rtmpdump | grep -v grep | awk '{print substr($5,1)}'") command = ("ps ax | grep radiko.jp | grep -v grep | awk '{print $(NF-1)}' | awk 'NR==1' | cut -c 8-13") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() #if proc == 'rtmpdump': # Radiko if proc == 'radiko': st = std; stb() #command = ("ps ax | grep rtmpdump | grep -v grep | awk '{print substr($9,1)}' | tr -d /_definst_") command = ("ps ax | grep radiko.jp | grep -v grep | awk '{print $(NF-1)}' | awk 'NR==1' | cut -c 26-") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() st=std; stb() with open(radio_dir+"RADIKO/radiko.list", 'r') as f: for id in f: if re.search(proc, id): st=id; st=(st[1:-2]); stb() # mms def on_mms(): global st command = ("ps ax | grep mms | grep -v grep | awk 'NR==1' | awk '{print $NF}'") proc = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') proc = proc.strip() st = std; sta() with open(radio_dir+"CSRA/mms.list", 'r') as f: for id in f: if re.search(proc, id): st=id; st=st.strip(); mms() # AREA def area_selection(): global v1,lb with open(t, 'r') as f: proc = f.read() currencies = (proc) v1 = StringVar(value=currencies) lb = Listbox(frame1, listvariable=v1, background=bg_color, foreground=fg_color,height=lb_h,width=lb_w) lb.grid(row=0, column=0) # Label CSRA NHK def sta(): label1 = ttk.Label( frame0, #font=('Helvetica', 10), background='#BF538C', foreground='#ffffff', text=st) label1.grid(row=0,column=0) # Label JCBA RADIKO def stb(): label1 = ttk.Label( frame0, #font=('Helvetica', 10), background='#06793C', foreground='#ffffff', text=st) label1.grid(row=0,column=0) # Label moOde def stc(): label1 = ttk.Label( frame0, #font=('Helvetica', 10), background='#4489B7', foreground='#ffffff', text=st) label1.grid(row=0,column=0) # Label default def stp(): global st st = "           ☆  Simul Radio  ☆          " label1 = ttk.Label( frame0, #font=('Helvetica', 10), background='#333333', foreground='#ffffff', text=st) label1.grid(row=0,column=0) # Label mms play def mms(): global st command = ("echo " + st + " | awk '{print $NF}'") st = (subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]).decode('utf-8') st = ("mms:// " + st); st = st.strip() label1 = ttk.Label( frame0, #font=('Helvetica', 10), background='#BF538C', foreground='#ffffff', text=st) label1.grid(row=0,column=0) subprocess.run(radio_dir+"CSRA/mms-pulse_vol.py", shell=True) # --- main ---- if __name__ == '__main__': root = Tk() root.title('Simul Radio') root.configure(background='#333333') # Background color style = ttk.Style() style.theme_use('default') #('clam', 'alt', 'default', 'classic') style.configure('TButton', background='#D9D9D9') # Button color # Listbox(color high-width) # 1F4660(Blue) bg_color='#222222'; fg_color='#ffffff'; lb_h = 28; lb_w = 73 # Listbox Font listfont = Font(family="Helvetica",size=9) root.option_add("*Listbox*Font", listfont) # Frame frame0 = ttk.Frame(root, padding=1) frame0.grid() frame1 = ttk.Frame( root, relief='sunken', borderwidth=1) frame1.grid() frame2 = ttk.Frame(root, padding=6) frame2.grid() frame3 = ttk.Frame(root, padding=6) frame3.grid() # label label1 = ttk.Label( frame0, text=st, font=('Helvetica', 10), background='#333333', foreground='#ffffff') label1.grid(row=0,column=0) # Listbox with open(t, 'r') as f: proc = f.read() currencies = (proc) v1 = StringVar(value=currencies) lb = Listbox(frame1, listvariable=v1,background='#222222',foreground='#ffffff', height=lb_h,width=lb_w) lb.grid(row=0, column=0) # Scrollbar y scrollbar = ttk.Scrollbar(frame1, orient=VERTICAL, command=lb.yview) lb['yscrollcommand'] = scrollbar.set scrollbar.grid(row=0,column=1,sticky=(N,S)) # Scrollbar x scrollbar = ttk.Scrollbar(frame1, orient=HORIZONTAL, command=lb.xview) lb['xscrollcommand'] = scrollbar.set scrollbar.grid(row=1,column=0,sticky=(E,W)) # Button color style = Style() style.configure('TButton', font=('Helvetica', 8), background='#E6C0D4') style.configure('W.TButton', font=('Helvetica', 8), background='#A4DCBF') style.configure('P.TButton', font=('Helvetica', 9), background='#C4CAD0') style.configure('GY.TButton', font=('Helvetica', 8), background='#C4CAD0') style.configure('SB.TButton', font=('Helvetica', 8), background='#A8DAED') style.configure('BE.TButton', font=('Helvetica', 8), background='#EDDFBD') style.configure('VI.TButton', font=('Helvetica', 8), background='#DEA6EC') style.configure('OR.TButton', font=('Helvetica', 8), background='#B5B5B5') style.configure('YE.TButton', font=('Helvetica', 8), background='#D6DB76') style.configure('GR.TButton', font=('Helvetica', 8), background='#8CF6A5') # Button frame1 button = ttk.Button(frame1, text='☆ PLAY SIMUL RADIO ☆', style='P.TButton', command=play_click) button.grid(row=2, column=0, columnspan=1) # Button frame2 button = ttk.Button(frame2, style='BE.TButton', text='favorite1', command=favorite1_click) button.grid(row=0, column=0, columnspan=1) button = ttk.Button(frame2, style='BE.TButton', text='favorite2', command=favorite2_click) button.grid(row=0, column=1, columnspan=1) button = ttk.Button(frame2, style='SB.TButton', text='moOde', command=moode_click) button.grid(row=0, column=2, columnspan=1) button = ttk.Button(frame2, style='TButton', text='NHK', command=nhk_click) button.grid(row=0, column=3, columnspan=1) button = ttk.Button(frame2, style='W.TButton', text='radiko', command=radiko_click) button.grid(row=0, column=4, columnspan=1) button = ttk.Button(frame2, style='SB.TButton', text='ON AIR', command=onair_click) button.grid(row=0, column=5, columnspan=1) button = ttk.Button(frame2, style='VI.TButton', text='STOP', command=stop_click) button.grid(row=0, column=6, columnspan=1) button = ttk.Button(frame2, style='OR.TButton', text='EXIT', command=exit_click) button.grid(row=0, column=7, columnspan=1) button = ttk.Button(frame2, style='BE.TButton', text='favorite3', command=favorite3_click) button.grid(row=1, column=0, columnspan=1) button = ttk.Button(frame2, style='BE.TButton', text='favorite4', command=favorite4_click) button.grid(row=1, column=1, columnspan=1) button = ttk.Button(frame2, style='SB.TButton', text='favorite5', command=favorite5_click) button.grid(row=1, column=2, columnspan=1) button = ttk.Button(frame2, style='GY.TButton', text='--5%', command=favorite7_click) button.grid(row=1, column=3, columnspan=1) button = ttk.Button(frame2, style='GY.TButton', text='--2%', command=favorite8_click) button.grid(row=1, column=4, columnspan=1) button = ttk.Button(frame2, style='TButton', text='Simul Vol', command=favorite9_click) button.grid(row=1, column=5, columnspan=1) button = ttk.Button(frame2, style='GY.TButton', text='+2%', command=favorite10_click) button.grid(row=1, column=6, columnspan=1) button = ttk.Button(frame2, style='GY.TButton', text='+5%', command=favorite11_click) button.grid(row=1, column=7, columnspan=1) # Button frame3 CSRA button = ttk.Button(frame3, text='北海道', command=csra1_click) button.grid(row=0, column=0, columnspan=1) button = ttk.Button(frame3, text='東北', command=csra2_click) button.grid(row=0, column=1, columnspan=1) button = ttk.Button(frame3, text='北関東', command=csra3_click) button.grid(row=0, column=2, columnspan=1) button = ttk.Button(frame3, text='南関東', command=csra4_click) button.grid(row=0, column=3, columnspan=1) button = ttk.Button(frame3, text='信・北・東海', command=csra5_click) button.grid(row=1, column=0, columnspan=1) button = ttk.Button(frame3, text='近・中・四国', command=csra6_click) button.grid(row=1, column=1, columnspan=1) button = ttk.Button(frame3, text='九州・沖縄', command=csra7_click) button.grid(row=1, column=2, columnspan=1) button = ttk.Button(frame3, text='mms', command=csra8_click) button.grid(row=1, column=3, columnspan=1) # edit button = ttk.Button(frame3, style='GY.TButton', text='cfg_Empty', command=cfg_empty_click) button.grid(row=2, column=2, columnspan=1) button = ttk.Button(frame3, style='GY.TButton', text='cfg_radio', command=cfg_radio_click) button.grid(row=2, column=3, columnspan=1) button = ttk.Button(frame3, style='GY.TButton', text='Vol Edit', command=vol_click) button.grid(row=2, column=4, columnspan=1) button = ttk.Button(frame3, style='GY.TButton', text='Area Edit', command=area_click) button.grid(row=2, column=5, columnspan=1) # Button frame3 JCBA button = ttk.Button(frame3, style='W.TButton', text='北海道・東北', command=jcba1_click) button.grid(row=0, column=4, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='北関東', command=jcba2_click) button.grid(row=0, column=5, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='南関東', command=jcba3_click) button.grid(row=0, column=6, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='甲信越・北陸', command=jcba4_click) button.grid(row=0, column=7, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='東海', command=jcba5_click) button.grid(row=1, column=4, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='近畿', command=jcba6_click) button.grid(row=1, column=5, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='中国・四国', command=jcba7_click) button.grid(row=1, column=6, columnspan=1) button = ttk.Button(frame3, style='W.TButton', text='九州', command=jcba8_click) button.grid(row=1, column=7, columnspan=1) root.mainloop()