-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (116 loc) · 3.73 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// https://blog.mousereeve.com/api-access-to-grimoire-org/
require('dotenv').config();
var request = require('sync-request');
var chalk = require('chalk');
var twitter = require('twitter');
exports.handler = function index() {
main();
}
/*
Description: main process
Pamameters: none
Returns: nothing
*/
function main() {
var excerpt;
while (excerpt === undefined) {
console.log("Searching library");
var grimoire = getRandomGrimoire();
excerpt = getRandomExcerpt(grimoire);
if (excerpt == undefined) {
console.log(chalk.yellow('Putting ' + grimoire.identifier + ' back on the shelf'));
}
}
excerpt = excerpt.props;
console.log("");
console.log(chalk.red("Reading from " + grimoire.identifier));
console.log("-----------------------------------------")
console.log(excerpt.identifier);
console.log('https://www.grimoire.org/' + excerpt.uid);
tweetExcerpt(excerpt);
}
/*
Description: cleans up excerpt content
Pamameters:
content to clean up
Returns: cleaned content
*/
function cleanContent(content)
{
content = content.replace("<pre>","").replace("</pre>","").replace("<PRE>","").replace("</PRE>","");
return content;
}
/*
Description: tweets an excerpt using credentials set in env
Pamameters:
excerpt: the excerpt to tweet
Returns: nothing
*/
function tweetExcerpt(excerpt) {
var client = new twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
var url = shortenUrl('https://www.grimoire.org/excerpt/' + excerpt.uid);
console.log(url);
var length = (excerpt.identifier + "\n" + "...\n" + url).length;
//console.log(length);
var remaining = 140 - length - 2;
var contents = cleanContent(excerpt.content).substring(0, remaining);
console.log((excerpt.identifier + "\n" + contents + "...\n" + url).length);
client.post('statuses/update', { status: excerpt.identifier + "\n" + contents + "...\n" + url }, function(error, tweet, response) {
if (error){
console.log(error);
throw error;
}
});
}
/*
Description: gets a random excerpt from a grimoire
Pamameters:
grimoire: the grimoire to get an excerpt from
Returns: an excerpt. Undefined if grimoire is empty (???)
*/
function getRandomExcerpt(grimoire) {
var res = request('GET', 'https://www.grimoire.org/api/v1/node/' + grimoire.uid + '/excerpt?limit=1&random=true');
var data = JSON.parse(res.getBody('utf8'));
var selectedExcerpt = data[rand(0, data.length - 1)];
return selectedExcerpt;
}
/*
Description: gets a random grimoire
Pamameters: none
Returns: random grimoire
*/
function getRandomGrimoire() {
//https://www.grimoire.org/api/v1/label/grimoire
var res = request('GET', 'https://www.grimoire.org/api/v1/label/grimoire?limit=1&random=true');
var data = JSON.parse(res.getBody('utf8'));
var selectedGrimoire = data[rand(0, data.length - 1)].props;
return selectedGrimoire;
}
/*
Description: get a random int
Pamameters:
min: minimum number (inclusive)
max: maximum number (exclusive)
Returns: a random number
*/
function rand(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
/*
Description: shorten a URL
Parameters:
url: url to shorten
Returns: bitly url
*/
function shortenUrl(url){
var res = request('GET', 'https://api-ssl.bitly.com/v3/shorten?access_token=' + process.env.BITLY_OAUTH + '&longUrl=' + encodeURI(url));
var data = JSON.parse(res.getBody('utf8'));
return data.data.url;
}