Giter Site home page Giter Site logo

prof18 / youtubeparser Goto Github PK

View Code? Open in Web Editor NEW
90.0 14.0 30.0 10.78 MB

An Android library to get video's information from Youtube channels.

License: Apache License 2.0

Java 24.37% Kotlin 75.63%
video youtube-channel statistics android-library youtube-api youtube android

youtubeparser's Introduction

YoutubeParser

Maven Central License API

This is an Android library to get information of videos from Youtube channels. You can retrieve title, link and thumbnail of a video from a specific channel. You can also get the statistics of a video like view, like, dislike, favorite and comment count. Now it is also possible to load more videos by making a new request.

⚠️ Important Notice

The library artifacts have been moved to MavenCentral. The group id is changed from com.prof.youtubeparser to com.prof18.youtubeparser. Be sure to add the gradle dependency to your root build.gradle file.

allprojects {
    repositories {
        mavenCentral()
    }
}

How to

Import:

The library is uploaded on MavenCentral, so you can easily add the dependency:

dependencies {
  compile 'com.prof18.youtubeparser:youtubeparser:<latest-version>'
}

Usage:

Starting from the version 3.x, the library has been completely rewritten using Kotlin and the coroutines. However, The compatibility with Java has been maintained and the same code of the versions 2.x (and below) can be used.

If you are using Kotlin you need to launch the coroutine that retrieves the videos.

To load videos from a specific channel:

import com.prof.youtubeparser.Parser
import com.prof.youtubeparser.models.videos.Video

coroutineScope.launch(Dispatchers.Main) {
    
    val parser = Parser()
    
    //(CHANNEL_ID, NUMBER_OF_RESULT_TO_SHOW, ORDER_TYPE ,BROSWER_API_KEY)
    //https://www.youtube.com/channel/UCVHFbqXqoYvEWM1Ddxl0QDg --> channel id = UCVHFbqXqoYvEWM1Ddxl0QDg
    //The maximum number of result to show is 50
    //ORDER_TYPE --> by date: "Parser.ORDER_DATE" or by number of views: "ORDER_VIEW_COUNT"  
    val requestUrl = parser.generateRequest(
            channelID = channelID,
            maxResult = 20,
            orderType = Parser.ORDER_DATE,
            key = BuildConfig.KEY
    )
    try {
        val result = parser.getVideos(requestUrl)
        nextToken = result.nextToken
        videoList = result.videos
    } catch (e: Exception) {
        // handle the exception
    }
}

If you are still using Java, the code is very similar to the older versions of the library:

import com.prof.youtubeparser.Parser;
import com.prof.youtubeparser.models.videos.Video;

Parser mParser = new Parser();
mParser.onFinish(new Parser.OnTaskCompleted() {
    @Override
    public void onTaskCompleted(@NonNull List<Video> list, @NonNull String nextPageToken) {
        // The list contains all the videos. For example you can use it for your adapter.
        // The next page token can be used to retrieve more videos
    }

    @Override
    public void onError(@NonNull Exception e) {
        // handle the exception
    }
});

//(CHANNEL_ID, NUMBER_OF_RESULT_TO_SHOW, ORDER_TYPE ,BROSWER_API_KEY)
//https://www.youtube.com/channel/UCVHFbqXqoYvEWM1Ddxl0QDg --> channel id = UCVHFbqXqoYvEWM1Ddxl0QDg
//The maximum number of result to show is 50
//ORDER_TYPE --> by date: "Parser.ORDER_DATE" or by number of views: "ORDER_VIEW_COUNT"  
String requestUrl = mParser.generateRequest(
            CHANNEL_ID,
            20,
            Parser.ORDER_DATE,
            BuildConfig.KEY
    );

mParser.execute(requestUrl);
Version 2.2 and belows:
import com.prof.youtubeparser.Parser;
import com.prof.youtubeparser.models.videos.Video;

Parser parser = new Parser();

//(CHANNEL_ID, NUMBER_OF_RESULT_TO_SHOW, ORDER_TYPE ,BROSWER_API_KEY)
//https://www.youtube.com/channel/UCVHFbqXqoYvEWM1Ddxl0QDg --> channel id = UCVHFbqXqoYvEWM1Ddxl0QDg
//The maximum number of result to show is 50
//ORDER_TYPE --> by date: "Parser.ORDER_DATE" or by number of views: "ORDER_VIEW_COUNT"  
String url = parser.generateRequest(CHANNEL_ID, 20, Parser.ORDER_DATE, API_KEY);
parser.execute(url);
parser.onFinish(new Parser.OnTaskCompleted() {

    @Override
    public void onTaskCompleted(ArrayList<Video> list, String nextPageToken) {
      //what to do when the parsing is done
      //the ArrayList contains all video data. For example you can use it for your adapter
    }

    @Override
    public void onError() {
        //what to do in case of error
    }
});

To create a BROSWER API KEY you can follow this guide.

To load more videos from the same channel:

To load more videos, you can use the same method described above but with the following URL:

val requestUrl = parser.generateMoreDataRequest(
                    channelID = channelID,
                    maxResult = 20,
                    orderType = Parser.ORDER_DATE,
                    key = BuildConfig.KEY,
                    nextToken = pageToken
            )
String requestUrl = mParser.generateMoreDataRequest(
                CHANNEL_ID,
                20,
                Parser.ORDER_DATE,
                BuildConfig.KEY,
                mNextToken
        );

