Search code examples
javascriptsortinggoogle-apiyoutube-data-apigoogle-api-js-client

How to retrieve last subscribers with sorting such as in youtube?


I use simple method, such us

buildApiRequest('GET','/youtube/v3/channels',
{
  'mySubscribers': true,
  'maxResults': MaxResult,
  'part': 'snippet'
}

It works, but in result I can't see how I can sort them. I need last MaxResult subscribers, and I wanna sort them by join date on my channel.


Solution

  • The subscriptions.list and subscriberSnippet with myRecentSubscribers set to true returns a list of resent subscribers to your channel. It does not return all of them.

    If you check the response you will notice there is no date. You will not be able to see when someone subscribed.

    "subscriberSnippet": {
        "title": string,
        "description": string,
        "channelId": string,
        "thumbnails": {
          (key): {
            "url": string,0
            "width": unsigned integer,
            "height": unsigned integer
          }
        }
    

    The order parameter should allow you to order them by title you cant change the parameter it uses to sort on.

    buildApiRequest('GET',
                '/youtube/v3/subscriptions',
                {'part': 'subscriberSnippet',
                 'myRecentSubscribers': 'true',
                  'order', 'alphabetical'});
    

    Response

    {
     "kind": "youtube#subscriptionListResponse",
     "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/wLsZnuAVb0T9-bdRdCnreaWBHNM\"",
     "nextPageToken": "CAUQAA",
     "pageInfo": {
      "totalResults": 7,
      "resultsPerPage": 5
     },
     "items": [
      {
    
      "kind": "youtube#subscription",
       "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/3_huGriwUWV4fbbzcclmEoNYJ3w\"",
       "id": "moP_YQe1scKJgrI0udrz3B2tJTmRwvz4ev3R2_L4JmI",
       "subscriberSnippet": {
        "title": "Kortney W",
        "description": "",
        "channelId": "UC33FFHTxOZ6NRZAp9afsRBw",
        "thumbnails": {
         "default": {
          "url": "https://yt3.ggpht.com/-T6Sn1ur07bk/AAAAAAAAAAI/AAAAAAAAAAA/BSSSRckoD4k/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
         },
         "medium": {
          "url": "https://yt3.ggpht.com/-T6Sn1ur07bk/AAAAAAAAAAI/AAAAAAAAAAA/BSSSRckoD4k/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
         },
         "high": {
          "url": "https://yt3.ggpht.com/-T6Sn1ur07bk/AAAAAAAAAAI/AAAAAAAAAAA/BSSSRckoD4k/s800-c-k-no-mo-rj-c0xffffff/photo.jpg"
         }
        }
       }
      },
      {
    
    
       "kind": "youtube#subscription",
       "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/UVJds9Q4d24quS-sEG8Qw_3MBzU\"",
       "id": "gI5QI3teCs8unbR7__8oVg7KlRfOtWQYR70kXNkS4PY",
       "subscriberSnippet": {
        "title": "TheCorty",
        "description": "",
        "channelId": "UC-0O3PZ0VPNySP2bNFAPDIA",
        "thumbnails": {
         "default": {
          "url": "https://yt3.ggpht.com/-8C6KXmEqDho/AAAAAAAAAAI/AAAAAAAAAAA/1roVNa2yF0o/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
         },
         "medium": {
          "url": "https://yt3.ggpht.com/-8C6KXmEqDho/AAAAAAAAAAI/AAAAAAAAAAA/1roVNa2yF0o/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
         },
         "high": {
          "url": "https://yt3.ggpht.com/-8C6KXmEqDho/AAAAAAAAAAI/AAAAAAAAAAA/1roVNa2yF0o/s800-c-k-no-mo-rj-c0xffffff/photo.jpg"
         }
        }
       }
      }
     ]
    }
    
    1. this is only going to work if you own the channel
    2. it does not return all of your subscribers just the most recent.
    3. There is no date in the response you cant sort by date.
    4. you can test this here just make sure you authenticate to the correct channel