1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-21 17:27:01 +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) logError(error.message)
} }
/* try { try {
const twitter = await fetchTwitter() const twitter = await fetchTwitter()
response = { ...response, twitter } if (twitter) response = { ...response, twitter }
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} */ }
try { try {
const telegram = await fetchTelegram() const telegram = await fetchTelegram()

View File

@ -1,19 +1,50 @@
import axios from 'axios' 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() { export default async function fetchTwitter() {
const url = const url = 'https://api.twitter.com/2/users/me?user.fields=public_metrics'
'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol'
const start = Date.now() 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) { if (response.status !== 200) {
logError(`Non-200 response code from Twitter: ${response.status}`) logError(`Non-200 response code from Twitter: ${response.status}`)
return null return null
} }
const json = response.data if (response.data.data.public_metrics.followers_count === undefined)
const followers = json[0].followers_count 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( log(
'✓ Twitter. ' + '✓ 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 chalk = require('chalk')
const log = (text) => console.log(text) const log = (text) => console.log(text)
@ -13,4 +14,32 @@ const arrSum = (arr) => {
return sum 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}}

19234
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,11 @@
"dependencies": { "dependencies": {
"axios": "^0.25.0", "axios": "^0.25.0",
"chalk": "^4.1.2", "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": { "devDependencies": {
"eslint": "^7.32.0", "eslint": "^7.32.0",