Android Studio and Youtube API Subscriber count? - android

I'm trying to insert into my app the number of subscribers and views from a channel on YouTube. How do I do that?
I've already setup the YouTube API and gotten my API Key.
I also have the URL that gives me the code below.
How do I turn this:
{
"kind": "youtube#channelListResponse",
"etag": "REMOVED",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "REMOVED",
"id": "REMOVED",
"statistics": {
"viewCount": "13398211",
"commentCount": "28",
"subscriberCount": "182758",
"hiddenSubscriberCount": false,
"videoCount": "84"
}
}
]
}

this is the request you need:
YouTube youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), null).setApplicationName("yourAppName").build();
YouTube.Channels.List channelListRequest = youTube.channels().list("statistics");
channelListRequest.setKey(DEVELOPER_KEY);
channelListRequest.setId(channelID);
channelListRequest.setFields("items/statistics(viewCount, subscriberCount)");
ChannelListResponse channelListResponse = channelListRequest.execute();
Channel channel = channelListResponse.getItems().get(0);
at this point you have the Channel object which contains the data you need. You can easly get the data from this object.
BigInteger viewCount = channel .getStatistics().getViewCount();
BigInteger subscriberCount = channel.getStatistics().getSubscriberCount();

Related

how to get youtube videos information in android? -kotlin

is there any chance to get video information with or without youtube APIs? right now I'm getting the thumbnail of videos using their video_id but I want information like title, description, views, YouTuber profile picture, and YouTuber name. I looked at youtube docs but just simple things like using players for playing videos.
this is the way I get thumbnails.
"https://img.youtube.com/vi/${videoID}/0.jpg"
also, I'm using youtube API for playing videos in my app with this code.
val intent = YouTubeStandalonePlayer.createVideoIntent(requireActivity(), "AIzaSyChnBzNGITQM2gUc52RnRfG3eDYmYRAhQ8", video_id)
startActivity(intent)
You can use these API to get video data
https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID&key=YOUR_API_KEY&part=snippet,statistics&fields=items(id,snippet,statistics)
Note: just add your API key in place of YOUR_API_KEY and pass videoId in place of VIDEO_ID.
API Response
{
"videos": [
{
"id": "7lCDEYXw3mM",
"snippet": {
"publishedAt": "2012-06-20T22:45:24.000Z",
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"title": "Google I/O 101: Q&A On Using Google APIs",
"description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg"
}
},
"categoryId": "28"
},
"statistics": {
"viewCount": "3057",
"likeCount": "25",
"dislikeCount": "0",
"favoriteCount": "17",
"commentCount": "12"
}
}
]
}

How to get specific data from API request without creating data classes including other data classes?

for example:
{
"error": false,
"message": "",
"code": "",
"ver": "v1.0.0",
"base": "https://,,,,,,,,,.com/",
"count": 1,
"updated": "2020-10-28T19:40:51+03:00",
"data": [
{
"id": "44",
"slug": "human-resources",
"name": "Human resources",
"img1": null,
"img2": "page_44/1560347480_2.jpg",
"date": "2019-05-31",
"des": "'vvvvvv.v",
"sendMail": "info#....",
"iframe_url": "ff/ff"
}
]
}
this is api json response.
I want only "data": [] value, not the rest.
Is there a way I can avoid creating data class for whole api response instead just request "data": [..] value?
I am using Android Studio Kotlin Retrofit Moshi
If I understand your question correctly, you just want to extract data from that JSON string. You can do that with:
JSONArray dataArray;
try {
dataArray = new JSONObject(yourString).optJSONArray("data");
} catch (JSONException e) {
// whatever
}
for (int i = 0; i < dataArray.length(); ++i) {
// do whatever you want with each object:
String name = dataArray.optJSONObject(i).optString("name");
}

Sorting JSON data that comes from Retrofit2 on API Level 15

