使用 youtube API v3 时顺序不起作用



我正在尝试获取频道的最后10个视频。当我运行以下代码时:

from apiclient.discovery import build
API_SERVICE_NAME = "youtube"
API_VERSION = "v3"
def youtubeTest():
    KEY = "my key here"
    service = build(API_SERVICE_NAME, API_VERSION , developerKey=KEY)           
    args = {}
    args['part']='snippet'
    args['maxResults']='10'
    args['channelId']='UCq-Fj5jknLsUf-MWSy4_brA'
    args['order']='date'
    args['type']='video'
    results = service.search().list(**args).execute()
    items = results['items']
    for item in items:
        print item['snippet']['publishedAt']

youtubeTest()

这是我得到的结果

2018-03-13T10:33:45.000Z
2018-03-07T10:19:59.000Z
2017-11-22T04:30:00.000Z
2012-05-06T07:47:37.000Z 
2014-10-08T13:26:35.000Z
2017-08-10T13:39:17.000Z
2018-07-28T08:45:00.000Z
2018-12-26T05:53:46.000Z
2014-07-11T13:36:08.000Z
2018-07-12T05:30:09.000Z

我想要最后十个视频,但这不是正确的顺序。这件代码工作了几个月,但我最近遇到了问题。

问题#128673552,https://issuetracker.google.com/issues/128673552。

您可以使用解决方法。而是使用search.list请求,检索上传的视频。

使用channel_id更改此字母,如下所示:

channel_id:u c q-fj5jknlsuf-mwsy4_bra

upload_playlist_id:u u q-fj5jknlsuf-mwsy4_bra

使用PlaylistItems.list请求从给定的频道检索上传的视频。

这是URL请求:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=<UPLOAD_PLAYLIST_ID>&fields=items(contentDetails(videoId%2CvideoPublishedAt)%2Csnippet(position%2Ctitle)%2Cstatus)&key={YOUR_API_KEY}

这些是结果:

{
 "items": [
  {
   "snippet": {
    "title": "Notebook | Main Taare | Teaser | Salman Khan | Pranutan Bahl | Zaheer Iqbal | Vishal Mishra",
    "position": 0
   },
   "contentDetails": {
    "videoId": "_wXRw1BMifw",
    "videoPublishedAt": "2019-03-16T04:51:10.000Z"
   }
  },
  {
   "snippet": {
    "title": "Finito Full Song | AMAVAS | Sachiin J Joshi, Vivan, Navneet | Jubin Nautiyal, Sukriti Kakar, Ikka",
    "position": 1
   },
   "contentDetails": {
    "videoId": "dSzjNuV4R3g",
    "videoPublishedAt": "2019-03-15T19:30:00.000Z"
   }
  },
  {
   "snippet": {
    "title": "T-SERIES MIXTAPE SEASON 2 Trailer l Bhushan Kumar | Abhijit Vaghani | Ahmed Khan",
    "position": 2
   },
   "contentDetails": {
    "videoId": "5itcXsszOiA",
    "videoPublishedAt": "2019-03-15T09:27:59.000Z"
   }
  },
  {
   "snippet": {
    "title": "Bheege Bheege Full Video | AMAVAS | Sachiin J Joshi & Nargis Fakhri |  Ankit Tiwari",
    "position": 3
   },
   "contentDetails": {
    "videoId": "3pY845c95AE",
    "videoPublishedAt": "2019-03-16T11:00:01.000Z"
   }
  },
  {
   "snippet": {
    "title": "Jab Se Mera Dil Full Video | AMAVAS |Sachiin J Joshi & Nargis Fakhri |Armaan Malik,Palak Muchhal",
    "position": 4
   },
   "contentDetails": {
    "videoId": "XNPbW9BmBSo",
    "videoPublishedAt": "2019-03-15T14:00:05.000Z"
   }
  }
 ]
}

这是您可以用来指导自己的Google API Explorer演示。

相关内容

  • 没有找到相关文章

最新更新