/halite/halite.py

https://github.com/prehensile/halite
Python | 173 lines | 121 code | 39 blank | 13 comment | 26 complexity | 942c42beef16b442084ffe8e857bfbd3 MD5 | raw file
  1. #!/usr/bin/env python
  2. import plistlib
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. import listenercounter
  8. from halitespotify import URLHandlerSpotify
  9. from haliteyoutube import URLHandlerYouTube
  10. from halitewrappers import MPlayerWrapper
  11. from halitelogging import Logger
  12. from haliteconfig import HaliteConfig
  13. from haliteplaylist import PlaylistHandler
  14. config = HaliteConfig.default_config()
  15. PIDFILE = config.get("halite","pidfile")
  16. COMMANDFILE = config.get("halite","commandfile")
  17. class FileHandler():
  18. def handled_extensions( self ):
  19. return []
  20. def handle_file( self, filepath ):
  21. self.filepath = filepath
  22. class FileHandlerURL():
  23. def __init__( self ):
  24. self.handlers = [ URLHandlerYouTube(),
  25. URLHandlerSpotify() ];
  26. def handled_extensions( self ):
  27. return[ ".webloc", ".txt", ".url" ]
  28. def handle_url( self, url ):
  29. handled = False
  30. # find a handler for the url
  31. for url_handler in self.handlers:
  32. # if we find one..
  33. if( url_handler.handles_url( url ) ):
  34. # process the url
  35. url_handler.handle_url( url )
  36. # exit the loop
  37. handled = True
  38. break;
  39. return( handled )
  40. def handle_file( self, filepath ):
  41. # get a url
  42. fh = open( filepath )
  43. fstring = fh.read()
  44. if( "plist" in fstring ):
  45. # file is a .webloc plist
  46. pl = plistlib.readPlist( filepath )
  47. self.handle_url( pl["URL"] )
  48. else:
  49. # step through lines, trying to handle each one
  50. lines = fstring.splitlines()
  51. for line in lines:
  52. if( self.handle_url( line ) ):
  53. break
  54. fh.close()
  55. class FileHandlerMediaFile():
  56. def handled_extensions( self ):
  57. return[ ".mp3", ".mp4", ".m4a" ]
  58. def handle_file( self, filepath ):
  59. Logger.log_message( "-> handle file: %s" % filepath )
  60. mplayer_wrapper = MPlayerWrapper()
  61. mplayer_wrapper.play_file( filepath )
  62. def start():
  63. if( os.path.isfile(PIDFILE) ):
  64. Logger.log_message( "There is already an instance of Halite running." )
  65. exit()
  66. else:
  67. fh = open( PIDFILE, "w" )
  68. fh.write( "%d" % os.getpid() )
  69. fh.close()
  70. Logger.log_message( "Halite start, pid is %d" % os.getpid() )
  71. file_handlers = [ FileHandlerMediaFile(),
  72. FileHandlerURL() ]
  73. playlist_handler = PlaylistHandler()
  74. running = True
  75. while running:
  76. # get a new file
  77. file = playlist_handler.get_next_file()
  78. (path, ext) = os.path.splitext(file)
  79. # play it
  80. handled = False
  81. for file_handler in file_handlers:
  82. handled_extensions = file_handler.handled_extensions()
  83. for handled_extension in handled_extensions:
  84. if( handled_extension == ext ):
  85. file_handler.handle_file( file )
  86. handled = True
  87. if( handled ):
  88. break
  89. if( handled ):
  90. break
  91. # check contol file
  92. if( os.path.isfile(COMMANDFILE) ):
  93. Logger.log_message( "Halite: has a commandfile" )
  94. fh = open( COMMANDFILE )
  95. command = fh.read()
  96. fh.close()
  97. Logger.log_message( "-> command is %s" % command )
  98. if( command == "stop" ):
  99. running = False
  100. os.remove( COMMANDFILE )
  101. Logger.log_message( "Halite: end main loop" )
  102. os.remove(PIDFILE)
  103. def write_command( command ):
  104. fh = open(COMMANDFILE, "w")
  105. fh.write( exec_command )
  106. fh.close()
  107. def main():
  108. if( len(sys.argv) > 1 ):
  109. exec_command = sys.argv[1]
  110. if( exec_command == "connect" ):
  111. listener_counter = listenercounter.ListenerCounter()
  112. num_listeners = listener_counter.increment_count()
  113. Logger.log_message( "Halite: listener connect, new num_listeners=%d" % num_listeners )
  114. if( num_listeners > 0 ):
  115. start()
  116. elif( exec_command == "disconnect" ):
  117. listener_counter = listenercounter.ListenerCounter()
  118. num_listeners = listener_counter.decrement_count()
  119. Logger.log_message( "Halite: listener disconnect, new num_listeners=%d" % num_listeners )
  120. if( num_listeners < 1 ):
  121. write_command( "stop" )
  122. elif( exec_command == "start" ):
  123. # Logger.PRINT_MESSAGES = False
  124. Logger.LOG_TO_USERCONSOLE = None
  125. start()
  126. else:
  127. # write command to COMMANDFILE so running instance can use it
  128. if( os.path.isfile(PIDFILE) ):
  129. write_command( exec_command )
  130. else:
  131. Logger.log_message( "There is no instance of Halite running" )
  132. else:
  133. Logger.log_message( "No argument provided" )
  134. main()