Order doesn't work when using youtube API v3

2020-04-19 05:44发布

I am trying to get the last 10 videos of a channel. When I run the following code:

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()

This is the result I am getting

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

I want the last ten videos but this is not the correct order. This piece of code was working for last few months but I am having the problem recently.

2条回答
太酷不给撩
2楼-- · 2020-04-19 05:45

You can use a workaround. Instead using the search.list request, retrieve the uploaded videos.

By using the channel_id change the letter as is explained here:

channel_id: UCq-Fj5jknLsUf-MWSy4_brA

upload_playlist_id: UUq-Fj5jknLsUf-MWSy4_brA

Use the PlaylistItems.list request for retrieve the uploaded videos from a given channel.

This is the URL request:

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}

And those are the results:

{
 "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"
   }
  }
 ]
}

This is the Google API Explorer demo you can use for guide yourself.

查看更多
该账号已被封号
3楼-- · 2020-04-19 05:58
登录 后发表回答