1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-12-22 01:03:26 +01:00

fix: init twitter fix test

This commit is contained in:
Gavrila Alexandru 2023-05-24 13:47:53 +03:00
parent eaa71ea64a
commit 8a3758007f
6 changed files with 19056 additions and 271 deletions

View File

@ -38,12 +38,12 @@ export default async (req, res) => {
logError(error.message)
}
/* try {
try {
const twitter = await fetchTwitter()
response = { ...response, twitter }
if (twitter) response = { ...response, twitter }
} catch (error) {
logError(error.message)
} */
}
try {
const telegram = await fetchTelegram()

View File

@ -1,19 +1,50 @@
import axios from 'axios'
import { log, logError } from '../utils'
import fs from 'fs'
import { log, logError, Oauth1Helper } from '../utils'
import jsonDb from '../../jsonDb.json'
export default async function fetchTwitter() {
const url =
'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol'
const url = 'https://api.twitter.com/2/users/me?user.fields=public_metrics'
const start = Date.now()
const response = await axios.get(url)
let followers = 0
const { twitter } = jsonDb
const currentTimestamp = Math.floor(Date.now() / 1000)
const oneDay = 60 * 60 * 24
const compareDatesBoolean = currentTimestamp - twitter.lastFetch > oneDay
if (!compareDatesBoolean) return { followers: twitter.followers }
const request = {
url,
method: 'GET'
}
const authHeader = await Oauth1Helper.getAuthHeaderForRequest(request)
const response = await axios.get(url, { headers: authHeader })
if (response.status !== 200) {
logError(`Non-200 response code from Twitter: ${response.status}`)
return null
}
const json = response.data
const followers = json[0].followers_count
if (response.data.data.public_metrics.followers_count === undefined)
return null
followers = response.data.data.public_metrics.followers_count
var stream = fs.createWriteStream('jsonDb.json')
const saveData = {
twitter: {
followers,
lastFetch: currentTimestamp
}
}
stream.once('open', function (fd) {
stream.write(JSON.stringify(saveData))
stream.end()
})
log(
'✓ Twitter. ' +

View File

@ -1,4 +1,5 @@
/* eslint-disable no-console */
import oauth1a from 'oauth-1.0a'
import crypto from 'crypto'
const chalk = require('chalk')
const log = (text) => console.log(text)
@ -13,4 +14,32 @@ const arrSum = (arr) => {
return sum
}
module.exports = { log, logError, arrSum }
const CONSUMERKEY = 'tzOlj9Tzhh16QZS1hKZHjJuj1'
const CONSUMERSECRET = '9HEmhjp6ZnRFWjisg8A6Z2IOXIZqkESaSWgIGEDqbuw4fHILLM'
const TOKENKEY = '154621635-qvE6cD5Ypr8zfk0gnGTsrzysw06VRZxUyF0d2kyM'
const TOKENSECRET = 'ij7DoC00Nux7ICClZJuyM3v8pP11gAsGP6ehDxAMhlTHw'
class Oauth1Helper {
static getAuthHeaderForRequest(request) {
const oauth = oauth1a({
consumer: { key: CONSUMERKEY, secret: CONSUMERSECRET },
signature_method: 'HMAC-SHA1',
// eslint-disable-next-line camelcase
hash_function(base_string, key) {
return crypto
.createHmac('sha1', key)
.update(base_string)
.digest('base64')
}
})
const authorization = oauth.authorize(request, {
key: TOKENKEY,
secret: TOKENSECRET
})
return oauth.toHeader(authorization)
}
}
module.exports = { log, logError, arrSum, Oauth1Helper }

1
jsonDb.json Normal file
View File

@ -0,0 +1 @@
{"twitter":{"followers":20,"lastFetch":1684920754}}

19238
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,11 @@
"dependencies": {
"axios": "^0.25.0",
"chalk": "^4.1.2",
"cheerio": "^1.0.0-rc.10"
"cheerio": "^1.0.0-rc.10",
"crypto": "^1.0.1",
"fs": "^0.0.1-security",
"oauth-1.0a": "^2.2.6",
"vercel": "^29.4.0"
},
"devDependencies": {
"eslint": "^7.32.0",