Last active
June 20, 2019 21:21
-
-
Save shrugs/9beb9e325bc47012a5d7e8cc1ce402dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AWS = require("aws-sdk"); | |
import { DateTime } from "luxon"; | |
const dynamodb = new AWS.DynamoDB(); | |
const kLiveChannelsTable = "live-channels"; | |
const kLiveChannelConstantPartitionKey = "constant"; | |
const kLiveChannelTTL = { minutes: 15 }; | |
const fetchLiveChannels = async ( | |
cursor?: string, | |
): Promise<{ cursor?: string; channels: LiveChannel[] }> => { | |
const client = await getExtensionClient(); | |
const result = await client.get(`live_activated_channels`, { | |
query: { cursor }, | |
}); | |
return result.body; | |
}; | |
export async function* getLiveChannels(): AsyncIterableIterator<LiveChannel[]> { | |
let cursor: string | undefined; | |
do { | |
const result = await fetchLiveChannels(cursor); | |
cursor = result.cursor; | |
yield result.channels; | |
} while (cursor); | |
} | |
const putLiveChannels = async (liveChannels: LiveChannel[]): Promise<void> => { | |
if (liveChannels.length === 0) { | |
return; | |
} | |
const ttl = DateTime.utc() | |
.plus(kLiveChannelTTL) | |
.toSeconds() | |
.toFixed(); | |
await dynamodb | |
.batchWriteItem({ | |
RequestItems: { | |
[kLiveChannelsTable]: liveChannels.map(le => ({ | |
PutRequest: { | |
Item: { | |
key: { S: kLiveChannelConstantPartitionKey }, | |
id: { S: le.id }, | |
username: { S: le.username }, | |
game: { S: le.game }, | |
title: { S: le.title }, | |
view_count: { N: le.view_count }, | |
ttl: { N: ttl }, | |
}, | |
}, | |
})), | |
}, | |
}) | |
.promise(); | |
}; | |
export const handler = main(async (event, context) => { | |
const liveChannels = getLiveChannels(); | |
for await (const batch of liveChannels) { | |
console.log( | |
`Putting ${batch.length} items: ${batch.map( | |
le => `${le.id}/${le.username}`, | |
)}`, | |
); | |
await putLiveChannels(batch); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment