15

I'm trying to figure out a way to grab the top 50,000 most subscribed youtube channels using javascript. These only need to be grabbed once and will be stored in a file to be used for an autocomplete input in a webpage.

I've gotten pretty close to getting the first top 50 by using search:list (/youtube/v3/search) by searching with parameters maxResults=50, order=viewCount, part=snippet, type=channel, fields=nextPageToken,items(snippet(channelId,title))

Returning:

{
 "nextPageToken": "CDIQAA",
 "items": [{
   "snippet": {
    "channelId": "UC-9-kyTW8ZkZNDHQJ6FgpwQ",
    "title": "Music"
   }
  },{
   "snippet": {
    "channelId": "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
    "title": "PewDiePie"
   }
  },{
   "snippet": {
    "channelId": "UCVPYbobPRzz0SjinWekjUBw",
    "title": "Анатолий Шарий"
   }
  },{
   "snippet": {
    "channelId": "UCam8T03EOFBsNdR0thrFHdQ",
    "title": "VEGETTA777"
   }
  },...

Then all I'd have to do is fetch that 1000 more times using the nextPageToken to get a list of the top 50,000.

Unfortunately, sorting by relevance, rating, viewCount, or nothing is not yielding the 50 most subscribed channels, and there doesn't seem to be any sort of way to order them by subscriber count according to the documentation; so it seems like i am stuck.

John Doe
  • 397
  • 3
  • 18
Marc Guiselin
  • 3,442
  • 2
  • 25
  • 37
  • Possible workaround: Scraping https://socialblade.com/youtube/top/5000/mostsubscribed – paolo Apr 23 '17 at 21:56
  • Thank for the suggestion @paolo, but I've already done that and only the 5000 highest channels is not going to cut it for me. – Marc Guiselin Apr 23 '17 at 23:52
  • There's no direct way of doing this from the API. The Top 10s I can find are found in https://developers.google.com/youtube/analytics/v1/dimsmets/Dimensions](https://developers.google.com/youtube/analytics/v1/dimsmets/dims#Dimensions) and there's no mention of top subscribers. – ReyAnthonyRenacia Apr 25 '17 at 03:07
  • Since you wrote "to be used for an autocomplete", I was wondering: Do you, at any point, need the data for something other than the autocomplete and does the autocomplete have anything to do with how many subscribers a channel has? Because otherwise simply searching with the current value of the input field whenever the input changes might give you even better autocomplete results. – paolo Apr 28 '17 at 09:54
  • Besides being used for an autocomplete the list will be used on a variety of other things like a dictionary for conversion of user ids to username. You make a good point however. I could use that api if the search using the list hasn't found anything. – Marc Guiselin Apr 28 '17 at 14:35
  • I found a link that may help: [https://developers.google.com/youtube/reporting/v1/reports/metrics#subscribers_gained](https://developers.google.com/youtube/reporting/v1/reports/metrics#subscribers_gained) However, I'm not sure how to use the API, so I can't give a code sample. – Brendan Lewis Apr 30 '17 at 11:52
  • If anyone's got a better answer here's your chance to earned my bounty. – Marc Guiselin Apr 30 '17 at 20:32

2 Answers2

4

Just before you writing your 50 results in file (or database), you can make one more API call, using channelId field from your result, and merge all of them with comma delimited and make another API call Channels: list.
On that page for example you can use following parameters:
(these are IDs from your example above)

part=statistics
id=UC-9-kyTW8ZkZNDHQJ6FgpwQ,UC-lHJZR3Gqxm24_Vd_AJ5Yw,UCVPYbobPRzz0SjinWekjUBw,UCam8T03EOFBsNdR0thrFHdQ`

And result will look something like this:

{
"kind": "youtube#channel",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/MG6zgnd09mqb3nAdyRnPDgFwfkE\"",
"id": "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
"statistics": {
     "viewCount": "15194203723",
     "commentCount": "289181",
     "subscriberCount": "54913094",
     "hiddenSubscriberCount": false,
     "videoCount": "3175"
    }
}

And you can take subscriberCount from result for each channel.

I know, this is not the way to sort your 50 results while writing into the file, but with this you can sort later your results by "subscriber count" while fetching from file for your autocomplete input.

I didn't find any other way to sort results by subscriber count, so maybe this can be helpful.

Nebojsa Nebojsa
  • 1,395
  • 1
  • 7
  • 19
  • Unfortunately this looks like it might be the only way to do this. Thanks for the answer. I'll mark it as correct in a few days unless someone comes up with a better solution. – Marc Guiselin May 04 '17 at 18:35
  • Unfortuantely this method is limited to only 500 results because of how the page tokens work – Root0x Mar 29 '21 at 18:06
-6

The idea to do is to run a server side script, that makes RESTful api calls in a loop, and writes the results to .JSON file, to save results. For that you can create PHP script, that makes REST API call to google, and fetch first 50 results, and then use file write operations to write your results. Run that PHP script as corn job to update results at regular intervals. Executing corn job at every specific time interval you set keeps results fresh.

Hit CURL command with loop for next, to fetches 50 results every time and create temp file with all the results saved in .JSON file. Once your results are fetched, replace your old JSON file with newly created temporary file. This will generate fresh JSON file are regular, with new results if any changes are made to data.

However, the idea to use temporary file is to avoid script avoid wait/slow of AJAX down due to consistent read and write operations on same file. Once temporary file is written, simply use move command to replace the actual file.

Make sure, you use cache control headers in AJAX results to keep its freshness of data.

Atul Jindal
  • 946
  • 8
  • 8
  • 3
    How is that answering the question? – Preview Apr 26 '17 at 14:26
  • Agreed, to fully answer the question you have to use `javascript`, list search parameters and urls with the `youtube-api-v3`, and propose either a precise explanation of how to grab the list, pseudocode, or working javascript. – Marc Guiselin Apr 26 '17 at 15:31