Advertisement
codemonkey

networkmanagers.py

Oct 11th, 2011
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. """
  2.     Contains network managers that fetches data for streams
  3. """
  4.  
  5. import sys, os
  6.  
  7. class NetworkManagerNotFoundException(Exception):
  8.     pass
  9.  
  10. class NetworkManagerReturnedErrorException(Exception):
  11.     pass
  12.  
  13. class JustintvManager:
  14.     def getStreamInfo(self, channelName):
  15.         """ Return a StreamInfo model instance """
  16.        
  17.         from website.models import StreamInfo
  18.        
  19.         ci = self.getChannelInfo(channelName)
  20.         if 'error' in ci:
  21.             raise NetworkManagerReturnedErrorException(ci['error'])
  22.  
  23.         # Create a StreamInfo object and map all the info      
  24.         sinfo = StreamInfo()
  25.         sinfo.title = ci['title']
  26.         sinfo.description = ci['description']
  27.         sinfo.about = ci['about']
  28.        
  29.         sinfo.stream_url = ci['channel_url']
  30.        
  31.         sinfo.backgroundImage = ci['channel_background_image_url']
  32.         sinfo.headerImage = ci['channel_header_image_url']
  33.         sinfo.mediumChannelImage = ci['image_url_medium']
  34.         sinfo.largeChannelImage = ci['image_url_large']
  35.        
  36.         sinfo.mediumScreenCap = ci['screen_cap_url_medium']
  37.         sinfo.largeScreenCap = ci['screen_cap_url_large']
  38.        
  39.         sinfo.backgroundColor = ci['channel_background_color']
  40.         sinfo.columnColor = ci['channel_column_color']
  41.         sinfo.textColor = ci['channel_text_color']
  42.         sinfo.linkColor = ci['channel_link_color']
  43.        
  44.         return sinfo
  45.    
  46.     def getStreamOnline(self, channelName, log = False):
  47.         return self.getChannelLive(channelName, log)
  48.        
  49.     def getChannelInfo(self, channelName):
  50.         """ Get channel info from justintv REST API """
  51.         from json import loads
  52.                
  53.         client = self.getClient()
  54.         response = client.get('/channel/show/%s.json' % channelName)
  55.  
  56.         result = loads(response.read())
  57.        
  58.         return result
  59.        
  60.     def getChannelLive(self, channelName, log):
  61.         """ Check if the stream is live using justintv REST API """
  62.         from json import loads
  63.         client = self.getClient()
  64.        
  65.         response = client.get('/stream/list.json?channel=%s' % channelName)
  66.         result = loads(response.read())
  67.        
  68.         # Log the response
  69.         if(log):
  70.             scrpath = os.path.dirname(os.path.abspath(__file__))
  71.            
  72.             sys.path.append(os.path.join(scrpath, "logs"))
  73.             from logger import log
  74.             log(os.path.join(scrpath, "logs/JustintvManager.getChannelLive"), result)
  75.        
  76.         if 'error' in result:
  77.             raise NetworkManagerReturnedErrorException(result['error'])
  78.            
  79.         # If the response is [], the channel is not live
  80.         if(len(result) == 0): return False
  81.        
  82.         return True
  83.    
  84.     def getClient(self):
  85.         """ Return an authenticated justintv client object """
  86.         from JtvClient import JtvClient
  87.         from jtvSettings import CONSUMER_KEY, CONSUMER_SECRET
  88.        
  89.         return JtvClient(CONSUMER_KEY, CONSUMER_SECRET)
  90.        
  91. def get_manager(name):
  92.     """ Returns the requested network manager or raises an exception """
  93.    
  94.     if(managers[name]):
  95.         return managers[name]()
  96.    
  97.     else:
  98.         raise NetworkManagerNotFoundException()
  99.    
  100. managers = {
  101.     "justin.tv" : JustintvManager
  102. }
  103.  
  104.  
  105.  
  106. """
  107.     DEBUG
  108. """
  109.  
  110. if(__name__ == "__main__"):
  111.     import sys
  112.     sys.path.append('C:/pymodules/')
  113.     sys.path.append('C:/workspace/')
  114.     sys.path.append('C:/workspace/honstreams')
  115.     sys.path.append('C:/workspace/honstreams/website')
  116.    
  117.     man = JustintvManager()
  118.     info = man.getStreamInfo('angrytestie')
  119.    
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement