0

I'm making an Android application and connecting YouTube API there and I need to get sort videos that I get. For example: only long, normal videos and only short videos. I'm new to YouTube API, and I don't know how to do it.

Can anyone help?

There is small part of my code:

RetroInstance file code:

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetroInstance {

    fun retroInstance(): YoutubeService {
        return Retrofit.Builder()
            .baseUrl("https://www.googleapis.com/youtube/v3/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(YoutubeService::class.java)
    }
}

And YoutubeService code:

import com.example.alaproapp.model.YoutubeModel
import retrofit2.*
import retrofit2.http.GET
import retrofit2.http.Query

interface YoutubeService {

    @GET("search")
    fun getAllData(
        @Query("key") key: String = "key",
        @Query("channelId") channelId: String = "UCHVY_-jY-FayjszyX1nlGtQ",
        @Query("order") order: String = "date",
        @Query("maxResults") maxResult: String = "1800",
        @Query("part") part: String = "snippet,id"
    ): Call<YoutubeModel>
}

I tried to use documentation, but how I mentioned earlier I'm new to this, and I don't understand how to do it.

  • you don't the API predates shorts and has not been updated – Linda Lawton - DaImTo Aug 09 '23 at 08:57
  • Does [this answer](https://stackoverflow.com/a/74758155) solve your problem? – Benjamin Loison Aug 09 '23 at 11:33
  • @BenjaminLoison I already read this answer before to ask this question, i want to use another api's, when i tried know to use your's api it give's me error to many request, while i make only one – DiyorbekJDK Aug 09 '23 at 13:49
  • The error message mentions that *[hosting your own instance of the API](https://github.com/Benjamin-Loison/YouTube-operational-API/blob/0f0f970ccc2bfc4728049f2af42c320473b89b76/README.md#install-your-own-instance-of-the-api) solves the problem*. – Benjamin Loison Aug 09 '23 at 13:53
  • @BenjaminLoison I don't have any website, and want to use terminal to do something. Is there any easier way to do that? – DiyorbekJDK Aug 09 '23 at 15:36
  • You can host a local website from your terminal. I'm not aware of any easier alternative. – Benjamin Loison Aug 09 '23 at 15:40

1 Answers1

0

There is another one param you should add to the /search request to filter videos by short, medium or long.

@Query("videoDuration") part: String = ""  // Value: long, medium, short, any

long - 20+ min

medium - 4-20 min

short - <= 4 min

https://youtube.googleapis.com/youtube/v3/search?
part=snippet,id
&channelId=UCHVY_-jY-FayjszyX1nlGtQ
&maxResults=1800
&order=date
&type=video
&videoDuration=long
&key=[YOUR_API_KEY]

Documentation https://developers.google.com/youtube/v3/docs/search/list

Additionally, you could check the video duration. Request

https://youtube.googleapis.com/youtube/v3/videos?
part=contentDetails
&id={video1 id here}
&id={video2 id here}
&id={videoN id here}
&key=[YOUR_API_KEY]

In the response, find duration: PT59S = 59 sec

   "contentDetails": {
        "duration": "PT59S",
        "dimension": "2d",
        "definition": "hd",
        "caption": "false",
        "licensedContent": true,
        "contentRating": {},
        "projection": "rectangular"
      }

Documentation https://developers.google.com/youtube/v3/docs/videos/list

Dmitri Chernysh
  • 260
  • 1
  • 7