Add ability to poll data from spreadsheet

This commit is contained in:
Max Goodhart
2020-06-16 16:36:09 -07:00
parent c405fe1339
commit c95472f4ad
4 changed files with 471 additions and 88 deletions

View File

@@ -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)
}
}

View File

@@ -1,7 +1,9 @@
import fs from 'fs'
import yargs from 'yargs'
import { app, BrowserWindow, BrowserView, ipcMain, shell } from 'electron'
import { interpret } from 'xstate'
import { pollPublicData } from './data'
import { pollPublicData, pollSpreadsheetData } from './data'
import viewStateMachine from './viewStateMachine'
import { boxesFromSpaceURLMap } from './geometry'
@@ -14,6 +16,23 @@ import {
} from '../constants'
async function main() {
const argv = yargs
.config('config', (configPath) => {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
})
.group(['gs-creds', 'gs-id', 'gs-tab'], 'Spreadsheet Configuration')
.option('gs-creds', {
describe: 'credentials file for Google Spreadsheet access',
implies: ['gs-id', 'gs-tab'],
})
.option('gs-id', {
describe: 'Google Spreadsheet id',
})
.option('gs-tab', {
describe: 'Google Spreadsheet tab name',
})
.help().argv
const mainWin = new BrowserWindow({
x: 0,
y: 0,
@@ -151,12 +170,25 @@ async function main() {
overlayView.webContents.openDevTools()
})
for await (const data of pollPublicData()) {
let dataGen
if (argv.gsCreds) {
dataGen = pollSpreadsheetData(argv.gsCreds, argv.gsId, argv.gsTab)
} else {
dataGen = pollPublicData()
}
for await (const data of dataGen) {
mainWin.webContents.send('stream-data', data)
overlayView.webContents.send('stream-data', data)
}
}
if (require.main === module) {
app.whenReady().then(main)
app
.whenReady()
.then(main)
.catch((err) => {
console.error(err.toString())
process.exit(1)
})
}