PLUGIN_PREFIX = "/video/pakitvtv"
NAME = L('Title')

# make sure to replace artwork with what you want
# these filenames reference the example files in
# the Contents/Resources/ folder in the bundle
ART  = 'art-default.jpg'
ICON = 'icon.png'
####################################################################################################
ARYICON = 'arylogo.jpg'
SAMAICON = "SamaLogo.jpg"
DUNIYAICON = 'dunyalogo.png'
ARYDIGITALICON = 'ARYDIGITAL.jpg'
GEOICON='geologo.gif'
####################################################################################################

def Start():
    Plugin.AddPrefixHandler(PLUGIN_PREFIX, MainMenu, NAME, ICON, ART)
    Plugin.AddViewGroup("InfoList", viewMode="InfoList", mediaType="items")
    Plugin.AddViewGroup("List", viewMode="List", mediaType="items")
    MediaContainer.title1 = NAME
    MediaContainer.viewGroup = "Grid"
    MediaContainer.art = R(ART)
    DirectoryItem.thumb = R(ICON)
    VideoItem.thumb = R(ICON)
    PrefsItem.thumb = R(ICON)

    InputDirectoryItem.thumb = R(ICON)
    HTTP.CacheTime = CACHE_1MONTH
    # Set header for all HTTP requests
#    HTTP.Headers["User-agent"]  = USER_AGENT
#    HTTP.Headers["Referer"]     = REFERER


#### the rest of these are user created functions and
#### are not reserved by the plugin framework.
#### see: http://dev.plexapp.com/docs/Functions.html for
def MainMenu():
    dir = MediaContainer(viewGroup="InfoList")
	
    title = "Abb Takk"
    summary = "Abb Takk Live"
    provider="http://dstreamone.com:1935/veritas/smil:veritas1.smil/manifest.m3u8"
    image="http://dstreamone.com/veritas/jp/AbbTakk.jpg"
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))
			
    title = "ARY News"
    summary = "ARY News Live"
    provider="http://109.163.232.58:1935/live/myStream/playlist.m3u8"
    image=R(ARYICON)
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))
    
    
    title = "ARY Digital"
    summary = "ARY Digital Live"
    provider="http://93.115.85.17:1935/ARYDIGITAL/myStream/playlist.m3u8"
    image=R(ARYDIGITALICON)
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image, clip="myStream"))
    
    
    title = "SAMA News"
    summary = "SAMA News Live"
    provider="http://samaatr.videocdn.scaleengine.net/samaatr-iphone/play/samaatr_264k.stream/playlist.m3u8"
    image=R(SAMAICON)
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))
    
    
    
    title = "HUM TV"
    summary = "HUM TV Live"
    provider="rtmp://cdn.ebound.tv/tv?wmsAuthSign=/c2VydmVyX3RpbWU9MS8xNy8yMDE1IDEwOjM3OjEzIFBNJm"#humtv
    image="http://www.hum.tv/images/hum_logo.png"
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image,clip="Live"))
    
    
    title = "Duniya News"
    summary = "Duniya News Live"
    provider="http://imob.dunyanews.tv:1935/live/smil:stream.smil/playlist.m3u8"
    image=R(DUNIYAICON)
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))
    
    
    title = "Geo News"
    summary = "Geo News Live"
    provider="rtmp://198.199.107.119/live?dig=4a38ebb81706e5cde9aea26c"
    image=R(GEOICON)
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))
    
    
    title = "Channel92"
    summary = "Channel92 Live"
    provider="http://92hd.styleofglobal.com:1935/live/92news_360p/playlist.m3u8"
    image="http://92newshd.tv/wp-content/themes/channel-92c/images/logo-newp.png"
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image="http://92newshd.tv/live-tv/player.png"))
			
    title = "Jaag TV"
    summary = "JAAG TV Live"
    provider="http://38.96.148.99:1935/live2/jaagtv/playlist.m3u8"
    image="https://lh6.ggpht.com/2ILquUUWCnnwuCBglGpx2-yvz4sb75Xop1EbI3OQgU4lY3bIJKyjUUUu-IPtUX-BEHk=w300"
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))

    title = "PTV World"
    summary = "PTV World Live"
    provider="http://app.pakistanvision.tv:1935/live/8090/player.m3u8"
    image="http://dnews.pk/wp-content/uploads/2014/10/ptv-news-live.jpg"
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image=image))
			
    title = "PTV News"
    summary = "PTV News Live"
    provider="http://app.pakistanvision.tv:1935/live/PTVnews/player.m3u8"
    image="https://lh6.ggpht.com/2ILquUUWCnnwuCBglGpx2-yvz4sb75Xop1EbI3OQgU4lY3bIJKyjUUUu-IPtUX-BEHk=w300"
    dir.Append(
        Function(
            DirectoryItem(
                key=StreamMenu,
                title=title,
                subtitle="",
                summary=summary,
                thumb=image,
                art=image
            ),title=title,provider=provider, image="https://lh6.ggpht.com/2ILquUUWCnnwuCBglGpx2-yvz4sb75Xop1EbI3OQgU4lY3bIJKyjUUUu-IPtUX-BEHk=w300"))
	
    return dir
  
def StreamMenu(sender,title, provider,image,clip=""):
  dir = ObjectContainer(title2=title)
  vco = VideoClipObject(key= Callback(StreamMenu, sender=sender, title=title, provider=provider, image=image,clip=clip),
			rating_key = title
			,title=title,
			thumb=image,
			art=image,
			items=[CreateMediaObject(provider,clip)])
  dir.add(vco)
  return dir

def CreateMediaObject(provider,clip):
  mediakey=HTTPLiveStreamURL(provider)
  protocol = None
  container = None
  mediaobject = None
  
  if provider.startswith('rtmp'):
    protocol='rtmp'
    mediakey=RTMPVideoURL(provider, clip=clip, live=True)
    mediaobject= MediaObject(
			    parts = [
			      PartObject(key=mediakey)
			      ],
			    video_codec = VideoCodec.H264,
			    audio_codec = AudioCodec.AAC,
			    audio_channels = 2,
			    optimized_for_streaming = True
			    ,protocol=protocol
			    ,container = container
			  )
  else:  
    mediaobject = MediaObject(
			    parts = [
			      PartObject(key=mediakey)
			      ],
			    optimized_for_streaming = True
			    ,protocol=protocol
			    ,container = container
			  )
  
  return mediaobject


def ValidatePrefs():
  pass

#####################################################################################################
def LiveStream(provider,title,thumb=R(ICON), include_container=False):
  vco = VideoClipObject(
    key = Callback(LiveStream, provider=provider, title=title, thumb=thumb, include_container=True),
    rating_key = provider,
    title = title,
    thumb = thumb,
#	height = HEIGHT,
#    width = WIDTH,
    items = [
      MediaObject(
	parts = [
	  PartObject(key=HTTPLiveStreamURL(provider))
	  ]
	,optimized_for_streaming = True
	)
      ]
    )
  if include_container:
    return ObjectContainer(objects=[vco])
  else:
    return vco
