-
Notifications
You must be signed in to change notification settings - Fork 3
/
article.js
178 lines (159 loc) · 5.04 KB
/
article.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var fs = require('fs');
var giphy = new require("giphy")("dc6zaTOxFJmzC");
var Markov = require("./markov");
var quizCreator = require("./quiz");
var Templater = require("./templater");
var async = require("async");
var zip = require("lodash.zip");
var flatten = require("lodash.flatten");
var findPictures = require("./flickr.js");
/**
* Will find gifs from Giphy given search tags.
* allWords: [String] a list of all the keywords to search for seperately
* callback: () => () a callback
*
* This will also filter out the duplicates.
*
* callback := [{data: [...]}, ...]
* (look at the return type of giphy here: https://github.com/giphy/GiphyAPI)
*/
function findGifUrls(allWords, callback){
async.mapLimit(allWords, 5, function(str, cb) {
giphy.search({q:str}, function (err, response){
if (err) return cb(err);
// Filter for duplicates (simple filter using the URL of the gif)
response.data = response.data.filter(function(v, i) {
return response.data.indexOf(v) === i;
});
cb(null, response.data);
});
}, function(err, allData) {
if (err) return console.error("Giphy: ", err);
if(allData.length === 1) return callback(allData[0]);
var ret = allData = flatten(zip.apply(null, allData)).filter(function(v) {
return !!v;
});
callback(ret);
});
}
function assignAuthor(article, author){
article.username = author.username;
article.authorName = author.name;
article.profileUrl = "/users/" + article.username;
article.authorProfilePicture = author.authorProfilePicture;
article.authorTitle = author.authorTitle;
}
function stringToIntHash(str){
var hash = 0;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return (hash > 0)? hash : -hash;
}
/////////////////////////////////////
// Entry point to Article Creation //
/////////////////////////////////////
function createEntireArticle(author, templater, callback){
if(typeof author === "function") {
callback = author;
author = newAuthor();
}
templater.loadBuzzTitles();
var article = templater.generateName();
assignAuthor(article, author);
article.url = "/users/" + article.username + "/" + article.articleName;
article.responses = rand(10,600);
article.elements = [];
article.timestamp = Date.now();
var m = new Markov();
m.pretrainBuzzfeedLists();
m.pretrainWikipediaSubject(article, function() {
var searchWords = [article.subj];
if(article.extra.length > 0) {
searchWords = searchWords.concat(article.extra.map(function(v) {
return article.subj + ' ' + v;
})).concat(article.extra.map(function(v) {
return v;
}));
}
findGifUrls(searchWords, function(gifs) {
if(gifs.length > 0) article.previewUrl = gifs[0].images.original_still.url;
// TODO: find a smarter way to sort the gifs
// right now the order depends on the which get request arrives first
// when querying all the searchWords in parallel
shuffle(gifs);
var arr = [];
var i;
if(gifs.length < article.num) {
for (i = 0; i < gifs.length; i++) {
arr.push({
imageUrl: gifs[i].images.original.url,
body: m.generate(rand(rand(0, 2),5),10, 2),
title: m.generate(1,10, 3)
});
}
findPictures(searchWords, function(allPics) {
var diffPictures = allPics.slice(0, article.num - gifs.length);
if(!article.previewUrl) article.previewUrl = diffPictures[0];
for (i = 0; i < diffPictures.length; i++) {
arr.push({
imageUrl: diffPictures[i],
body: m.generate(rand(rand(rand(0, 2), 3),7),10, 5),
title: m.generate(1,10, 3)
});
}
shuffle(arr);
article.elements = arr;
callback(article);
});
} else {
for (i = 0; i < article.num; i++){
if (i < gifs.length){
arr.push({
imageUrl: gifs[i].images.original.url,
body: m.generate(rand(rand(0, 2),5),10, 2),
title: m.generate(1,10, 3)
});
} else break;
}
shuffle(arr);
article.elements = arr;
callback(article);
}
});
});
}
function identity(v) {
return v;
}
function contains(coll, el, f) {
return find(coll, el, f) !== null;
}
function find(coll, el, f) {
var max = coll.length;
for (var i = 0; i < max; ++i){
if(f(coll[i], el)) {
return coll[i];
}
}
return null;
}
function rand(min, max){
return Math.floor(Math.random() * (max-min))+min;
}
/**
* Shuffles a given given inplace.
* @param {[a']} coll array to be shuffled
* @return {[a']} shuffled array
*/
function shuffle(coll){
for(var j, x, i = coll.length; i; j = Math.floor(Math.random() * i), x = coll[--i], coll[i] = coll[j], coll[j] = x);
return coll;
}
module.exports = {
createEntireArticle: createEntireArticle,
findPictures: findPictures
};