home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume30 / radio2.0 / part02 / ttytuner.py < prev   
Encoding:
Python Source  |  1992-06-29  |  6.8 KB  |  267 lines

  1. #!/usr/local/python
  2.  
  3. # /***********************************************************
  4. # Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
  5. # Netherlands.
  6. #                         All Rights Reserved
  7. # Permission to use, copy, modify, and distribute this software and its 
  8. # documentation for any purpose and without fee is hereby granted, 
  9. # provided that the above copyright notice appear in all copies and that
  10. # both that copyright notice and this permission notice appear in 
  11. # supporting documentation, and that the names of Stichting Mathematisch
  12. # Centrum or CWI not be used in advertising or publicity pertaining to
  13. # distribution of the software without specific, written prior permission.
  14. # STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  15. # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. # FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  17. # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  19. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  20. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. # ******************************************************************/
  22.  
  23. # A tuner to control radio programs, by Jack Jansen.
  24.  
  25. import sys
  26. import os
  27. import time
  28. import socket
  29. import time
  30. import string
  31. import getopt
  32. from stat import ST_MTIME
  33.  
  34.  
  35. RADIOCTLPORT=54320
  36. TRANSMITTERCTLPORT=54319
  37. CTLWS=socket.gethostname()
  38.  
  39. # The list of networks to broadcast on, when looking for radio
  40. # stations. This should somehow be gotten differently.
  41. #
  42. MCASTLIST = ['192.16.184.0', '192.16.191.0', '192.16.201.255']
  43. # MCASTLIST = ['192.16.201.255']
  44.  
  45. class struct(): pass
  46. info = struct()
  47.  
  48. #
  49. # sendsock - send a message (and get optional reply)
  50. #
  51. def sendsock(host, port, msg, needrepl):
  52.     try:
  53.     host = socket.gethostbyname(host)
  54.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  55.     s.sendto(msg, (host,port))
  56.     if needrepl:
  57.         # Loop for max. 2.5 seconds waiting for a reply
  58.         i = 0
  59.         while i < 5 and not s.avail():
  60.         time.millisleep(500)
  61.         i = i + 1
  62.         if not s.avail():
  63.         print 'Radio program not responding'
  64.         return ''
  65.         return s.recv(500)
  66.     except socket.error:
  67.     print 'Incorrect radio settings'
  68.     if needrepl:
  69.         return ''
  70.     else:
  71.         return
  72. #
  73. # sendmulti - send a multicast message (and get replies)
  74. #
  75. def sendmulti(mcastlist, port, msg):
  76.     rv = []
  77.     try:
  78.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  79.     s.allowbroadcast(1)
  80.     for host in mcastlist:
  81.         s.sendto(msg, (host,port))
  82.     # Loop for max. 2.5 seconds waiting for a reply
  83.     i = 0
  84.     while i < 25:
  85.         time.millisleep(100)
  86.         i = i + 1
  87.         if s.avail():
  88.         rv.append(s.recv(500))
  89.     except socket.error:
  90.     print 'Incorrect mcast settings'
  91.     return rv
  92. #
  93. # getstationinfo - Return playlist, current and playing time (in minutes).
  94. # Returns a triple (playlist-filename, current record, playing time)
  95. #
  96. def getstationinfo(info):
  97.     name = info[0]
  98.     if len(info) > 3:
  99.     playlist = info[3]
  100.     else:
  101.     playlist = '/ufs/' + name + '/CDlog'
  102.     if len(info) > 5:
  103.     since = eval(info[4])
  104.     cur = string.joinfields(info[5:], ':')
  105.     else:
  106.     curfn = '/ufs/' + name + '/CD'
  107.     try:
  108.         curf = open(curfn,'r')
  109.         cur = curf.readline()
  110.         curf.close()
  111.     except IOError:
  112.         cur = '???'
  113.     if cur[-1:] == '\n':
  114.         cur = cur[:-1]
  115.     try:
  116.         sb = os.stat(curfn)
  117.         since = time.time() - sb[ST_MTIME]
  118.     except os.error:
  119.         since = -1
  120.     if since >= 0: since = since / 60 # Convert to minutes
  121.     if since < 0:
  122.     str = '??'
  123.     elif since < 60:
  124.     str = `since` + ' mins'
  125.     else:
  126.     since = since / 60
  127.     if since < 24:
  128.         str = `since` + ' hrs'
  129.     else:
  130.         str = `since / 24` + ' days'
  131.     return playlist, cur, str
  132.  
  133. #
  134. # updateinfo - Update one of the views with info on station 'name'
  135. #
  136. def printinfo(info):
  137.     name = info[0]
  138.     print 'Station '+ name + ':'
  139.     print '\tPort               ' + `info[1]`
  140.     if name == '' or name[:3] == '???':
  141.     print '\tNo information on station'
  142.     else:
  143.     try:
  144.         p, c, t = getstationinfo(info)
  145.     except IOError:
  146.         return
  147.     if c <> '' and c <> '???':
  148.         print '\tCurrently playing: ' + c
  149.     if t <> '??':
  150.         print '\tSince:             ' + t
  151.     if p <> '':
  152.         print '\tPlaylist in file:  ' + p
  153.  
  154. #
  155. # getstations - Return an array of (stationname, stationport)
  156. # listing all available stations.
  157. #
  158. def getstations():
  159.     stations = []
  160.     raw = sendmulti(MCASTLIST, TRANSMITTERCTLPORT, 'radio:s')
  161.     for i in raw:
  162.     if i[:7] == 'radio:S':
  163.         fields = string.splitfields(i,':')[2:]
  164.         fields[1] = string.atoi(fields[1])
  165.         stations.append(fields)
  166.     else:
  167.         print 'Funny reply from transmitter:', i[:7]
  168.     return stations
  169.  
  170. #
  171. # getcurstation - Return (name,port) for station to which radio
  172. # on workstation ws is currently tuned to.
  173. #
  174. def getcurstation(stationlist, ws, port):
  175.     rv = sendsock(ws, port, 'radio:i', 1)
  176.     if rv == '':
  177.     return ('',0)
  178.     if rv[0:8] <> 'radio:I:':
  179.     print 'Illegal reply from radio:',rv
  180.     return ('',0)
  181.     # Remove optional pause field
  182.     rv = rv[8:]
  183.     rv = string.splitfields(rv,':')
  184.     rv = rv[len(rv)-1]
  185.     tport = string.atoi(rv)
  186.     for i in stationlist:
  187.     if i[1] == tport:
  188.         return i
  189.     return ('???(port '+`tport`+')' ,tport)
  190.  
  191. def main():
  192.     radio_port = RADIOCTLPORT
  193.     radio_ws = CTLWS
  194.     try:
  195.     optlist, args = getopt.getopt(sys.argv[1:], 'w:p:lLcPC')
  196.     mode = ''
  197.     for o, a in optlist:
  198.         if o == '-w':
  199.         radio_ws = a
  200.         elif o == '-p':
  201.         radio_port = string.atoi(a)
  202.         elif mode == '':
  203.         mode = o[1]
  204.         else:
  205.         raise getopt.error
  206.     if len(args) + len(mode) <> 1:
  207.         raise getopt.error
  208.     except getopt.error:
  209.     print 'Usage: '+sys.argv[0]+' [options] command'
  210.     print 'Options:'
  211.     print '\t-w ws\tControl radio on workstation ws'
  212.     print '\t-p port\tControl radio on port port'
  213.     print 'Command:'
  214.     print '\t-l\tList names of active stations'
  215.     print '\t-L\tList names and info of active stations'
  216.     print '\t-c\tList info on current station'
  217.     print '\t-P\tTemporarily suspend radio, freeing port'
  218.     print '\t-C\tContinue radio'
  219.     print '\tstation\tTune to new station'
  220.     sys.exit(1)
  221.     
  222.     #
  223.     # First, do pause/continue command.
  224.     #
  225.     if mode == 'P':
  226.     sendsock(radio_ws, radio_port, 'radio:0', 0)
  227.     sys.exit(0)
  228.     if mode == 'C':
  229.     sendsock(radio_ws, radio_port, 'radio:1', 0)
  230.     sys.exit(0)
  231.     #
  232.     # And list of new stations
  233.     #
  234.     stations = getstations()
  235.     #
  236.     # Set info for current station
  237.     #
  238.     if mode == '' or mode == 'c':
  239.     cn = getcurstation(stations,radio_ws, radio_port)
  240.     if mode == 'l':
  241.     for i in stations:
  242.         print i[0]
  243.     elif mode == 'L':
  244.     for i in stations:
  245.         printinfo(i)
  246.     elif mode == 'c':
  247.     printinfo(cn)
  248.     else:
  249.     for i in range(len(stations)):
  250.         if stations[i][0] == args[0]:
  251.         sendsock(radio_ws, radio_port, \
  252.                 'radio:t:' + `stations[i][1]`, 0)
  253.         sys.exit(0)
  254.     print 'No such station: ', args[0]
  255.     
  256.     
  257.  
  258. main()
  259.  
  260. # Local variables:
  261. # py-indent-offset: 4
  262. # end:
  263.