i am new in retrofit . i was using Comparator for sorting my json but i find out that this code Added from api 24 . How can i sort my data for api 15 .
I dont want to sort data before getting it in mysql and i want to do it by android coding .
this is my sample data
[
{
"id": "1",
"name": "name1",
"lastname": "lastname1",
"rank": "1",
},
{
"id": "2",
"name": "name2",
"lastname": "lastname2",
"rank": "10",
},
{
"id": "20",
"name": "name20",
"lastname": "lastname20",
"rank": "105",
}
}
and some other question :
how do i delete coming data from retrofit that have one parameter null?
how to get the object that has rank 105?
Get your response in any model like
Like
List data;
where Response is
public class Response {
public int id;
public String name;
boolean isValid() {
return id > 0 && !TextUtils.isEmpty(name);
}
}
and filter invalid response like
ListIterator iterator = data.listIterator();
while (iterator.hasNext()) {
Response response = (Response) iterator.next();
boolean success = response.isValid();
if (!success) {
iterator.remove();
}
}

Making Post Requests to NodeJS Server Using Postman

I am building a movies API with a node-js back-end. I have used postman to create the data to be fetched at the API. The first object works fine, I am able to GET and POST only one object like this:
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
}
When I try to create more than one object in my POST request, the data returned at the end point is garbage, like this:
[
{
_id: "59b4c78a1157f62f88defdee",
__v: 0,
isReleased: true
}
]
The data I am trying to send to the end point resembles this format, json array of objects:
[
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
},
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
},
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
}
]
I am using postman to create post and mongodb to save data. My code looks like this:
var moviesModel = require('./../models/movieModel');
/**
Get Request For Retrieving All Movies from MongoDB
*/
var getMovies = function(req, res){
moviesModel.find(function(err, movies_data){
if (err) {
console.error(`Collections Empty`);
res.status(500);
res.end('A Server Error Has Occurred');
}
res.status(200);
res.send(movies_data);
});
}
/**
Post Request For Adding Movie to MongoDB
*/
var addMovie = function(req, res){
var m = new moviesModel(req.body);
m.save(function(err){
if (err) {
res.status(500);
res.end(`Save Operation Failed`);
} else {
res.status(201); //movie created
res.send(m);
}
});
}
My schema is defined in movieModel.js file. Could someone share with me how to construct this kind of post request with postman or share a link?
I know this question has no programming relevance but it could help me with building APIs to call from my website and android app. Thanks.

YouTube Api for channel list

I am newer in youtube api,I need help for implementing youtube api in android. I have find one youtube api for getting all channel list on associated username https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={USERNAME}&key={YOUR_API_KEY}. I have created 3 channels in my youtube account but above api is not getting response.
This is response for above api with valid username and api key
{
"kind": "youtube#channelListResponse",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/ewwRz0VbTYpp2EGbOkvZ5M_1mbo\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": []
}
Use it like this:
https://www.googleapis.com/youtube/v3/channels?part=statistics&id=channel_id&key=your_key
You can try your API request here: https://developers.google.com/youtube/v3/docs/channels/list#try-it
Request:
HTTP GET: GET https://www.googleapis.com/youtube/v3/channels?part=statistics&id={CHANNEL_ID}&key={YOUR_API_KEY}
Response (with id=UCt7iVnJwjBsof8IPLJHCTgQ):
{
"kind": "youtube#channelListResponse",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/WNxXCvycTyqTjTn9sLJ5toVjBRY\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/jijTuA_iWn2Kv9aRnqeAWNAcQ6I\"",
"id": "UCt7iVnJwjBsof8IPLJHCTgQ",
"statistics": {
"viewCount": "796662",
"commentCount": "20",
"subscriberCount": "257",
"hiddenSubscriberCount": false,
"videoCount": "126"
}
}
]
}
You can pass in a comma-separated list of Channel IDs for the id parameter. Because I only passed in one id, the first object of the items array will have the values you need. Get the object for the subscriberCount and videoCount values in the statistics dictionary for the data you want.

Categories

Resources