How can I like a Video using the YouTube Data API v3 - java

I am using the YouTube Data API V3 in Java and I am trying to "like" a video. I am using the following method:
private static String insertPlaylistItem(String playlistId, String videoId) throws IOException {
// Define a resourceId that identifies the video being added to the
// playlist.
ResourceId resourceId = new ResourceId();
resourceId.setKind("youtube#video");
resourceId.setVideoId(videoId);
// Set fields included in the playlistItem resource's "snippet" part.
PlaylistItemSnippet playlistItemSnippet = new PlaylistItemSnippet();
playlistItemSnippet.setTitle("First video in the test playlist");
playlistItemSnippet.setPlaylistId(playlistId);
playlistItemSnippet.setResourceId(resourceId);
// Create the playlistItem resource and set its snippet to the
// object created above.
PlaylistItem playlistItem = new PlaylistItem();
playlistItem.setSnippet(playlistItemSnippet);
// Call the API to add the playlist item to the specified playlist.
// In the API call, the first argument identifies the resource parts
// that the API response should contain, and the second argument is
// the playlist item being inserted.
YouTube.PlaylistItems.Insert playlistItemsInsertCommand =
youtube.playlistItems().insert("snippet,contentDetails", playlistItem);
PlaylistItem returnedPlaylistItem = playlistItemsInsertCommand.execute();
System.out.println("New PlaylistItem name: " + returnedPlaylistItem.getSnippet().getTitle());
System.out.println(" - Video id: " + returnedPlaylistItem.getSnippet().getResourceId().getVideoId());
System.out.println(" - Posted: " + returnedPlaylistItem.getSnippet().getPublishedAt());
System.out.println(" - Channel: " + returnedPlaylistItem.getSnippet().getChannelId());
return returnedPlaylistItem.getId();
}
The Method above came from the official YouTube Example located here:
https://developers.google.com/youtube/v3/docs/playlists/insert?hl=de#examples
I go the hint that I have to add the video to the "liked" playlist, which automatically adds a like to that video.
Here is how I get the Playlist for Likes
....
String likesPlaylistId = channelsList.get(0).getContentDetails().getRelatedPlaylists().getLikes();
insertPlaylistItem(likesPlaylistId, "pwi9TAKUMYI" );
If I like a video that I uploaded myself, it works. But If I try to like a video that another youtuber uploaded, the following error appears:
http://pokit.org/get/?d25a148b2a20d169488cf167d22ad7b0.jpg
I see the video as "liked" but the like counter isn't increasing. Noone else can see that like. Can anyone tell me what I am doing wrong? Is that a restriction? Or is it a prevention against Bots?

