Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 5, 2020 12:29 pm GMT

Twitter Dynamic Name Generator

Alt Text

Twitter dynamic name generator is a pet project in which the twitter profile name updates for every minute with the followers count. I just written this article as a journel note of how I did this.

It was done as one of the challenge of #100DaysOfCode. Thanks for to Suren and Vadim Bauer for their idea inspiration. Special thanks to Karthikeyan for his guidance.

Tech Stack Includes,

How I did this? What all the steps?

Initially started with the guidance of this aritcle written by Suren. I majorly break down this task into 3 parts.

  1. Getting Twitter API key from Twitter Dev account.
  2. Node js script to update the profile name.
  3. Cron part to update the profile name for every 1 minute.

Step 1 - Getting twitter API keys

Went to the twitter developers page as mentioned in the article and signed in. By providing the required informations, twitter asked me to verify with my account email. Once verified with the email, my developers account was created.(Hurray!) In the Create an app after entering the informations like app name, app description and so on, it asked me to clearly explain the reason of how I'm going to use the app. After reviewing the terms, my app was be created. Noted down the Consumer API key, Consumer API Secret key and Access token & access token secret keys!

At the end this step I got my Consumer API key, Consumer API Secret key, Access token, Access secret key.

Next interesting part! lets to dive into the coding part!

Step 2 - Script to update the profile name.

Okay, now it coding time. Fire mode on
I choosed node.js as I'am familiar with that. From the Suren article I came to know about the twitter-lite.

Started with the simple,

$ npm init$ npm install twitter-lite

In the index.js file, imported the twitter-lite and update the required keys as we got from the previous step,

const Twitter = require("twitter-lite");const client = new Twitter({  subdomain: "api", // api is the default  version: "1.1", // version 1.1 is the default  consumer_key: process.env.twitter_consumer_key,  consumer_secret: process.env.twitter_consumer_secret,  access_token_key: process.env.twitter_access_token_key,   access_token_secret: process.env.twitter_access_token_secret, });

Now comes the actually coding part.
So what is the logic to do this?

  1. We need to get the followers count using twitter api.
  2. We need to append the followers count with the name, but with emoji (Hmm)
  3. We need to update the new name to profile name using twitter api.

Cool, lets do it!
To get the followers count, used the twitter lite api.

client    .get("account/verify_credentials")    .then((results) => {      const followerCount = results.followers_count;    });    .catch(console.error);};

Now, some little tricks! I got all the logic but struck with how to update the emoji dynamically. So asked Suren in the twitter. He just replied with, Have JSON with matching emoji. Gotcha, now continue with the journey!
Next simple logics like, get the followers count, convert to string, change to string array, then finally reduce the followers count with mapped emoji.

exports.handler = () => {  client    .get("account/verify_credentials")    .then((results) => {      const followerCount = results.followers_count;      const string = followerCount.toString();      const stringSplit = string.split("");      const followers = stringSplit.reduce((acc, val) => {        return acc + numberMatch[val];      }, "");      const profile_name = `${name} | ${emoji} |" + ${followers}`;      console.log("profile_name: ", profile_name);    })    .catch(console.error);};const numberMatch = {  "0": "0",  "1": "1",  "2": "2",  "3": "3",  "4": "4",  "5": "5",  "6": "6",  "7": "7",  "8": "8",  "9": "9",};

Checked with by running,

node index.js

Coool!, now got the console with the new name, appended with the emoji. Now next is, need to update the profile name. Used the same twitter-lite api, to update the profile.

  const response = client.post("account/update_profile", {        name: user_name,      });

Ran again to check whether the name gets updated. ( Tik tik tik moments) Hurray

It works! Now comes the finaly part!

Step 3 - Cron to update the profile name for every 1 minute.

Okay! Now all works fine, but how to make this to run for every minute? Here comes my technical guru Karthikeyan into the play. Since he is an serverless expert, he suggested me to run the cron in AWS Lambda function since I'm already familiar with lambda. Okay! Cool let's do it. I'm skipping the AWS setup, since it takes too long, and diving direcly into the cron part.

service: twitter-schedulercustom:    twitter_consumer_key: ${ssm:/twitter_consumer_key~true}    twitter_consumer_secret: ${ssm:/twitter_consumer_secret~true}    twitter_access_token_key: ${ssm:/twitter_access_token_key~true}    twitter_access_token_secret: ${ssm:/twitter_access_token_secret~true}provider:  name: aws  runtime: nodejs12.x  stage: prod  region: ap-south-1  environment:      STAGE: prodfunctions:  subscription:    handler: index.handler    environment:       twitter_consumer_key: ${self:custom.twitter_consumer_key}      twitter_consumer_secret: ${self:custom.twitter_consumer_secret}      twitter_access_token_key: ${self:custom.twitter_access_token_key}      twitter_access_token_secret: ${self:custom.twitter_access_token_secret}    events:      - schedule: rate(1 minute)

This cron runs for every 1 minute, and the new profile name is updated dynamically. New profile name is consoled in cloud watch.

I have used service AWS System Manager(SSM) to avoid the exposure of explicit keys.

Finally, my work is done. Here is the link to the source code.

For any doubts, suggestions and feedback contact me in my twitter profile @code_rams. Hope you enjoyed and find this useful. Thanks for reading.


Original Link: https://dev.to/code_rams/twitter-dynamic-name-generator-3ka2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To