Advertisement
Guest User

Kodi Youtube-Channels for APIv3

a guest
May 10th, 2015
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.39 KB | None | 0 0
  1. diff -Naur ytcold/addon.xml ytcnew/addon.xml
  2. --- ytcold/addon.xml    2013-03-20 19:09:52.000000000 +0000
  3. +++ ytcnew/addon.xml    2015-04-26 16:04:34.000000000 +0000
  4. @@ -2,6 +2,7 @@
  5.  <addon id="plugin.video.youtube.channels" name="YouTube Channels" version="2.0.2" provider-name="AddonScriptorDE">
  6.      <requires>
  7.          <import addon="xbmc.python" version="2.1.0"/>
  8. +        <import addon="script.module.simplejson" version="2.0.10"/>
  9.      </requires>
  10.      <extension point="xbmc.python.pluginsource" library="default.py">
  11.          <provides>video</provides>
  12. diff -Naur ytcold/default.py ytcnew/default.py
  13. --- ytcold/default.py   2013-03-20 19:09:52.000000000 +0000
  14. +++ ytcnew/default.py   2015-04-27 16:40:22.000000000 +0000
  15. @@ -1,6 +1,10 @@
  16.  #!/usr/bin/python
  17.  # -*- coding: utf-8 -*-
  18.  import xbmcaddon,xbmcplugin,xbmcgui,sys,urllib,urllib2,re,socket,os
  19. +try:
  20. +  import json
  21. +except:
  22. +  import simplejson as json
  23.  
  24.  socket.setdefaulttimeout(30)
  25.  pluginhandle = int(sys.argv[1])
  26. @@ -147,45 +151,44 @@
  27.                fh.close()
  28.  
  29.  def listVideos(params):
  30. +        apikey="AIzaSyAd-YEOqZz9nXVzGtn3KWzYLbLaajhqIDA"
  31.          spl=params.split("#")
  32.          user=spl[0]
  33.          index=spl[1]
  34.          orderby=spl[2]
  35.          updateThumb(user)
  36. -        content = getUrl("http://gdata.youtube.com/feeds/api/videos?author="+user+"&racy=include&max-results=50&start-index="+index+"&orderby="+orderby+"&v=2")
  37. -        match=re.compile("<openSearch:totalResults>(.+?)</openSearch:totalResults><openSearch:startIndex>(.+?)</openSearch:startIndex>", re.DOTALL).findall(content)
  38. -        maxIndex=int(match[0][0])
  39. -        startIndex=int(match[0][1])
  40. -        spl=content.split('<entry')
  41. -        for i in range(1,len(spl),1):
  42. -          entry=spl[i]
  43. -          match=re.compile('<yt:videoid>(.+?)</yt:videoid>', re.DOTALL).findall(entry)
  44. -          id=match[0]
  45. -          match=re.compile("viewCount='(.+?)'", re.DOTALL).findall(entry)
  46. -          viewCount="0"
  47. -          if len(match)>0:
  48. -            viewCount=match[0]
  49. -          match=re.compile("duration='(.+?)'", re.DOTALL).findall(entry)
  50. -          durationTemp=int(match[0])
  51. -          min=(durationTemp/60)+1
  52. -          sec=durationTemp%60
  53. -          duration=str(min)+":"+str(sec)
  54. -          match=re.compile("<author><name>(.+?)</name>", re.DOTALL).findall(entry)
  55. -          author=match[0]
  56. -          match=re.compile("<media:title type='plain'>(.+?)</media:title>", re.DOTALL).findall(entry)
  57. -          title=match[0]
  58. -          title=cleanTitle(title)
  59. -          match=re.compile("<media:description type='plain'>(.+?)</media:title>", re.DOTALL).findall(entry)
  60. -          desc=""
  61. -          if len(match)>0:
  62. -            desc=match[0]
  63. -            desc=cleanTitle(desc)
  64. -          match=re.compile("<published>(.+?)T", re.DOTALL).findall(entry)
  65. -          date=match[0]
  66. -          thumb="http://img.youtube.com/vi/"+id+"/0.jpg"
  67. +        newcontent = getUrl("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&"+
  68. +          "forUsername="+user+
  69. +          "&racy=include&maxResults=50&startIndex="+index+
  70. +          "&orderby="+orderby+
  71. +          "&key="+apikey)
  72. +        userdata = json.loads(newcontent)
  73. +        playlistid = userdata['items'][0]['contentDetails']['relatedPlaylists']['uploads']
  74. +        print "Playlist for the user's uploads is: "+playlistid
  75. +        params = "?part=id%2Csnippet%2CcontentDetails%2Cstatus"+"&playlistId="+playlistid+"&maxResults=50"+"&key="+apikey
  76. +        if index!="1":
  77. +          params=params+"&pageToken="+index
  78. +        newcontent = getUrl("https://www.googleapis.com/youtube/v3/playlistItems"+params)
  79. +        playlistdata = json.loads(newcontent)
  80. +        videos = playlistdata['items']
  81. +        for i in range(0,len(videos),1):
  82. +          #print "Titel: "+videos[i]['snippet']['title'].encode('utf8')
  83. +          id = videos[i]['snippet']['resourceId']['videoId']
  84. +          #print "Video-ID: "+id
  85. +          viewCount="0" #not available via this API-Call, and not interesting for me
  86. +          duration="0" #TODO: not available via this API-Call
  87. +          author=videos[i]['snippet']['channelTitle']
  88. +          title=videos[i]['snippet']['title']
  89. +          desc=videos[i]['snippet']['description']
  90. +          date=videos[i]['snippet']['publishedAt']
  91. +          thumb="https://i.ytimg.com/vi/"+id+"/0.jpg" #behaviour kept from the old api, now the URL is also available from the API in different resolutions
  92.            addLink(title,id,'playVideo',thumb,"Date: "+date+"; Views: "+viewCount+"\n"+desc,duration,author)
  93. -        if startIndex+50<=maxIndex:
  94. -          addDir(translation(30007),user+"#"+str(int(index)+50)+"#"+orderby,'listVideos',"")
  95. +        print 'nextPageToken' in playlistdata
  96. +        print hasattr(playlistdata, 'nextPageToken')
  97. +        try:
  98. +          addDir(translation(30007),user+"#"+playlistdata['nextPageToken']+"#"+orderby,'listVideos',"")
  99. +        except KeyError:
  100. +          print "Seems to be the last page"
  101.          xbmcplugin.endOfDirectory(pluginhandle)
  102.          if forceViewMode=="true":
  103.            xbmc.executebuiltin('Container.SetViewMode('+viewMode+')')
  104. @@ -398,6 +401,7 @@
  105.            xbmc.executebuiltin("Container.Refresh")
  106.  
  107.  def getUrl(url):
  108. +        print "Fetching URL: "+url
  109.          req = urllib2.Request(url)
  110.          req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0')
  111.          response = urllib2.urlopen(req)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement