1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-12-23 01:29:49 +01:00
community-numbers/api/networks/medium.js

28 lines
726 B
JavaScript
Raw Normal View History

2020-06-16 11:12:27 +02:00
import fetch from 'node-fetch'
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() {
2020-06-16 12:32:47 +02:00
const url = 'https://medium.com/oceanprotocol?format=json'
const start = Date.now()
const response = await fetch(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
2020-06-16 12:32:47 +02:00
const responseText = await response.text()
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
}