mirror of
https://github.com/streamwall/streamwall.git
synced 2026-01-26 23:12:48 -05:00
Add ability to poll data from spreadsheet
This commit is contained in:
@@ -1,18 +1,47 @@
|
||||
import zip from 'lodash/zip'
|
||||
import { promisify } from 'util'
|
||||
import fetch from 'node-fetch'
|
||||
import csv from 'csvtojson'
|
||||
import { GoogleSpreadsheet } from 'google-spreadsheet'
|
||||
|
||||
const sleep = promisify(setTimeout)
|
||||
|
||||
const PUBLIC_DATA_URL = 'https://woke.net/csv'
|
||||
const PUBLIC_DATA_REFRESH_INTERVAL = 5 * 60 * 1000
|
||||
function filterLive(data) {
|
||||
return data.filter((d) => d.Link && d.Status === 'Live')
|
||||
}
|
||||
|
||||
export async function* pollPublicData() {
|
||||
const publicDataURL = 'https://woke.net/csv'
|
||||
const refreshInterval = 5 * 60 * 1000
|
||||
while (true) {
|
||||
const resp = await fetch(PUBLIC_DATA_URL)
|
||||
const resp = await fetch(publicDataURL)
|
||||
const text = await resp.text()
|
||||
const data = await csv().fromString(text)
|
||||
yield data.filter((d) => d.Link && d.Status === 'Live')
|
||||
sleep(PUBLIC_DATA_REFRESH_INTERVAL)
|
||||
yield filterLive(data)
|
||||
await sleep(refreshInterval)
|
||||
}
|
||||
}
|
||||
|
||||
export async function* pollSpreadsheetData(creds, sheetId, tabName) {
|
||||
const refreshInterval = 10 * 1000
|
||||
|
||||
const doc = new GoogleSpreadsheet(sheetId)
|
||||
await doc.useServiceAccountAuth(creds)
|
||||
await doc.loadInfo()
|
||||
const sheet = Object.values(doc.sheetsById).find((s) => s.title === tabName)
|
||||
await sheet.loadHeaderRow()
|
||||
|
||||
while (true) {
|
||||
let rows
|
||||
try {
|
||||
rows = await sheet.getRows()
|
||||
const data = rows.map((row) =>
|
||||
Object.fromEntries(zip(row._sheet.headerValues, row._rawData)),
|
||||
)
|
||||
yield filterLive(data)
|
||||
} catch (err) {
|
||||
console.warn('error fetching rows', err)
|
||||
}
|
||||
await sleep(refreshInterval)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user