Get most popular videos of a channel by its name - php

I'm trying to list most popular (or newest) videos of a channel by its name using YouTube 3.0 API.
I already implemented this, just not sure if it is correct way to do this, so need an advice.
First of I find channel ID by name (e.g. TEDxTalks in place of {channel_name}):
https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&q={channel_name}&key={API_KEY}
Then I get videos of this channel ID, ordered by viewCount (or date), which perfectly lists videos:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channel_id}&order=viewCount&type=video&key={API_KEY}
How reliable is the first request? If name is the exact match of channel url slug or username (e.g. tedxtalks), will it always return corresponding channel? Or is there any more reliable way to get channel ID by its name?
Thank you.

Search API is not for exact match query. And it searching for all channel title(username found in URL if exist), channel name(xxx added 1 video) and even channel ID!
To answer your question, unfortunately it's not reliable and no other more reliable way.
A quick example, try to query username "abc".
https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=abc&type=channel&key={YOUR_API_KEY}
The Search API response will return ABCNetwork, ABCNews, udruzenjeabc... and so on. No channelTitle(username) "abc" in the first 50 items.
Even though not all channels have username. If you got username of specific channel, then you should use Channel API for exact match query,
https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=abc&maxResults=5&key={YOUR_API_KEY}
You might wonder why YouTube API doesn't provide exact search for "channel name"(xxx added 1 video)? It's because channel name is not unique, as a person name is not unique.
As indicated at https://developers.google.com/youtube/v3/guides/working_with_channel_ids:
If you are using v3 and want to retrieve the channel ID that
corresponds to the currently authorized user, you can call the
channels.list(part="id", mine=true) method. This is equivalent to
asking for the channel profile of the default user in v2.
If you ever do find yourself with an arbitrary legacy YouTube username
that you need to translate into a channel ID using v3 of the API, you
can make a channels.list(part="id", forUsername="username") call to
the API.
If you only know a display name and are looking to find the
corresponding channel, the search.list(part="snippet", type="channel",
q="display name") method will come in handy. You should be prepared to
deal with the possibility of the call returning more than one item in
the response, since display names are not unique.
Finally, i have no idea why YouTube didn't force to generate a unique readable username (like how Facebook add a random number at the end of duplicated username, e.g. zuck.5 and zuck.21 ) for all new user.
References:
https://developers.google.com/youtube/v3/docs/search/list
https://developers.google.com/youtube/v3/docs/channels/list#try-it
https://support.google.com/youtube/answer/2657968?hl=en
https://developers.google.com/youtube/v3/guides/working_with_channel_ids

Related

What are the parts of google adwords url?

I need to automatically generate url to the existing advertisement in google adwords. I've got a link like this:
https://adwords.google.com/cm/CampaignMgmt?authuser=1&__u=%param_one%&__c=%param_two%#c.%param_three%.create&app=cm
I do not know what are those params one, two, and three. I do not know where can I get them and what is their role in an address. But I'd like to! In fact I just need to generate a URL to click on it and appear on an ad page. How can that be done? May be their lib provides a method to generate url like this?
Unfortunately that's not possible, at least not in a reliable way (see also this rather old post from the Adwords API forum).
The structure itself is not hard to parse:
Inside the query string, __u is your user account's ID while __c refers to the ID of the specific Adwords account. These don't appear to be in standard Adwords XXX-XXX-XXXX format, though, so you would have to either create a mapping of all your accounts or come up with the proper translation function.
The fragment refers to a specific Adwords entity, for instance; c.123 is the campaign with ID 123 while a.ABC_XYZ refers to the adgroup with ID ABC inside the campaign with ID XYZ.
But even if you manage to get the __u and __c parameters right, more often than not you will be redirected to your Adwords account's top level view.
Interestingly enough, Google Analytics introduced little buttons in the "Adwords" reporting tab a few months ago which are supposed to take you right to the correct campaign if you have access to the linked account, but even those only work half of the time, especially if your Adwords account is an MCC.

Datasift Facebook PHP Client

