Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Contains network managers that fetches data for streams
- """
- import sys, os
- class NetworkManagerNotFoundException(Exception):
- pass
- class NetworkManagerReturnedErrorException(Exception):
- pass
- class JustintvManager:
- def getStreamInfo(self, channelName):
- """ Return a StreamInfo model instance """
- from website.models import StreamInfo
- ci = self.getChannelInfo(channelName)
- if 'error' in ci:
- raise NetworkManagerReturnedErrorException(ci['error'])
- # Create a StreamInfo object and map all the info
- sinfo = StreamInfo()
- sinfo.title = ci['title']
- sinfo.description = ci['description']
- sinfo.about = ci['about']
- sinfo.stream_url = ci['channel_url']
- sinfo.backgroundImage = ci['channel_background_image_url']
- sinfo.headerImage = ci['channel_header_image_url']
- sinfo.mediumChannelImage = ci['image_url_medium']
- sinfo.largeChannelImage = ci['image_url_large']
- sinfo.mediumScreenCap = ci['screen_cap_url_medium']
- sinfo.largeScreenCap = ci['screen_cap_url_large']
- sinfo.backgroundColor = ci['channel_background_color']
- sinfo.columnColor = ci['channel_column_color']
- sinfo.textColor = ci['channel_text_color']
- sinfo.linkColor = ci['channel_link_color']
- return sinfo
- def getStreamOnline(self, channelName, log = False):
- return self.getChannelLive(channelName, log)
- def getChannelInfo(self, channelName):
- """ Get channel info from justintv REST API """
- from json import loads
- client = self.getClient()
- response = client.get('/channel/show/%s.json' % channelName)
- result = loads(response.read())
- return result
- def getChannelLive(self, channelName, log):
- """ Check if the stream is live using justintv REST API """
- from json import loads
- client = self.getClient()
- response = client.get('/stream/list.json?channel=%s' % channelName)
- result = loads(response.read())
- # Log the response
- if(log):
- scrpath = os.path.dirname(os.path.abspath(__file__))
- sys.path.append(os.path.join(scrpath, "logs"))
- from logger import log
- log(os.path.join(scrpath, "logs/JustintvManager.getChannelLive"), result)
- if 'error' in result:
- raise NetworkManagerReturnedErrorException(result['error'])
- # If the response is [], the channel is not live
- if(len(result) == 0): return False
- return True
- def getClient(self):
- """ Return an authenticated justintv client object """
- from JtvClient import JtvClient
- from jtvSettings import CONSUMER_KEY, CONSUMER_SECRET
- return JtvClient(CONSUMER_KEY, CONSUMER_SECRET)
- def get_manager(name):
- """ Returns the requested network manager or raises an exception """
- if(managers[name]):
- return managers[name]()
- else:
- raise NetworkManagerNotFoundException()
- managers = {
- "justin.tv" : JustintvManager
- }
- """
- DEBUG
- """
- if(__name__ == "__main__"):
- import sys
- sys.path.append('C:/pymodules/')
- sys.path.append('C:/workspace/')
- sys.path.append('C:/workspace/honstreams')
- sys.path.append('C:/workspace/honstreams/website')
- man = JustintvManager()
- info = man.getStreamInfo('angrytestie')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement