DEV Community

Cover image for How I Used Github API to Generate a Dynamic Name Updating Every Minute!
Mohammad AbdulHakim
Mohammad AbdulHakim

Posted on • Edited on

How I Used Github API to Generate a Dynamic Name Updating Every Minute!

بسم الله الرحمن الرحيم

All praise is due to The Creator of the heavens and the earth

How I Used Github API to Generate a Dynamic Name Updating Every Minute!

1- Getting the current time in GMT

const currentDate = new Date().toUTCString();
console.log(currentDate)   // Sun, 28 May 2023 08:00:48 GMT
Enter fullscreen mode Exit fullscreen mode

we only want "08:00"

const hoursAndMinutes = currentDate.slice(currentDate.indexOf(":")-2,22);
Enter fullscreen mode Exit fullscreen mode

2- Generating the new name

so, the whole new name will be

const newname = `The time is ${hoursAndMinutes} GMT`;
console.log(newname) // The time is 08:00 GMT
Enter fullscreen mode Exit fullscreen mode

make a function which return the new name;

const generateNewName = () => {
  const currentDate = new Date().toUTCString();
  const hoursAndMinutes =
Enter fullscreen mode Exit fullscreen mode

بسم الله الرحمن الرحيم
All praise is due to The Creator of the heavens and the earth


1- Getting the current time in GMT

const currentDate = new Date().toUTCString();

console.log(currentDate)   // Sun, 28 May 2023 08:00:48 GMT
Enter fullscreen mode Exit fullscreen mode

we only want "08:00"


const hoursAndMinutes = currentDate.slice(currentDate.indexOf(":")-2,22);

Enter fullscreen mode Exit fullscreen mode

2- Generating the new name

so, the whole new name will be


const newname = `The time is ${hoursAndMinutes} GMT`;
console.log(newname) // The time is 08:00 GMT

Enter fullscreen mode Exit fullscreen mode

make a function which return the new name;


const generateNewName = () => {
  const currentDate = new Date().toUTCString();
  const hoursAndMinutes = currentDate.slice(currentDate.indexOf(":") - 2, 22);

  const newname = `The time is ${hoursAndMinutes} GMT`;
  return newname;
};
console.log(generateNewName()) // logs "The time is 08:00 GMT"
Enter fullscreen mode Exit fullscreen mode

3- Getting the github token

you will need your github token to use it when update your account.

you can find the steps get your token in github docs

  1. https://github.com/settings/tokens
  2. Generate new token (classic)
  3. select scopes you want, (be sure to select "user" scope to be able to "Update ALL user data")
  4. then generate token, and copy this.
  5. add this token to .env

4- installing packages

npm i dotenv
npm i cross-fetch


5- update account name with github api


const updateAccount = () =>{
    fetch('https://api.github.com/user', {
      method: 'PATCH',
      body: JSON.stringify({
        name: generateNewName(),
      }),
      headers: {
        'Content-type': 'application/json; charset=UTF-8',
        Authorization: `Bearer ${token}`,
      },
    })
}


Enter fullscreen mode Exit fullscreen mode

and run this function in interval each 60000ms


setInterval(() => {
    updateAccount()
}, 60000)

Enter fullscreen mode Exit fullscreen mode

6- run the code

write node {filename}.js in the terminal.
e.g. node index.js

so, the whole code will be


const dotenv = require("dotenv");
const fetch = require("cross-fetch")

dotenv.config()
const token = process.env.GITHUB_TOKEN


//*========> [ GENERATE THE NEW NAME ] <============= 
const generateNewName = () => {
  const currentDate = new Date().toUTCString();
  const hoursAndMinutes = currentDate.slice(currentDate.indexOf(":") - 2, 22);

  const newname = `The time is ${hoursAndMinutes} GMT`;
  return newname;
};
//!======================


//*========> [ UPDATE ACCOUNT FUNCTION ] <============= 
const updateAccount = async() =>{
    let res = await fetch('https://api.github.com/user', {
      method: 'PATCH',
      body: JSON.stringify({
        name: generateNewName(),
      }),
      headers: {
        'Content-type': 'application/json; charset=UTF-8',
        Authorization: `Bearer ${token}`,
      },
    })
    console.log(res.status)
}
//!=====================================


//*========> [ EVERY MINUTE ] <============= 
setInterval(() => {
    updateAccount()
}, 60000)
// !================

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
bkpecho profile image
Bryan King Pecho

Wow, what a clever use of the Github API! Your code for generating a dynamic name based on the current time is brilliant. 👏

Collapse
 
abomisr profile image
Mohammad AbdulHakim

Thanks! Glad you liked it.