How do I filter facebook data avaialble in datasift using csdl with a unique id which identifies a user.
Reading docos,
facebook.author.hash_id seems to be unique per user.
My CSDL,
$CSDL = 'facebook.author.hash_id == "some_facebook.author.hash_id"';
The datasift client library returns the following error,
"Uncaught exception 'DataSift_Exception_CompileFailed' with message
'The target facebook.author.hash_id does not exist' "
However I'm able to filter facebook messages using,
'facebook.author.type == "user"'
Both filter tags above, are mentioned in the below post.
[Ref http://dev.datasift.com/docs/data/dictionary ]
How do we filter facebook messages using the author (hash_id or other) ?
It is not possible to filter for individual Facebook users with Datasift. Facebook asked for all its data to be anonymized.
The output you get from Datasift is not a one for one reflection of what can be filtered on, take a look at the data dictionary.
The hash_id is created by Datasift to obscure the user's real ID. However, the hash_id will always be the same for a given user.
As of April 2015 the Facebook search API was deprecated. If you are looking for Facebook topic data you can take a look at Datasift PYLON.

How do I get YouTube channel identifier from a channel link? - PHP

I'm trying to get a YouTube channel's username or ID from their channel link. Some channels have unique usernames and some channels have random IDs. Is there a method to retrieve this ID in a string format from a given link to their channel using PHP?
The Channel resource (https://developers.google.com/youtube/v3/docs/channels) has a id property that YouTube uses to uniquely identify the channel.
You can use the Channel's list method (https://developers.google.com/youtube/v3/docs/channels/list) which will search for Channels based on a search criteria you provide. One possible search criteria is a username parameter. Once you get the channel that corresponds to that username, you can get the id property from that channel.

YouTube Api (V2.0 or 3.0) get channel info given channel url

I've to get 3 simple information from a youtube channel given the channel url.
Channel url(example): http://www.youtube.com/user/autocar
Needed info: Total videos, subscribers and total views.
I thought that those information should be simple to retrieve thanks to the API. Instead I'm going crazy trying to understand the Google Data documentation.
I'm working with PHP, so I suppose that I could use the php google client. But on the other side I'm pretty sure that to retrieve those simple information I can avoid to use the full library... anyway, someone knows how to get those data?
First, you need to find the channel id, some URLs come with channel id, some with username.
Great documentation here.
Basically, if it's a
Channel id, you already have it.
Username, channels.list(part="id", forUsername="username")
Once you have the channel id, you'll do a channel->list request to get all these details.
Response will have all the details.
Give it a try with API explorer with a channel id or username.
PHP Samples to get you started fast.
One way to get all the things you have requested is to search for channel
For instance
$channel_query = "channel_name";
$yt_url = "https://gdata.youtube.com/feeds/api/channels?q=".$channel_query."&alt=json&v=2";
$data = file_get_contents($yt_url);
echo "<pre>"; print_r(json_decode($data)); echo "</pre>";
After you get the feed, you would need to loop through the channel entries and match your channel. Best way of doing that is using your channel ID.

Is there any way to get the owner of a YouTube video via the 3.0 api?

I'm looking for some sort of username or uid field associated with the video (so I can confirm that a video someone claims to own is really owned by that person). I'd expect it to be in "snippet", but it's not there. I guess I could go through the channels interface and the playlist of the user, but I'd think the video's owner is something that would be available through the videos api itself. Any ideas?
snippet is correct, and the field is called "channelTitle"
Screenshot:
Yes, video owner is not returned from Videos resource.
You can do Channels->list
from relatedPlaylist.uploads.
Iterate through PlaylisItems to match.
Use Channels.list
snippet.channelTitle will give you the name of the Channel.
contentDetails.googlePlusUserId might give you more info, but only if the user has associated a Google+ account with with the YouTube channel.
Make sure to specify part='snippet,contentDetails'
There is also a lot of information in:
http://www.youtube.com/get_video_info?video_id=your-video-id, including a field called content_owner_name. It requires some parsing to extract the info.

Categories