Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Args #2

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* www.openedcaptions.com routes captions from C-Span 1 channel to a socket end point.
* This script serves as an itnermediate server to buffer text from socket and expose it as REST API end point.
* www.openedcaptions.com routes captions from C-Span 1 channel to a socket end point.
* This script serves as an intermediate server to buffer text from socket and expose it as REST API end point.
* * that also support char offset. see README for more info.
* author: Dan Z @impronunciable
*/
Expand All @@ -10,6 +10,24 @@ const http = require('http')
const URL = require('url')
const s = require('underscore.string')
const parseCsv = require('csv-parse/lib/sync')
const argv = require('minimist')(process.argv.slice(2),
{alias: { h: 'help', t: 'transcript',
p: 'port', s: 'skip', f: 'flags'}});

const help = 'Usage: index.js [options]\n\n' +
' A intermediate node web server to cache CSPAN transcript feed\n\n' +
' Options:\n' +
' -h, --help output usage information\n' +
' -p, --port server port\n' +
' -t, --transcript relative path to the file to store transcript\n' +
' -s, --skip if present, skips initial load of the transcript file\n' +
' -f, --flags mode to apply to the transcript file; a: append, w: write';

if (argv.help) {
console.log(help);
process.exit(0);
}


// Load proper noun dictionary
const words = parseCsv(fs.readFileSync('words.csv'))
Expand All @@ -18,19 +36,22 @@ const words = parseCsv(fs.readFileSync('words.csv'))
var cache = []

// Setup a cache buster so our cache doesn't use all the memory
const ttl = 20 * 60 * 1000 // 20 mins -> microseconds
const cacheCheckInterval = 5 * 60 * 1000 // 5 mins -> microseconds
const ttl = 20 * 60 * 1000 // 20 mins -> miliseconds
const cacheCheckInterval = 5 * 60 * 1000 // 5 mins -> miliseconds
setInterval(cleanCache, cacheCheckInterval)

// Setup a transcription file, if desired
var txt = false;
if ( process.env.TRANSCRIPT_FILE ) {
const transcriptFile = process.env.TRANSCRIPT_FILE
if ( argv.transcript || process.env.TRANSCRIPT_FILE ) {
const transcriptFile = argv.transcript || process.env.TRANSCRIPT_FILE
if ( fs.existsSync(transcriptFile) ) {
cache.push({t: Date.now(), r: fs.readFileSync(transcriptFile)})
if (!argv.skip) {
cache.push({t: Date.now(), r: fs.readFileSync(transcriptFile)})
}
}

txt = fs.createWriteStream(transcriptFile, {flag: 'a'})
var flags = argv.flags || 'a'
txt = fs.createWriteStream(transcriptFile, {flags: flags})
}

const socket = io.connect('https://openedcaptions.com:443')
Expand All @@ -56,7 +77,7 @@ http.createServer((req, res) => {
captions: formatText(getWordsSince(timestamp))
}))
}
}).listen(process.env.PORT || 5000)
}).listen(argv.port || process.env.PORT || 5000)

function formatText(str) {
var ret = str.toLowerCase().replace("\r\n", ' ') // remove random line breaks
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"csv-parse": "^1.1.9",
"socket.io": "^1.7.2",
"minimist": "^1.2.0",
"underscore.string": "^3.3.4"
}
}