Skip to content

Commit

Permalink
apply with twitter ( without views stats ) (#271)
Browse files Browse the repository at this point in the history
## PR Description

Hey team,

This PR aims to address a change in our Twitter API usage. We're
transitioning back to Twitter API v1.1 from v2, mainly due to the cost
associated with the v2 version.

As a result of this shift, we can gather all required information about
a specific link, except for its 'Views'. We're aware of the importance
of this data, and we're planning to develop a feature for scraping views
in the future.

## Changes Made

- Switched back from Twitter API v2 to v1.1 due to cost considerations.
- Adjusted our data retrieval mechanisms to function effectively with
API v1.1, now able to retrieve all link-related information, with the
exception of 'Views'.
- Laid groundwork for future implementation of a view-scraping feature.

## Notes for Reviewers

- Please review the changes made, focusing particularly on the API
transition and its potential effects on the overall functionality.
- Verify that the system works correctly with Twitter API v1.1, and that
all link-related data, barring 'Views', is retrieved successfully.
- We'd like to draw your attention to the planned view-scraping feature.
We welcome any suggestions or recommendations that could aid us in this
future implementation.


Thank you for your time and contributions. Your feedback and suggestions
are greatly appreciated.

Best regards,
Louay HICHRI
  • Loading branch information
ksibisamir committed Jun 22, 2023
2 parents 1be59f9 + a46b37a commit 75617ba
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions manager/oracles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {
IgMedia,
LinkedinProfile,
} = require('../model/index')
var Twitter2 = require('twitter-api-v2')
var fs = require('fs')
const axios = require('axios')

Expand Down Expand Up @@ -150,15 +149,15 @@ exports.verifyInsta = async function (userId, idPost) {
exports.verifyTwitter = async function (twitterProfile, userId, idPost) {
try {
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: twitterProfile.access_token_key,
access_token_secret: twitterProfile.access_token_secret,
bearer_token: process.env.TWITTER_BEARER_TOKEN
})
const tweet = await client.get(`https://api.twitter.com/2/tweets?ids=${idPost}&tweet.fields=author_id`, {params: {}});
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: twitterProfile.access_token_key,
access_token_secret: twitterProfile.access_token_secret,
bearer_token: process.env.TWITTER_BEARER_TOKEN
});
const tweet = await client.get('statuses/show', { id: idPost });
var twitterProfile = await TwitterProfile.findOne({
id: tweet.data[0].author_id,
id: tweet.user.id_str,
UserId: userId,
}).select('access_token_key access_token_secret id')
return twitterProfile ? true : false
Expand Down Expand Up @@ -705,36 +704,24 @@ const instagram = async (UserId, link) => {

const twitter = async (userName, idPost) => {
try {

var tweet = new Twitter({
consumer_key: oauth.twitter.consumer_key_alt,
consumer_secret: oauth.twitter.consumer_secret_alt,
access_token_key: oauth.access_token_key,
access_token_secret: oauth.access_token_secret,
bearer_token: process.env.TWITTER_BEARER_TOKEN
})

var tweet_res = await tweet.get('statuses/show', { id: idPost })
var twitterProfile = (
await TwitterProfile.find({
id: tweet_res.user.id_str,
})
)[0]

const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: twitterProfile.access_token_key,
access_token_secret: twitterProfile.access_token_secret,
bearer_token: process.env.TWITTER_BEARER_TOKEN
})
const res = await client.get(`https://api.twitter.com/2/tweets?ids=${idPost}&tweet.fields=public_metrics&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text`, {params: {}});
const res = await tweet.get(`https://api.twitter.com/1.1/statuses/show.json?id=${idPost}&include_entities=true`, {params: {}});
var perf = {
shares: res.data[0]?.public_metrics.retweet_count,
likes: res.data[0]?.public_metrics.like_count,
views: res.data[0]?.public_metrics.impression_count,
shares: res.retweet_count,
likes: res.favorite_count,
// views: No direct equivalent in API v1.1, impression count is not available
date: Math.floor(Date.now() / 1000),
media_url: res.includes?.media[0]?.url || ' ',
}
// API v1.1 doesn't provide a 'media_url' in the same manner as API v2.
// Here we are trying to find it from the entities->media object if it exists.
media_url: res.extended_entities.media[0]?.media_url_https || ' ',
};
return perf
} catch (err) {
console.error('error twittRate limit exceededer oracles', err)
Expand Down Expand Up @@ -873,7 +860,6 @@ exports.getButtonStatus = (link) => {
link.payedAmount = link.payedAmount || '0'

if (link.status === false) {
console.log('false')
return 'waiting_for_validation'
}

Expand Down

0 comments on commit 75617ba

Please sign in to comment.