Skip to content

Commit

Permalink
init 1
Browse files Browse the repository at this point in the history
Translation to 2.1 PLEX Framework of the __init__.py completed
  • Loading branch information
nixaraven committed Mar 6, 2013
1 parent facec2d commit dde0184
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions Twitch.tv.bundle/Contents/Code/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Twitch.tv Plugin
# v0.1 by Marshall Thornton <marshallthornton@gmail.com>
# Code inspired by Justin.tv plugin by Trevor Cortez and John Roberts
# v0.2 by Nicolas Aravena <mhobbit@gmail.com>
# Adaptation of v0.1 for new Plex Media Server.

####################################################################################################

Expand All @@ -22,27 +24,28 @@

####################################################################################################
def Start():
Plugin.AddPrefixHandler("/video/twitchtv", VideoMainMenu, NAME, ICON, ART)
Plugin.AddViewGroup("InfoList", viewMode="InfoList", mediaType="items")
Plugin.AddViewGroup("List", viewMode="List", mediaType="items")
MediaContainer.art = R(ART)
MediaContainer.title1 = NAME
MediaContainer.viewGroup = "InfoList"
DirectoryItem.thumb = R(ICON)
PrefsItem.thumb = R(ICON)
InputDirectoryItem.thumb = R(ICON)
Plugin.AddViewGroup("List", viewMode = "List", mediaType = "items")

ObjectContainer.art = R(ART)
ObjectContainer.title1 = NAME
ObjectContainer.view_group = "List"
DirectoryItem.thumb = R(ICON)

HTTP.Headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1'

def VideoMainMenu():
dir = MediaContainer(noCache=True)
dir.Append(Function(DirectoryItem(FeaturedStreamsMenu, title="Featured Streams", summary="Browse featured streams")))
dir.Append(Function(DirectoryItem(GamesMenu, title="Games", summary="Browse live streams by game")))
dir.Append(Function(InputDirectoryItem(SearchResults, title="Search", prompt="Search For A Stream", summary="Search for a stream")))
return dir

@handler "/video/twitch"
def MainMenu():
oc = ObjectContainer()
oc.add(DirectoryObject(key=Callback(FeaturedStreamsMenu), title="Featured Streams"))
oc.add(DirectoryObject(key=Callback(GamesMenu), title="Games", summary="Browse live streams by game"))
oc.add(InputDirectoryObject(key=Callback(SearchResults), title="Search", prompt="Search For A Stream", summary="Search for a stream"))

return oc

@route('/video/twitch/featured', page=int, cacheTime=int, allow_sync=True)
def FeaturedStreamsMenu(sender, page=None):
dir = MediaContainer(viewGroup="List", title2="Featured Streams")
oc = ObjectContainer(view_group="List", title2="Featured Streams")
url = "%s?limit=%s" % (TWITCH_FEATURED_STREAMS, PAGE_LIMIT)

featured = JSON.ObjectFromURL(url, cacheTime=CACHE_INTERVAL)
Expand All @@ -51,51 +54,51 @@ def FeaturedStreamsMenu(sender, page=None):
subtitle = "%s\n%s Viewers" % (stream['stream']['game'], stream['stream']['viewers'])
summary = strip_tags(stream['text'])
streamUrl = "%s&channel=%s" % (TWITCH_LIVE_PLAYER, stream['stream']['channel']['name'])
dir.Append(WebVideoItem(streamUrl, title=stream['stream']['channel']['display_name'], subtitle=subtitle, summary=summary, thumb=stream['stream']['preview']))

return dir
oc.add(VideoClipObject(url=streamUrl, title=stream['stream']['channel']['display_name'], thumb=stream['stream']['preview'], subtitle=subtitle))

return oc

@route('/video/twitch/games', page=int, cacheTime=int, allow_sync=True)
def GamesMenu(sender, page=0):
dir = MediaContainer(viewGroup="List", title2="Top Games")
oc = ObjectContainer(view_group="List", title2="Top Games")
url = "%s?limit=%s&offset=%s" % (TWITCH_TOP_GAMES, PAGE_LIMIT, page*PAGE_LIMIT)

games = JSON.ObjectFromURL(url, cacheTime=CACHE_INTERVAL)

for game in games['top']:
gameSummary = "%s Channels\n%s Viewers" % (game['channels'], game['viewers'])
dir.Append(Function(DirectoryItem(ChannelMenu, title=game['game']['name'], thumb=game['game']['logo']['large'], summary=gameSummary), game=game['game']['name']))
oc.add(DirectoryObject(key=Callback(ChannelMenu, game=game['game']['name'])), title=game['game']['name'], summary=gameSummary, thumb=game['game']['logo']['large'])

if(len(games['top']) == 100):
dir.Append(Function(DirectoryItem(GamesMenu, title="More Games"), page=(page+1)))
oc.add(DirectoryObject(key=Callback(GamesMenu, title = "More Games", page = (page+1))))

return dir
return oc


def ChannelMenu(sender, game=None):
dir = MediaContainer(title2=sender.itemTitle)
oc = ObjectContainer(title2=sender.itemTitle)
url = "%s?game=%s&limit=%s" % (TWITCH_LIST_STREAMS, urllib.quote_plus(game), PAGE_LIMIT)

streams = JSON.ObjectFromURL(url, cacheTime=CACHE_INTERVAL)

for stream in streams['streams']:
subtitle = " %s Viewers" % stream['viewers']
streamURL = "%s&channel=%s" % (TWITCH_LIVE_PLAYER, stream['channel']['name'])
dir.Append(WebVideoItem(streamURL, title=stream['channel']['display_name'], summary=stream['channel']['status'], subtitle=subtitle, thumb=stream['channel']['banner'], duration=0))
oc.add(VideoClipObject(url=streamUrl, title=stream['channel']['display_name'], summary=stream['channel']['status'],thumb=stream['channel']['logo'], subtitle=subtitle, duration=0)))

return dir
return oc


def SearchResults(sender, query=None):
dir = MediaContainer()
oc = ObjectContainer()

results = JSON.ObjectFromURL("%s?query=%s&limit=%s" % (TWITCH_SEARCH_STREAMS, urllib.quote_plus(query), PAGE_LIMIT), cacheTime=CACHE_INTERVAL)

for stream in results['streams']:
subtitle = "%s\n%s Viewers" % (stream['game'], stream['viewers'])
streamURL = "%s&channel=%s" % (TWITCH_LIVE_PLAYER, stream['channel']['name'])
dir.Append(WebVideoItem(streamURL, title=stream['channel']['display_name'], summary=stream['channel']['status'], subtitle=subtitle, thumb=stream['channel']['banner']))
oc.add(VideoClipObject(url=streamUrl, title=stream['channel']['display_name'], summary=stream['channel']['status'],thumb=stream['channel']['logo'], subtitle=subtitle, duration=0)))
if len(dir) > 0:
return dir
return oc
else:
return MessageContainer("Not found", "No streams were found that match your query.")
return ObjectContainer(header="Not found", message="No streams were found that match your query.")

0 comments on commit dde0184

Please sign in to comment.