Random Famous Person Birthday of The Day Command

Asked Dec 19, 2022·8 replies·Last activity 3 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

Hey, community. Looking for an api or command that gives me a random birthday of the day. I couldn't find anything promising upon initial research, so if you could, at the very least, point me in the right direction, you'd be helping me automate my friend's stream a little bit.

Much love, and I much appreciate you guys.

8 replies

You'd probably need to have an API for that, and do it with a "urlfetch". Haven't heard of one that already exists to my knowledge.

I just wish I knew of one, as well. 😦

Hi, here is a command I made for you:

text
$(eval $(urlfetch json https://pastebin.com/raw/P3dVrU4n)("$(querystring $(urlfetch json https://www.onthisday.com/today/birthdays.php))"))

You can add it through chat like this:

text
!commands add !famousbirthday $(eval $(urlfetch json https://pastebin.com/raw/P3dVrU4n)("$(querystring $(urlfetch json https://www.onthisday.com/today/birthdays.php))"))

This command gets the current day's famous birthdays from https://www.onthisday.com/today/birthdays.php.

Here’s the code for the part of the command that’s hosted on Pastebin. I had to host the majority of the command’s code on Pastebin since the code was too long for Nightbot by itself. This will be useful if the Pastebin link ever expires.

Code: ||Minified:

javascript
javascript
(s=>(()=>{let t="https://www.onthisday.com/today/birthdays.php",e=decodeURIComponent(s);if(/^(?:\[?Error|Remote Server Returned Code) /.test(e))return`Failed to access ${t}: ${e}`;let a=e.match(/<span\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")month(?=\s|")[^"]*"[^>]*>\s*([a-z]+)\s*<\/span\s*>\s*<span\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")date(?=\s|")[^"]*"[^>]*>\s*(\d+)\s*<\/span\s*>/i);if(null==a)return`Failed to parse today's date from ${t}: ${e}`;let[,l,r]=a,d=[...e.matchAll(/<li\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")person(?=\s|")[^"]*"[^>]*>((?:.|\s)+?)<\/li\s*>|(<span\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")poi__heading-txt(?=\s|")[^"]*"[^>]*>(?:.|\s)+?<\/p\s*>)/gi)];if(0===d.length)return`Failed to parse any birthdays from ${t}: ${e}`;let n=d[Math.floor(Math.random()*d.length)],[,i,o]=n,p=s=>s.replace(/<[^>]+>/g,""),h=(()=>{let s={amp:"&",lt:"<",gt:">",quot:'"',"#39":"'","#x27":"'","#x60":"`"},t=RegExp(`&(${Object.keys(s).join("|")});`,"g");return e=>e.replace(t,(t,e)=>s[e])})(),u=s=>h(p(s)),m,c,y,f;if(null!=i){let g=u(i),b=g.match(/^\s*(\d+)\s+((?:.|\s)+?)(?:\s*,\s*((?:.|\s)+?)??)?(?:\s*\(\s*d\.\s*(\d+)\s*\))?(?:\s*\[\s*\d+\s*\])*\s*$/i);if(null==b)return`Failed to parse birthday from ${t}: ${g}`;[,c,m,f,y]=b}else{let F=u(o),$=F.match(/^\s*((?:.|\s)+?)\s*\(\s*(\d+)\s*(?:-\s*(\d+)\s*\)\s*((?:.|\s)+?)??|[a-z]+\s+Birthday\s*\)\s*(\d+)(?:\s+((?:.|\s)+?))??)(?:\s*\[\s*\d+\s*\])*\s*$/i);if(null==$)return`Failed to parse birthday from ${t}: ${F}`;let x,w,z,R;[,m,x,y,w,z,R]=$,c=z??x,f=null==z?w:R}return`Famous birthday today, ${l} ${r}: ${m} (${c}–${y??""})${null==f?"":`: ${f}`}`})().substring(0,400))

Non-minified:

javascript
((
/** @type {string} */ urlEncodedTodaysFamousBirthdaysHtml
) => (() => {
javascript
const srcSiteUrl = 'https://www.onthisday.com/today/birthdays.php';
const todaysFamousBirthdaysHtml = decodeURIComponent(urlEncodedTodaysFamousBirthdaysHtml);
if (/^(?:\[?Error|Remote Server Returned Code) /.test(todaysFamousBirthdaysHtml)) {
/* Return Nightbot urlfetch error message */
return `Failed to access ${srcSiteUrl}: ${todaysFamousBirthdaysHtml}`;
}
const todayMatch = todaysFamousBirthdaysHtml.match(
/* <span class="month">December</span> <span class="date">28</span> */
/<span\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")month(?=\s|")[^"]*"[^>]*>\s*([a-z]+)\s*<\/span\s*>\s*<span\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")date(?=\s|")[^"]*"[^>]*>\s*(\d+)\s*<\/span\s*>/i
);
if (todayMatch == null) {
return `Failed to parse today's date from ${srcSiteUrl}: ${todaysFamousBirthdaysHtml}`;
}
const [, todayMonthName, todayDayOfMonthString] = todayMatch;
const birthdayHtmlMatches = [...todaysFamousBirthdaysHtml.matchAll(
/*
<li class="person"><a href="/birthdays/date/1164" class="birthDate">1164</a> Emperor Rokujō of Japan (d. 1176)</li>
<span class="poi__heading-txt">Woodrow Wilson <span class="poi__date">(1856-1924)</span></span></a></h3></header><p>28th <a href="/people/us-presidents">President of the United States</a> (Democrat: 1913-21) and 1919 Nobel Peace Prize Laureate, born in Staunton City, Virginia</p>
*/
/<li\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")person(?=\s|")[^"]*"[^>]*>((?:.|\s)+?)<\/li\s*>|(<span\s[^>]*(?<=\s)class\s*=\s*"[^"]*(?<=\s|")poi__heading-txt(?=\s|")[^"]*"[^>]*>(?:.|\s)+?<\/p\s*>)/gi
)];
if (birthdayHtmlMatches.length === 0) {
return `Failed to parse any birthdays from ${srcSiteUrl}: ${todaysFamousBirthdaysHtml}`;
}
javascript
const birthdayHtmlMatch = birthdayHtmlMatches[Math.floor(Math.random() * birthdayHtmlMatches.length)];
const [, denseBirthdayHtml, poiBirthdayHtml] = birthdayHtmlMatch;
const stripHtmlElements = (/** @type {string} */ html) => html.replace(/<[^>]+>/g, '');
const decodeHtmlText = (() => {
const textByCode = {
amp: '&',
lt: '<',
gt: '>',
quot: '"',
'#39': "'",
'#x27': "'",
'#x60': '`'
};
const codeRegex = RegExp(`&(${Object.keys(textByCode).join('|')});`, 'g');
return (/** @type {string} */ text) => text.replace(codeRegex, (_, code) => textByCode[code]);
})();
const renderHtmlToText = (/** @type {string} */ html) => decodeHtmlText(stripHtmlElements(html));
/** @type {string} */ let name;
/** @type {string} */ let birthYearString;
/** @type {string | undefined} */ let deathYearString;
/** @type {string | undefined} */ let description;
if (denseBirthdayHtml != null) {
const srcBirthdayText = renderHtmlToText(denseBirthdayHtml);
const birthdayMatch = srcBirthdayText.match(
/*
1164 Emperor Rokujō of Japan (d. 1176)
1952 Arun Jaitley, Indian Politician
1913 Lou Jacobi, Canadian actor and comedian (Irma la Douce, The Dean Martin Show), born in Toronto, Ontario (d. 2009) [1]
1989 Mackenzie Rosman
*/
/^\s*(\d+)\s+((?:.|\s)+?)(?:\s*,\s*((?:.|\s)+?)??)?(?:\s*\(\s*d\.\s*(\d+)\s*\))?(?:\s*\[\s*\d+\s*\])*\s*$/i
);
if (birthdayMatch == null) {
return `Failed to parse birthday from ${srcSiteUrl}: ${srcBirthdayText}`;
}
[, birthYearString, name, description, deathYearString] = birthdayMatch;
} else { /* poiBirthdayHtml != null */
const srcBirthdayText = renderHtmlToText(poiBirthdayHtml);
javascript
const birthdayMatch = srcBirthdayText.match(
/*
Terry Sawchuk (1929-1970) Canadian Hockey Hall of Fame goaltender (4-time Vezina Trophy & Stanley Cup winner), born in Winnipeg, Manitoba
Maggie Smith (88th Birthday) 1934 English actress (Harry Potter, Death on the Nile, Clash of Titans), born in Ilford, England
Adam Peaty (28th Birthday) 1994
Liu Xiaobo (1955-2017) [1]
*/
/^\s*((?:.|\s)+?)\s*\(\s*(\d+)\s*(?:-\s*(\d+)\s*\)\s*((?:.|\s)+?)??|[a-z]+\s+Birthday\s*\)\s*(\d+)(?:\s+((?:.|\s)+?))??)(?:\s*\[\s*\d+\s*\])*\s*$/i
);
if (birthdayMatch == null) {
return `Failed to parse birthday from ${srcSiteUrl}: ${srcBirthdayText}`;
}
/** @type {string} */ let deadBirthYearOrAliveAgeString;
/** @type {string | undefined} */ let deadDescription;
/** @type {string | undefined} */ let aliveBirthYearString;
/** @type {string | undefined} */ let aliveDescription;
[, name, deadBirthYearOrAliveAgeString, deathYearString, deadDescription, aliveBirthYearString, aliveDescription] = birthdayMatch;
birthYearString = aliveBirthYearString ?? deadBirthYearOrAliveAgeString;
description = aliveBirthYearString == null ? deadDescription : aliveDescription;
}
return `Famous birthday today, ${todayMonthName} ${todayDayOfMonthString}: ${name} (${birthYearString}–${deathYearString ?? ''})${description == null ? '' : `: ${description}`}`;
})().substring(0, 400))
```||

Didn't find your answer? The community is on Discord.

Ask on Discord