Skip to content

Instantly share code, notes, and snippets.

@gandolf0
Created January 21, 2017 04:06
Show Gist options
  • Save gandolf0/3978830dade06fa5ce2971126b60df20 to your computer and use it in GitHub Desktop.
Save gandolf0/3978830dade06fa5ce2971126b60df20 to your computer and use it in GitHub Desktop.
PianobarMonitor
#!/usr/bin/python
import subprocess
import re
import os
import select
import sys
import time
import urllib2
PIANOBAR_FOLDER = os.path.join(os.path.expanduser('~'), '.config/pianobar')
def internet_on():
try:
urllib2.urlopen('http://216.58.192.142', timeout=1)
return True
except:
return False
def get_fingerprint():
print ('Updating TLS Fingerprint...')
connected = False
while not connected:
print ('checking internets')
time.sleep(5)
connected = internet_on()
command = '''openssl s_client -connect tuner.pandora.com:443 < /dev/null 2> /dev/null | openssl x509 -noout -fingerprint | tr -d ':' | cut -d'=' -f2'''
finger = os.popen(command).read()
fingerprint = 'tls_fingerprint = ' + finger
with open(pianobar_config, 'r+') as f:
all_lines = f.readlines()
for i in range(len(all_lines)):
if 'tls_fingerprint' in all_lines[i]:
all_lines[i] = fingerprint
f.seek(0)
f.writelines(all_lines)
f.truncate()
class PianoBar:
def __init__(self, *args, **kwargs):
""" Setup defaults.
:param args:
"""
self.number_of_stations = 0
def start_pianobar(self):
""" Starts pianobar.
:param user: pandora username, probably a full email address
:param station: initial station selection for the user
"""
pb_process = subprocess.Popen(['/usr/local/bin/pianobar'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return pb_process
if __name__ == '__main__':
station_regex = re.compile(r'[0-9]\) ')
sentry_regex = re.compile(r'\[?\] Select station')
radio = None
num_stations_file = '/home/pi/.config/pianobar/station_file'
pianobar_config = '/home/pi/.config/pianobar/config'
get_fingerprint()
while True:
radio = PianoBar()
pb_sub_process = radio.start_pianobar()
inputs = [pb_sub_process.stdout.fileno(), pb_sub_process.stderr.fileno()]
while True:
readers, writers, exceptions = select.select(inputs, [], inputs)
if not readers:
time.sleep(0.3)
continue
for reader in readers:
if reader == pb_sub_process.stdout.fileno():
read = pb_sub_process.stdout.readline()
elif reader == pb_sub_process.stderr.fileno():
read = pb_sub_process.stderr.readline()
if station_regex.search(read):
radio.number_of_stations += 1
elif sentry_regex.search(read):
print("stations:")
print(str(radio.number_of_stations))
with open('/home/pi/.config/pianobar/station_file', 'w') as f:
f.write(str(radio.number_of_stations))
sys.stdout.write(read)
sys.stdout.flush()
if pb_sub_process.poll() is not None: #pb went away
#time.sleep(2)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment