-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·114 lines (93 loc) · 2.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env node
'use strict';
/*
* vid2tweet utility
* processes vimeo data into tweets
*
* relies on the following:
* - https://www.npmjs.com/package/commander
*
* 2018-12
* mamund
*/
var program = require('commander');
var fs = require('fs');
var settings = {};
settings.template = "";
settings.list = "";
settings.tweetList = [];
settings.tweetTemplate = "tweet-template.txt";
settings.writeFile = "tweet-list.txt";
// top-level routine
program
.arguments('<file>')
.action(function(file){work(file)})
.parse(process.argv);
// the work
function work(file) {
if(readFile(settings.tweetTemplate, "template")) {
if(readFile(file, "list")) {
genTweets();
}
}
}
// generate tweets
function genTweets() {
var list;
list = parseList();
list.forEach(function(item) {
settings.tweetList.push(makeTweet(item));
});
writeFile(settings.writeFile,settings.tweetList);
}
// make a tweet
function makeTweet(item) {
var output = "";
output = settings.template;
output = output.replace("{speaker}", item.title);
output = output.replace("{title}", item.speaker);
output = output.replace("{link}", item.link);
output = output.replace("{handle}", item.handle);
return output;
}
// parse the input file
function parseList() {
var rtn = [];
var items = [];
var tweet = {};
items = settings.list.split("\n");
items.forEach(function(item) {
if(item.length!==0) {
tweet = {};
tweet.title = item.split("::")[0];
tweet.speaker = item.split("::")[1];
tweet.link = item.split("::")[2];
tweet.handle = item.split("::")[3];
rtn.push(tweet);
}
});
return rtn;
}
// write string file
function writeFile(name,list) {
var output = "";
list.forEach(function(item) {
output = output + item + "\n";
});
fs.writeFileSync(name,output);
return true;
}
// read string file
function readFile(file, name) {
var rtn = "";
if(fs.existsSync(file)) {
settings[name] = fs.readFileSync(file,'utf8');
rtn = true;
}
else {
console.log("File not found or invalid: "+file);
rtn = false;
}
return rtn;
}
// EOF