DavideViolante / socialblade-com-api

Unofficial APIs for socialblade.com website.
MIT License
14 stars 8 forks source link

Youtube URL Fix #86

Closed sferez closed 6 months ago

sferez commented 1 year ago

It seems that the URL generated for YouTube does not provide the 30D period for the table, but only the 14D-ish period.

The reason is that the URL is generated us as follows:

function generateUrl(source, username) {
  const userUrlCriteria = {
    facebook: 'page',
    youtube: 'channel',
  };
  const userUrl = userUrlCriteria[source] || 'user';
  const urlSuffix = source === 'facebook' ? '' : '/monthly';
  return `https://socialblade.com/${source}/${userUrl}/${username}${urlSuffix}`;
}

I think Social Blade updated their URL and now if you pass the 'channel' inside the URL it will redirect you to the user summary page and not the detailed statistics one.

The solution here is to pass the new URL with only 'c' as follows:

function generateUrl(source, username) {
  const userUrlCriteria = {
    facebook: 'page',
    youtube: 'c',
  };
  const userUrl = userUrlCriteria[source] || 'user';
  const urlSuffix = source === 'facebook' ? '' : '/monthly';
  return `https://socialblade.com/${source}/${userUrl}/${username}${urlSuffix}`;
}

This way the page access is: https:// socialblade .com/youtube/c/{username}/monthly

sferez commented 1 year ago

Update: it seems that for some account 'channel' work and for some 'c' work. From what I see for now channel works with the user_id of YouTube and c with the username.

Social Blade is working either with the user_id (and channel) or the username (and c) but you cannot really know in advance which one they will choose.

sferez commented 1 year ago

The fix that seems to work is to have by default the URL with 'c' for YouTube, as you usually work with the YouTube username and not the YouTube ID. In the case of a redirect to the 'channel' page (it means that Social Blade is using the youtube_id for its page) it will be redirected to the user summary and not the details statistics.

So when accessing the page we need to check if a redirect occurs and if we lost the /monthly property, if so we can add /monthly to the newly generated URL

await page.goto(url);
const currentUrl = await page.url();
if (!currentUrl.endsWith('monthly') && url.endsWith('monthly')) {
  console.log('Redirecting to monthly');
  await page.goto(currentUrl + '/monthly');
}

I didn't do exhaustive testing but it seems to work with all the artists I'm dealing with.

DavideViolante commented 1 year ago

Thank you @sferez for the detailed replies. Can you please tell me some youtube usernames and user ids so I do some tests? Using 'c' with selenagomez returns correctly 30 days. I think I need some other usernames or user ids where it does not work the 'c'. Thanks

Edit: I pushed this change for now. https://github.com/DavideViolante/socialblade-com-api/commit/91dfa6742c6d3e07aaddd21fc754e62230bde517

sferez commented 1 year ago

Some examples:

tamerhosny : use 'c' imbryantbarnes : use 'channel' ryanmackmusic : use 'c' dylangossettmusic : use 'channel'