Remember that this request can be made only AFTER the a previous one, because you need the nextPageToken. Remember also that every request can get a maximum of 50 elements.

To get the statistics of a video:

If you are using Kotlin you need to launch the coroutine that retrieves the videos.

import com.prof.youtubeparser.VideoStats
import com.prof.youtubeparser.models.videos.Video

val videoStats = VideoStats()
val requestUrl = videoStats.generateStatsRequest(
        videoID = videoId,
        key = BuildConfig.KEY
)
coroutineScope.launch(Dispatchers.Main) {
    try {
        _stats.postValue(videoStats.getStats(requestUrl))
    } catch (e: Exception) {
        e.printStackTrace()
        _snackbar.value = "An error has occurred. Please retry"
    }
}

If you are still using Java, the code is very similar to the older versions of the library:

import com.prof.youtubeparser.VideoStats;
import com.prof.youtubeparser.models.stats.Statistics;

VideoStats videoStats = new VideoStats();
videoStats.onFinish(new VideoStats.OnTaskCompleted() {
    @Override
    public void onTaskCompleted(@NonNull Statistics stats) {
        //Here you can set the statistic to a Text View for instance
        
        //for example:
        String body = "Views: " + stats.getViewCount() + "\n" +
                    "Like: " + stats.getLikeCount() + "\n" +
                    "Dislike: " + stats.getDislikeCount() + "\n" +
                    "Number of comment: " + stats.getCommentCount() + "\n" +
                    "Number of favourite: " + stats.getFavoriteCount();
    }

    @Override
    public void onError(@NonNull Exception e) {
        // handle the exception
    }
});

String requestUrl = videoStats.generateStatsRequest(videoId, BuildConfig.KEY);
videoStats.execute(requestUrl);
Version 2.2 and belows:
import com.prof.youtubeparser.VideoStats;
import com.prof.youtubeparser.models.stats.Statistics;

VideoStats videoStats = new VideoStats();
String url = videoStats.generateStatsRequest(videoId, API_KEY);
videoStats.execute(url);
videoStats.onFinish(new VideoStats.OnTaskCompleted() {
  @Override
  public void onTaskCompleted(Statistics stats) {
      //Here you can set the statistic to a Text View for instance

      //for example:
      String body = "Views: " + stats.getViewCount() + "\n" +
                    "Like: " + stats.getLikeCount() + "\n" +
                    "Dislike: " + stats.getDislikeCount() + "\n" +
                    "Number of comment: " + stats.getCommentCount() + "\n" +
                    "Number of favourite: " + stats.getFavoriteCount();
  }

  @Override
  public void onError() {
      //what to do in case of error
  }
});

Sample app

I wrote a simple app that shows videos from Android Developer Youtube Channel.

The sample is written both in Kotlin and Java. You can browse the Kotlin code here and the Java code here

Please use the issues tracker only to report issues. If you have any kind of question you can ask them on the blog post

Changelog

From version 1.4.4 and above, the changelog is available in the release section.

  • 21 May 2019 - Rewrote library with Kotlin - Version 3.0.0
  • 14 December 2017 - Improved Error Management - Version 2.2
  • 12 August 2017 - Fixed Library Manifest - Version 2.1
  • 22 June 2017 - Big update: now you can load more video and get the statistic of a video - Version 2.0
  • 17 June 2016 - Fixed a bug on Locale - Version 1.1
  • 15 June 2016 - First release - Version 1.0

License

   Copyright 2016 Marco Gomiero

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Apps using RSS Parser

If you are using Youtube Parser in your app and would like to be listed here, please open a pull request!

List of Apps using Youtube Parser

youtubeparser's People

Contributors

ngodiseni avatar prof18 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

youtubeparser's Issues

İt is not working

Hello

I downloaded your apk.

But it is not working.
Can u check it.

Thanks.

Unparseable date

Good day

I have been getting an error that says an unparseable date that comes from the CoreJsonParser line 62. I attached an image of the stack trace of the error
Capture

Api key

Where do you replace the api key, because, I have gone through the code and can't find where to replace the youtube api key

Integration into my App

Hello, I am having trouble getting my app to work with the code you provided. Everything is correct in the Android Studio(no errors are showing) and it will compile and I am able to run my app in my emulator but as soon as I go to click the button I am using to initiate it to go to the youtube channel it does not work. Could you take a look at my code to see what could be wrong? I just don't even know where to start. I have my own API key. I am using the correct channel ID but it just does not seem to be working. The app continues to crash.
I also tried contacting you on your blog but for some reason I could not get the verification email to send to my email so that is why I am contacting you through this way.

Problem adding more than 50 items in list

Hi,

Thanks for publishing such a wonderful project. But there is a problem my channel has around 73 videos and we publish 5 videos every week. Please add an option to add more videos and also will be best if you also add an option to endless scroll the videos.

Thanks again for providing such a great help :)

Demo app doesn't work

Hi! I have tried to use the demo app but doesn't get any video. Maybe something need an update or I'm doing something wrong. Can u help me please?

v3.0.0 extend Parse class to get more video info

Hi. Migrating from v2.2 to v3.0.0 now is not able to extend Parse class, as it is not a AsyncTask anymore. There is any class, object or callback to override to be able to get the video items to get more info like channelID, channelTitle, description, etc? I used to override the onPostExecute of your Parser class to get these info and create an VideoExtended class.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.