بسم الله الرحمن الرحيم
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
we only want "08:00"
const hoursAndMinutes = currentDate.slice(currentDate.indexOf(":")-2,22);
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
make a function which return the new name;
const generateNewName = () => {
const currentDate = new Date().toUTCString();
const hoursAndMinutes =
…بسم الله الرحمن الرحيم
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
we only want "08:00"
const hoursAndMinutes = currentDate.slice(currentDate.indexOf(":")-2,22);
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
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"
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
- https://github.com/settings/tokens
- Generate new token (classic)
- select scopes you want, (be sure to select "user" scope to be able to "Update ALL user data")
- then generate token, and copy this.
- 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}`,
},
})
}
and run this function in interval each 60000ms
setInterval(() => {
updateAccount()
}, 60000)
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)
// !================
Top comments (2)
Wow, what a clever use of the Github API! Your code for generating a dynamic name based on the current time is brilliant. 👏
Thanks! Glad you liked it.