-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
87 lines (65 loc) · 3.64 KB
/
main.js
File metadata and controls
87 lines (65 loc) · 3.64 KB
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
var goodreads = require('./goodreads');
var twitter = require('./twitter');
var LINQ = require('node-linq').LINQ;
var booksWithTheStoryOf = [];
var booksWithoutTheStoryOf = [];
var express = require("express");
var app = express();
app.listen(process.env.PORT);
app.set('port', process.env.PORT || 3000);
goodreads.setApiKey("YOUR_API_KEY");
goodreads.setApiSecret("YOUR_API_SECRET");
var GetBookTitles = function(query){
var bookObjects = [];
aggregateBookObjects(0, 5, function(results) { postNewMemoir(results) });
function aggregateBookObjects(min, max, next){
if(min < max){
// There are approximagely 3479 pages of data from this search on goodreads currently
var randomPage = Math.floor(Math.random() * 3475);
goodreads.searchForBooks(query, randomPage, "title", function(bookResults){
var resultObjects = bookResults.GoodreadsResponse.search[0].results[0].work;
bookObjects = bookObjects.concat(resultObjects);
//console.log("book objects count: " + bookObjects.length);
aggregateBookObjects(min + 1, max, next);
});
}
else{
next(bookObjects);
}
}
};
var postNewMemoir = function(bookObjects, query){
booksWithTheStoryOf = new LINQ(bookObjects)
.Where(function(item) { return typeof item !== 'undefined'; } )
.Where(function(item) { return item.best_book[0].title[0].toLowerCase().indexOf("story of") != -1; })
.Where(function(item) { return item.best_book[0].title[0].indexOf(":") == -1 })
.SelectMany( function(item) { return item.best_book[0].title[0]; })
.SelectMany(function(item) { return item.substring(item.toLowerCase().indexOf("the story of"), item.length); })
.ToArray();
booksWithoutTheStoryOf = new LINQ(bookObjects)
.Where(function(item) { return typeof item !== 'undefined'; } )
.Where(function(item) { return item.best_book[0].title[0].toLowerCase().indexOf("the story of") == -1 })
.Where(function(item) { return item.best_book[0].title[0].indexOf(":") == -1 })
.SelectMany( function(item) { return item.best_book[0].title[0]; })
.ToArray();
//console.log("Array length 1: " + booksWithTheStoryOf.length);
//console.log("Array length 2: " + booksWithoutTheStoryOf.length);
var withoutStoryOfString = "";
var storyOfString = "";
var memoirString = "";
while(memoirString.length == 0 || memoirString.length > 140){
withoutStoryOfString = booksWithoutTheStoryOf[Math.floor(Math.random() * booksWithoutTheStoryOf.length)];
storyOfString = booksWithTheStoryOf[Math.floor(Math.random() * booksWithTheStoryOf.length)];
memoirString = MakeMemoirTitle(withoutStoryOfString, storyOfString);
}
console.log(memoirString);
twitter.postToTwitter(MakeMemoirTitle(withoutStoryOfString, storyOfString));
};
var MakeMemoirTitle = function(blank, theStoryOf){
return (blank + ": " + theStoryOf);
};
// Try to retweet something as soon as we run the program...
//GetBookTitles("The Story of");
// ...and then every hour after that. Time here is in milliseconds, so
// 1000 ms = 1 second, 1 sec * 60 = 1 min, 1 min * 15 = 0.25 hour --> 1000 * 60 * 15
setInterval(function(){ GetBookTitles("The Story of")}, 1000 * 60 * 0.5);