1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-25 03:20:17 +01:00
community-numbers/api/networks/medium.js

28 lines
716 B
JavaScript
Raw Permalink Normal View History

2021-11-09 09:36:02 +01:00
import axios from 'axios'
2020-06-16 11:12:27 +02:00
import { log, logError } from '../utils'
2019-05-14 20:52:48 +02:00
2020-06-16 11:12:27 +02:00
export default async function fetchMedium() {
2021-11-09 09:36:02 +01:00
const url = 'http://medium.com/oceanprotocol?format=json'
2020-06-16 12:32:47 +02:00
const start = Date.now()
2021-11-09 09:36:02 +01:00
const response = await axios.get(url)
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
if (response.status !== 200) {
logError(`Non-200 response code from Medium: ${response.status}`)
return null
}
2019-05-14 20:52:48 +02:00
2021-11-09 10:04:05 +01:00
const responseText = response.data
2020-06-16 12:32:47 +02:00
const json = await JSON.parse(responseText.replace('])}while(1);</x>', ''))
const { collection } = json.payload
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
const followers = collection.metadata.followerCount
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
log(
2020-06-16 15:16:31 +02:00
'✓ Medium. ' +
2020-06-16 12:32:47 +02:00
`Total: ${followers} followers. ` +
`Elapsed: ${new Date() - start}ms`
)
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
return { followers }
2019-05-14 20:52:48 +02:00
}