Have you tried actually getting the rating of a video? (https://developers.google.com/youtube/v3/docs/videos/getRating).
Get the rating of the video before and after liking the video and then you can verify if the rating count increased. There might be a graphical delay.
raw api call is :
GET https://www.googleapis.com/youtube/v3/videos/getRating?id=pwi9TAKUMYI&key={YOUR_API_KEY}

Related

Youtube Data API - PlayListItems: list returns duplicated entries and hide other uploaded videos

I'm using the youtube data api (v3-rev222-1.25.0) to list all videos from the 'uploads' playlist of an youtube channel. The result of this request is a list with duplicated entries (see below). Even worse, these entries also hide other uploaded videos. Bottom line is, the list doesn't show all videos uploaded for a channel.
The URL for the 'uploads' play list is: https://www.youtube.com/playlist?list=UU_G9FS-HSMW6Jm-R4mwf8ng
String nextPageToken = null;
do {
PlaylistItemListResponse response = getService().playlistItems()
.list("snippet,contentDetails")
.setPlaylistId("UU_G9FS-HSMW6Jm-R4mwf8ng")
.setMaxResults(25L)
.setPageToken(nextPageToken)
.execute();
nextPageToken = response.getNextPageToken();
for (PlaylistItem item: response.getItems())
System.out.println(item.getSnippet().getPosition() + "\t" +
item.getId() + "\t" +
item.getContentDetails().getVideoId() + "\t" +
item.getSnippet().getTitle());
} while(nextPageToken != null);
This code snipped outputs the following lines (excerpt). You can see, there are some duplicated entries with the same playlist id and with the same vidoe id but with a different position within the playlist 'uploads'. There are much more such duplicated entries.
...
1076 VVVfRzlGUy1IU01XNkptLVI0bXdmOG5nLl9TblRlQU84aHFN _SnTeAO8hqM
...
1126 VVVfRzlGUy1IU01XNkptLVI0bXdmOG5nLl9TblRlQU84aHFN _SnTeAO8hqM
...
1163 VVVfRzlGUy1IU01XNkptLVI0bXdmOG5nLl9TblRlQU84aHFN _SnTeAO8hqM
...
1017 VVVfRzlGUy1IU01XNkptLVI0bXdmOG5nLjBRRlozUmI3blgw 0QFZ3Rb7nX0
...
1065 VVVfRzlGUy1IU01XNkptLVI0bXdmOG5nLjBRRlozUmI3blgw 0QFZ3Rb7nX0
...
Can anyone determine what the problem is?

Display multiple elements in card-like disposition with a loop - Android

I'm fairly new with Android development so I'm just trying some concepts.
For this POC I took the first 20 values from this IMDB list => https://www.imdb.com/chart/toptv/
added them into a JSON which is inside the assets folder of my project.
I have a method in my main activity to access the content of the JSON file, it goes like this:
public void readMovies() {
String jsonFileString = Utils.getJsonFromAssets(getApplicationContext(), "movies.json");
Log.i("data", jsonFileString);
Gson gson = new Gson();
Type listUserType = new TypeToken<List<Movies>>() { }.getType();
List<Movies> movies = gson.fromJson(jsonFileString, listUserType);
for (int i = 0; i < movies.size(); i++) {
Log.i("data", "> Item " + i + "\n" + movies.get(i));
}
}
Using a textView I'm able to output all the titles (for example) in the layout, but I'd like to be able to display a view similar to the IMDB page, with an image to the left side, title on the centre and any other property on the right side.
How can I achieve a view like the shared URL?
You've to create a Listview with images and text. Attaching some resources.
Checkout the answers to this Stack overflow question
Check out this Youtube tutorial or this one.
These are the ones I found which provide a code walk-through. But now you know the keywords to search for, you can find resources on your own!

View all comments on a YouTube video

I am trying to get all the comments on a YouTube video using a Java program. I cannot get them though as it has the "Show More" instead of all the comments. I'm looking for a way to get all the comments or pages of comments that I can go through. I have a video id and things, just need the comments.
I have tried all_comments instead of watch in the URL but it doesn't show all comments still and redirects to watch again.
I have also looked at the YouTube api and can only find how to get comments with their id but I need to get all comments from a video id.
If anyone knows how to do this please tell me.
I have added a 50 rep bounty for whoever can give me a good answer to this.
You need to get comment threads list request for your video and then scroll forward using next page token from the last response:
private static int counter = 0;
private static YouTube youtube;
public static void main(String[] args) throws Exception {
// For Auth details consider:
// https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/Auth.java
// Also don't forget secrets https://github.com/youtube/api-samples/blob/master/java/src/main/resources/client_secrets.json
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
Credential credential = Auth.authorize(scopes, "commentthreads");
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).build();
String videoId = "video_id";
// Get video comments threads
CommentThreadListResponse commentsPage = prepareListRequest(videoId).execute();
while (true) {
handleCommentsThreads(commentsPage.getItems());
String nextPageToken = commentsPage.getNextPageToken();
if (nextPageToken == null)
break;
// Get next page of video comments threads
commentsPage = prepareListRequest(videoId).setPageToken(nextPageToken).execute();
}
System.out.println("Total: " + counter);
}
private static YouTube.CommentThreads.List prepareListRequest(String videoId) throws Exception {
return youtube.commentThreads()
.list("snippet,replies")
.setVideoId(videoId)
.setMaxResults(100L)
.setModerationStatus("published")
.setTextFormat("plainText");
}
private static void handleCommentsThreads(List<CommentThread> commentThreads) {
for (CommentThread commentThread : commentThreads) {
List<Comment> comments = Lists.newArrayList();
comments.add(commentThread.getSnippet().getTopLevelComment());
CommentThreadReplies replies = commentThread.getReplies();
if (replies != null)
comments.addAll(replies.getComments());
System.out.println("Found " + comments.size() + " comments.");
// Do your comments logic here
counter += comments.size();
}
}
Consider api-samples, if you need a sample skeleton project.
Update
The situation when you can't get all the comments can be also caused by the quota limits (at least I faced it):
units/day 50,000,000
units/100seconds/user 300,000
This is not a java, python, js, or whatever language specific rules. If you want to get above the quota, you cant try to apply for higher quota. Though, I would start from controlling your throughput. It's very easy to get above the 100seconds/user quota.
try this it can download all the comments for a given video which i have tested.
https://github.com/egbertbouman/youtube-comment-downloader
python downloader.py --youtubeid YcZkCnPs45s --output OUT
Downloading Youtube comments for video: YcZkCnPs45s
Downloaded 1170 comment(s)
Done!
output is in the JSON format:
{
"text": "+Tony Northrup many thanks for the prompt reply - I'll try that.",
"time": "1 day ago",
"cid": "z13nfbog0ovqyntk322txzjamuensvpch.1455717946638546"
}

Obtain a share UpdateKey from LinkedIn using LinkedIn J and getNetworkUpdates() with Coldfusion

Using the "Network Updates API" example at the following link I am able to post network updates with no problem using client.postNetworkUpdate(updateText).
http://code.google.com/p/linkedin-j/wiki/GettingStarted
So posting works great.. However posting an update does not return an "UpdateKey" which is used to retrieve stats for post itself such as comments, likes, etc. Without the UpdateKey I cannot retrieve stats. So what I would like to do is post, then retrieve the last post using the getNetworkUpdates() function, and in that retrieval will be the UpdateKey that I need to use later to retrieve stats. Here's a sample script in Java on how to get network updates, but I need to do this in Coldfusion instead of Java.
Network network = client.getNetworkUpdates(EnumSet.of(NetworkUpdateType.STATUS_UPDATE));
System.out.println("Total updates fetched:" + network.getUpdates().getTotal());
for (Update update : network.getUpdates().getUpdateList()) {
System.out.println("-------------------------------");
System.out.println(update.getUpdateKey() + ":" + update.getUpdateContent().getPerson().getFirstName() + " " + update.getUpdateContent().getPerson().getLastName() + "->" + update.getUpdateContent().getPerson().getCurrentStatus());
if (update.getUpdateComments() != null) {
System.out.println("Total comments fetched:" + update.getUpdateComments().getTotal());
for (UpdateComment comment : update.getUpdateComments().getUpdateCommentList()) {
System.out.println(comment.getPerson().getFirstName() + " " + comment.getPerson().getLastName() + "->" + comment.getComment());
}
}
}
Anyone have any thoughts on how to accomplish this using Coldfusion?
Thanks
I have not used that api, but I am guessing you could use the first two lines to grab the number of updates. Then use the overloaded client.getNetworkUpdates(start, end) method to retrieve the last update and obtain its key.
Totally untested, but something along these lines:
<cfscript>
...
// not sure about accessing the STATUS_UPDATE enum. One of these should work:
// method 1
STATUS_UPDATE = createObject("java", "com.google.code.linkedinapi.client.enumeration.NetworkUpdateType$STATUS_UPDATE");
// method 2
NetworkUpdateType = createObject("java", "com.google.code.linkedinapi.client.enumeration.NetworkUpdateType");
STATUS_UPDATE = NetworkUpdateType.valueOf("STATUS_UPDATE");
enumSet = createObject("java", "java.util.EnumSet");
network = yourClientObject.getNetworkUpdates(enumSet.of(STATUS_UPDATE));
numOfUpdates = network.getUpdates().getTotal();
// Add error handling in case numOfUpdates = 0
result = yourClientObject.getNetworkUpdates(numOfUpdates, numOfUpdates);
lastUpdate = result.getUpdates().getUpdateList().get(0);
key = lastUpdate.getUpdateKey();
</cfscript>
You can also use socialauth library to retrieve updates and post status on linkedin.
http://code.google.com/p/socialauth

ROME API to parse RSS/Atom

I'm trying to parse RSS/Atom feeds with the ROME library. I am new to Java, so I am not in tune with many of its intricacies.
Does ROME automatically use its modules to handle different feeds as it comes across them, or do I have to ask it to use them? If so, any direction on this.
How do I get to the correct 'source'? I was trying to use item.getSource(), but it is giving me fits. I guess I am using the wrong interface. Some direction would be much appreciated.
Here is the meat of what I have for collection my data.
I noted two areas where I am having problems, both revolving around getting Source Information of the feed. And by source, I want CNN, or FoxNews, or whomever, not the Author.
Judging from my reading, .getSource() is the correct method.
List<String> feedList = theFeeds.getFeeds();
List<FeedData> feedOutput = new ArrayList<FeedData>();
for (String sites : feedList ) {
URL feedUrl = new URL(sites);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
List<SyndEntry> entries = feed.getEntries();
for (SyndEntry item : entries){
String title = item.getTitle();
String link = item.getUri();
Date date = item.getPublishedDate();
Problem here --> ** SyndEntry source = item.getSource();
String description;
if (item.getDescription()== null){
description = "";
} else {
description = item.getDescription().getValue();
}
String cleanDescription = description.replaceAll("\\<.*?>","").replaceAll("\\s+", " ");
FeedData feedData = new FeedData();
feedData.setTitle(title);
feedData.setLink(link);
And Here --> ** feedData.setSource(link);
feedData.setDate(date);
feedData.setDescription(cleanDescription);
String preview =createPreview(cleanDescription);
feedData.setPreview(preview);
feedOutput.add(feedData);
// lets print out my pieces.
System.out.println("Title: " + title);
System.out.println("Date: " + date);
System.out.println("Text: " + cleanDescription);
System.out.println("Preview: " + preview);
System.out.println("*****");
}
}
getSource() is definitely wrong - it returns back SyndFeed to which entry in question belongs. Perhaps what you want is getContributors()?
As far as modules go, they should be selected automatically. You can even write your own and plug it in as described here
What about trying regex the source from the URL without using the API?
That was my first thought, anyway I checked against the RSS standardized format itself to get an idea if this option is actually available at this level, and then try to trace its implementation upwards...
In RSS 2.0, I have found the source element, however it appears that it doesn't exist in previous versions of the spec- not good news for us!
[ is an optional sub-element of 1
Its value is the name of the RSS channel that the item came from, derived from its . It has one required attribute, url, which links to the XMLization of the source.

Categories

Resources