2017-02-18 58 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

相關問題