Advertisement
Guest User

fmplapla.py

a guest
May 19th, 2019
2,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. """
  5. usage: fmplapla.py --station fmrara768 --time 1800 | mplayer.exe - -dumpstream -dumpfile out.ogg
  6.  
  7. """
  8.  
  9. import sys
  10. import time
  11. import argparse
  12. #!pip install websocket-client
  13. import websocket
  14.  
  15. class fmplapla:
  16.     def __init__(self, station_id, duration=0):
  17.         self.station_id = station_id
  18.         self.duration = duration
  19.         self.start_time = time.time()
  20.         self.count = 0
  21.         #websocket.enableTrace(True)
  22.         self.ws = websocket.WebSocketApp('wss://fmplapla.com/socket', on_open=self._on_open, on_message=self._on_message)
  23.         try:
  24.             self.ws.run_forever()
  25.         except (Exception, KeyboardInterrupt, SystemExit) as e:
  26.             self.ws.close()
  27.    
  28.     def _on_message(self, data):
  29.         if data:
  30.             sys.stdout.buffer.write(data)
  31.         if self.duration > 0:
  32.             if self.duration < (time.time() - self.start_time):
  33.                 raise KeyboardInterrupt
  34.         self.ws.send('{{"method":"continue","count":{count}}}'.format(count=self.count))
  35.         self.count += 1
  36.    
  37.     def _on_open(self):
  38.         self.ws.send('{{"method":"start","station":"{station}","burst":5}}'.format(station=self.station_id))
  39.  
  40. def main():
  41.     parser = argparse.ArgumentParser(description='example: python fmplapla.py -s fmrara768 -t 1800 | mplayer -')
  42.     parser.add_argument('-s', '--station', required=True, help='station id. example: fmrara768')
  43.     parser.add_argument('-t', '--time', type=int, default=0, help='stop writing the output after its seconds reaches duration. it defaults to 0, meaning that loop forever.')
  44.     args = parser.parse_args()
  45.     radio = fmplapla(args.station, args.time)
  46.  
  47. if __name__ == '__main__':
  48.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement