microlink logo

Microlink Recipe

Interact with twitter.com

twitter.js
const mql = require('@microlink/mql')

const twitter = async (url, opts) => {
  const result = await mql(url, {
    data: {
      stats: {
        selector: 'main',
        attr: {
          tweets: {
            selector:
              '[data-testid="titleContainer"] div[dir="auto"]:nth-child(2)',
          },
          followings: {
            selector: 'a[href*="following"] span span',
          },
          followers: {
            selector: 'a[href*="followers"] span span',
          },
        },
      },
    },
    ...opts,
  })

  if (result.data.stats.tweets) {
    result.data.stats.tweets = Number(
      result.data.stats.tweets.replace(' Tweets', '')
    )
  }

  return result
}

const result = await twitter('https://twitter.com/microlinkhq')
console.log(result)