2017-02-18 59 views
1

我使用Youtube数据API版本3.使用提供的Java代码在特定频道内搜索所有视频。Youtube数据api v3按特定频道搜索所有上传和发布的视频

在youtube.com我可以看到两个哪类视频频道的视频标签里面

  • 发布的视频(通过其他渠道上传)

  • 上传的视频(通过此通道上传)

当通过api搜索时,通过设置特定的channelId,api只返回通过该频道上传的视频。 还有什么方法可以发布视频吗?

public static void main(String[] args) { 
     // Read the developer key from the properties file. 
     Properties properties = new Properties(); 
     try { 
      InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME); 
      properties.load(in); 

     } catch (IOException e) { 
      System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause() 
        + " : " + e.getMessage()); 
      System.exit(1); 
     } 

     try { 
      // This object is used to make YouTube Data API requests. The last 
      // argument is required, but since we don't need anything 
      // initialized when the HttpRequest is initialized, we override 
      // the interface and provide a no-op function. 
      youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() { 
       public void initialize(HttpRequest request) throws IOException { 
       } 
      }).setApplicationName("youtube-cmdline-search-sample").build(); 

      // Prompt the user to enter a query term. 
      String queryTerm = getInputQuery(); 

      // Define the API request for retrieving search results. 
      YouTube.Search.List search = youtube.search().list("id,snippet"); 

      // Set your developer key from the {{ Google Cloud Console }} for 
      // non-authenticated requests. See: 
      // {{ https://cloud.google.com/console }} 
      String apiKey = properties.getProperty("youtube.apikey"); 
      search.setKey(apiKey); 
      search.setQ(queryTerm); 
      search.setChannelId("UCEgdi0XIXXZ-qJOFPf4JSKw"); 

      // Restrict the search results to only include videos. See: 
      // https://developers.google.com/youtube/v3/docs/search/list#type 
      search.setType("video"); 

      // To increase efficiency, only retrieve the fields that the 
      // application uses. 
      search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)"); 
      search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED); 

      // Call the API and print results. 
      SearchListResponse searchResponse = search.execute(); 
      List<SearchResult> searchResultList = searchResponse.getItems(); 
      if (searchResultList != null) { 
       prettyPrint(searchResultList.iterator(), queryTerm); 
      } 
     } catch (GoogleJsonResponseException e) { 
      System.err.println("There was a service error: " + e.getDetails().getCode() + " : " 
        + e.getDetails().getMessage()); 
     } catch (IOException e) { 
      System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage()); 
     } catch (Throwable t) { 
      t.printStackTrace(); 
     } 
    } 

回答

2

通过使用指定channelId的Search: list,您将获得248个结果。这意味着这些结果是用户上传的视频。但是,这并不意味着他拥有它。

为了更好的解释,我使用这个参数。

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&channelId=UCEgdi0XIXXZ-qJOFPf4JSKw&_h=1&

我用的是channelId,你在你的问题中指定。我们会得到这样的第一个结果。

"snippet": { 
    "publishedAt": "2015-12-03T17:14:46.000Z", 
    "channelId": "UCEgdi0XIXXZ-qJOFPf4JSKw", 
    "title": "Kobe's Farewell Tour", 
    "description": "Kobe Bryant announced that this season, his 20th, will be his last, and is saying goodbye to fans around the league.", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/FR0AqkteAYw/default.jpg", 
     "width": 120, 
     "height": 90 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/FR0AqkteAYw/mqdefault.jpg", 
     "width": 320, 
     "height": 180 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/FR0AqkteAYw/hqdefault.jpg", 
     "width": 480, 
     "height": 360 
    } 
    }, 
    "channelTitle": "Sports", 
    "liveBroadcastContent": "none" 
    } 
    }, 

你会发现,第一个结果的标题是“科比的告别之旅”,通过获取这个视频,我发现这是一个播放列表,如果你检查它的内容是组成不同的视频由不同的用户上传。

https://www.youtube.com/watch?v=FR0AqkteAYw&list=PL8fVUTBmJhHLB3FW_53W1P0mtmwRTCEK_

enter image description here enter image description here

因此,这些都是您在渠道ID = UCEgdi0XIXXZ-qJOFPf4JSKw看到的影片。因此,要获取所有视频,请在您搜索到的所有播放列表中使用PlaylistItems: list

希望它可以帮助你。

+0

谢谢,这在我的情况下不起作用,因为我将'type'参数值设置为'video',您可能在我的代码中错过了它。我只搜索视频,而不是播放列表,但您的解决方案在某种意义上起作用。 – mhshimul

相关问题