Showing
8 changed files
with
311 additions
and
183 deletions
1 | -// Tell a Chuck Norris Joke | 1 | +const MongoClient = require('mongodb').MongoClient; |
2 | -function chuckJoke(){ | 2 | +const dboperation = require('../operations'); |
3 | - axios.get('http://api.icndb.com/jokes/random/') | 3 | +const dbname = 'jokeapi'; |
4 | - .then(res =>{ | 4 | +const collec = 'jokes'; |
5 | - const joke = res.data.value.joke; | 5 | +const url = 'mongodb://localhost:27017/'; |
6 | - | 6 | + |
7 | + | ||
8 | +exports.randomJoke= ()=>{ | ||
9 | + MongoClient.connect('mongodb://localhost:27017', function (err, client){ | ||
10 | + if (err) throw err; | ||
11 | + var db = client.db('jokeapi'); | ||
12 | + | ||
13 | + json_max = 376; | ||
14 | + function getRandomInt() { | ||
15 | + min = Math.ceil(1); | ||
16 | + max = Math.floor(376); | ||
17 | + return Math.floor(Math.random() * (max - min + 1)) + min; | ||
18 | + } | ||
19 | + random = getRandomInt(); | ||
20 | + result = db.collection('jokes').findOne({id: random}); | ||
21 | + | ||
22 | + user = result; | ||
23 | + user.then(function(total){ | ||
24 | + question = total.setup; | ||
25 | + joke = total.punchline; | ||
7 | const face = { | 26 | const face = { |
8 | icon_emoji: ':laughing:' | 27 | icon_emoji: ':laughing:' |
9 | }; | 28 | }; |
10 | 29 | ||
11 | - bot.postMessageToChannel('everyone', `Chuck Norris: ${joke}`,face); | 30 | + bot.postMessageToChannel('everyone', question, joke, face); |
12 | - bot.postMessageToChannel('full-stack-web', `Yo mama: ${joke}`,face); | 31 | + bot.postMessageToChannel('full-stack-web', question, joke, face); |
13 | - bot.postMessageToChannel('bot_test', `Yo mama: ${joke}`,face); | 32 | + bot.postMessageToChannel('bot_test', question, joke, face); |
14 | - }); | 33 | + }) |
34 | + client.close(); | ||
35 | + }) | ||
15 | } | 36 | } |
16 | 37 | ||
17 | -// Tell a yomama Joke | 38 | + function generalJoke(){ |
18 | -function yoMamaJoke(){ | 39 | + dboperation.getdata(dbname, collec, "general") |
19 | - axios.get('http://api.yomomma.info/') | 40 | + .then((data) =>{ |
20 | - .then(res =>{ | 41 | + var query = { state: 'OK' }; |
21 | - const joke = res.data.joke; | 42 | + var n = data.count(query); |
43 | + var r = Math.floor(Math.random() * n); | ||
44 | + var randomElement = data.find(query).limit(1).skip(r); | ||
45 | + var question = randomElement.setup; | ||
46 | + var joke = randomElement.punchline; | ||
47 | + const face = { | ||
48 | + icon_emoji: ':laughing:' | ||
49 | + }; | ||
50 | + | ||
51 | + bot.postMessageToChannel('everyone', question, joke, face); | ||
52 | + bot.postMessageToChannel('full-stack-web', question, joke, face); | ||
53 | + bot.postMessageToChannel('bot_test', question, joke, face); | ||
54 | + }); | ||
55 | + }; | ||
22 | 56 | ||
23 | - const face = { | 57 | + // Tell a yomama Joke |
24 | - icon_emoji: ':laughing:' | 58 | + function yomamaJoke(){ |
59 | + axios.get('http://api.yomomma.info/') | ||
60 | + .then(res =>{ | ||
61 | + const joke = res.data.joke; | ||
62 | + | ||
63 | + const face = { | ||
64 | + icon_emoji: ':laughing:' | ||
65 | + }; | ||
66 | + | ||
67 | + bot.postMessageToChannel('everyone', `Yo mama: ${joke}`,face); | ||
68 | + bot.postMessageToChannel('full-stack-web', `Yo mama: ${joke}`,face); | ||
69 | + bot.postMessageToChannel('bot_test', `Yo mama: ${joke}`,face); | ||
70 | + | ||
71 | + }); | ||
25 | }; | 72 | }; |
73 | + //Tell random joke | ||
74 | + function runhelp(){ | ||
75 | + const face = { | ||
76 | + icon_emoji: ':question:' | ||
77 | + }; | ||
26 | 78 | ||
27 | - bot.postMessageToChannel('everyone', `Yo mama: ${joke}`,face); | ||
28 | - bot.postMessageToChannel('full-stack-web', `Yo mama: ${joke}`,face); | ||
29 | - bot.postMessageToChannel('bot_test', `Yo mama: ${joke}`,face); | ||
30 | - | ||
31 | - }); | ||
32 | -} | ||
33 | -//Tell random joke | ||
34 | -function randomJoke(){ | ||
35 | - const rand = Math.floor(Math.random() * 2) +1; | ||
36 | - if(rand ===1){ | ||
37 | - chuckJoke(); | ||
38 | - } | ||
39 | - else if(rand === 2){ | ||
40 | - yoMamaJoke(); | ||
41 | - } | ||
42 | -} | ||
43 | -function runHelp(){ | ||
44 | - const face = { | ||
45 | - icon_emoji: ':question:' | ||
46 | - }; | ||
47 | - | ||
48 | - bot.postMessageToChannel('everyone', "Type @joker and write a joke that you would like\n ex- @joker random",face); | ||
49 | - bot.postMessageToChannel('full-stack-web', "Type @joker and write a joke that you would like\n ex- @joker random",face); | ||
50 | -} | ||
51 | - | ||
52 | -module.exports = route; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
79 | + bot.postMessageToChannel('everyone', "Type @joker and write a joke that you would like\n ex- @joker random",face); | ||
80 | + bot.postMessageToChannel('full-stack-web', "Type @joker and write a joke that you would like\n ex- @joker random",face); | ||
81 | + }; | ... | ... |
... | @@ -2,8 +2,12 @@ const SlackBot = require('slackbots'); | ... | @@ -2,8 +2,12 @@ const SlackBot = require('slackbots'); |
2 | const axios = require('axios'); | 2 | const axios = require('axios'); |
3 | const route = require('./Routers/route'); | 3 | const route = require('./Routers/route'); |
4 | 4 | ||
5 | +const MongoClient = require('mongodb').MongoClient; | ||
6 | + | ||
7 | +const url = 'mongodb://localhost:27017/'; | ||
8 | + | ||
5 | const bot = new SlackBot({ | 9 | const bot = new SlackBot({ |
6 | - token : "xoxb-582582124755-587875604934-rRhFVlXlB0StEMnlrmsQlcac", | 10 | + token : "xoxb-582582124755-587875604934-EWnGacmzNvfVQk2XvhteoIzw", |
7 | name : "Joker" | 11 | name : "Joker" |
8 | }); | 12 | }); |
9 | 13 | ||
... | @@ -34,33 +38,56 @@ bot.on('message', (data) => { | ... | @@ -34,33 +38,56 @@ bot.on('message', (data) => { |
34 | 38 | ||
35 | // Responding to Data | 39 | // Responding to Data |
36 | function handleMessage(message){ | 40 | function handleMessage(message){ |
37 | - if(message.includes('chucknorris')){ | 41 | + |
38 | - route.chuckJoke() | 42 | + if(message.includes(' yomama')){ |
39 | - .then((err, data)=>{ | 43 | + route.startdb.yomamaJoke(); |
40 | - if(err)throw err; | ||
41 | - console.log("ChuckJoke function activated"); | ||
42 | - }); | ||
43 | } | 44 | } |
44 | - else if(message.includes(' yomama')){ | 45 | + else if(message.includes(' general')){ |
45 | - route.yoMamaJoke() | 46 | + route.randomJoke(); |
46 | - .then((err, data)=>{ | ||
47 | - if(err)throw err; | ||
48 | - console.log("yoMamaJoke function activated"); | ||
49 | -}); | ||
50 | } | 47 | } |
48 | + | ||
51 | else if(message.includes(' random')){ | 49 | else if(message.includes(' random')){ |
52 | - route.randomJoke() | 50 | + randomJoke(); |
53 | - .then((err, data)=>{ | 51 | + } |
54 | - if(err)throw err; | 52 | + |
55 | - console.log("randomJoke function activated"); | 53 | + else if(message.includes(' programming')){ |
56 | - }); | 54 | + route.startdb() |
55 | + | ||
56 | + } | ||
57 | + else if(message.includes(' help')){ | ||
58 | + route.startdb.runHelp(); | ||
59 | + } | ||
57 | } | 60 | } |
58 | 61 | ||
59 | - else if(message.includes(' help')){ | 62 | +randomJoke= ()=>{ |
60 | - route.runHelp() | 63 | + MongoClient.connect('mongodb://localhost:27017', function (err, client){ |
61 | - .then((err, data)=>{ | 64 | + if (err) throw err; |
62 | - if(err)throw err; | 65 | + var db = client.db('jokeapi'); |
63 | - console.log(" function activated"); | 66 | + |
64 | - }); | 67 | + json_max = 376; |
68 | + function getRandomInt() { | ||
69 | + min = Math.ceil(1); | ||
70 | + max = Math.floor(376); | ||
71 | + return Math.floor(Math.random() * (max - min + 1)) + min; | ||
65 | } | 72 | } |
66 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
73 | + random = getRandomInt(); | ||
74 | + result = db.collection('jokes').findOne({id: random}); | ||
75 | + | ||
76 | + user = result; | ||
77 | + user.then(function(total){ | ||
78 | + question = total.setup; | ||
79 | + joke = total.punchline; | ||
80 | + const face = { | ||
81 | + icon_emoji: ':laughing:' | ||
82 | + }; | ||
83 | + | ||
84 | + bot.postMessageToChannel('everyone', joke, face); | ||
85 | + bot.postMessageToChannel('full-stack-web', joke, face); | ||
86 | + bot.postMessageToChannel('bot_test', joke, face); | ||
87 | + bot.postMessageToChannel('everyone', question, face); | ||
88 | + bot.postMessageToChannel('full-stack-web', question, joke, face); | ||
89 | + bot.postMessageToChannel('bot_test', question, face); | ||
90 | + }) | ||
91 | + client.close(); | ||
92 | + }) | ||
93 | +} | ... | ... |
1 | const MongoClient = require('mongodb').MongoClient; | 1 | const MongoClient = require('mongodb').MongoClient; |
2 | -const assert = require('assert'); | ||
3 | const dboperation = require('./operations'); | 2 | const dboperation = require('./operations'); |
4 | const jokedoc = require('./jokes/jokes.json'); | 3 | const jokedoc = require('./jokes/jokes.json'); |
5 | -const url = 'mongodb://localhost:27017/'; | ||
6 | const dbname = 'jokeapi'; | 4 | const dbname = 'jokeapi'; |
7 | const collec = 'jokes'; | 5 | const collec = 'jokes'; |
8 | 6 | ||
9 | var url = "mongodb://localhost:27017/jokeapi"; | 7 | var url = "mongodb://localhost:27017/jokeapi"; |
10 | 8 | ||
11 | -// Code for Creating database | 9 | +// Create database |
12 | // MongoClient.connect(url, function(err, db) { | 10 | // MongoClient.connect(url, function(err, db) { |
13 | // if (err) throw err; | 11 | // if (err) throw err; |
14 | // console.log("Database created!"); | 12 | // console.log("Database created!"); |
15 | // db.close(); | 13 | // db.close(); |
16 | // }); | 14 | // }); |
17 | 15 | ||
18 | -// Code for Creating a Collection | ||
19 | // MongoClient.connect(url, function(err, db) { | 16 | // MongoClient.connect(url, function(err, db) { |
20 | // if (err) throw err; | 17 | // if (err) throw err; |
21 | // var dbo = db.db("jokeapi"); | 18 | // var dbo = db.db("jokeapi"); |
... | @@ -26,21 +23,50 @@ var url = "mongodb://localhost:27017/jokeapi"; | ... | @@ -26,21 +23,50 @@ var url = "mongodb://localhost:27017/jokeapi"; |
26 | // }); | 23 | // }); |
27 | // }); | 24 | // }); |
28 | 25 | ||
29 | -// | ||
30 | -MongoClient.connect(url).then((client) => { | ||
31 | 26 | ||
32 | - console.log('Connected correctly to server'); | 27 | +// Storing data jokes.json data into mongodb |
33 | -}).catch((err)=> console.log(err)); | 28 | +// MongoClient.connect(url).then((client) => { |
34 | - /* | ||
35 | - const db = client.db(dbname); | ||
36 | 29 | ||
37 | - dboperation.insertDocument(db, jokedoc, collec) | 30 | +// console.log('Connected correctly to server'); |
38 | - .then((result) => { | 31 | + |
39 | - console.log("Inserted Document:\n", result.ops); | 32 | + |
33 | +// const db = client.db(dbname); | ||
40 | 34 | ||
41 | - return dboperation.findDocuments(db, collec); | 35 | +// dboperation.insertDocument(db, jokedoc, collec) |
42 | - }) | 36 | +// .then((result) => { |
43 | - .catch((err) => console.log(err)); | 37 | +// console.log("Inserted Document:\n", result.ops); |
44 | 38 | ||
45 | -}) | ||
46 | -*/ | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
39 | +// return dboperation.findDocuments(db, collec); | ||
40 | +// }) | ||
41 | +// .catch((err) => console.log(err)); | ||
42 | + | ||
43 | +// }) | ||
44 | +// .catch((err) => console.log(err)); | ||
45 | + | ||
46 | +// //will encapsulate all that database operations | ||
47 | +// const assert = require('assert'); | ||
48 | + | ||
49 | +// exports.insertDocument = (db, document, collection, callback) =>{ | ||
50 | +// const coll = db.collection(collection); | ||
51 | +// return coll.insert(document); | ||
52 | +// }; | ||
53 | + | ||
54 | +// exports.findDocuments = (db, collection, input,callback)=>{ | ||
55 | +// const coll = db.collection(collection); | ||
56 | +// return coll.find({"type": input}).toArray(); | ||
57 | +// } | ||
58 | + | ||
59 | +// exports.removeDocuments = (db, document , collection, callback)=>{ | ||
60 | +// const coll = db.collection(collection); | ||
61 | +// return coll.deleteone(document); | ||
62 | +// } | ||
63 | + | ||
64 | +// exports.updateDocuments = (db, document , update ,collection, callback)=>{ | ||
65 | +// const coll = db.collection(collection); | ||
66 | +// return coll.updateOne(document, {$set: update}, null); | ||
67 | +// } | ||
68 | + | ||
69 | +// exports.getdata = (db, document, collection, input, callback =>{ | ||
70 | +// result = findDocuments(db,collection); | ||
71 | +// return result; | ||
72 | +// }) | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -6,9 +6,9 @@ exports.insertDocument = (db, document, collection, callback) =>{ | ... | @@ -6,9 +6,9 @@ exports.insertDocument = (db, document, collection, callback) =>{ |
6 | return coll.insert(document); | 6 | return coll.insert(document); |
7 | }; | 7 | }; |
8 | 8 | ||
9 | -exports.findDocuments = (db, collection, callback)=>{ | 9 | +exports.findDocuments = (db, collection, input)=>{ |
10 | const coll = db.collection(collection); | 10 | const coll = db.collection(collection); |
11 | - return coll.find({}).toArray(); | 11 | + return coll.find({"type": input}).toArray(); |
12 | } | 12 | } |
13 | 13 | ||
14 | exports.removeDocuments = (db, document , collection, callback)=>{ | 14 | exports.removeDocuments = (db, document , collection, callback)=>{ |
... | @@ -19,4 +19,11 @@ exports.removeDocuments = (db, document , collection, callback)=>{ | ... | @@ -19,4 +19,11 @@ exports.removeDocuments = (db, document , collection, callback)=>{ |
19 | exports.updateDocuments = (db, document , update ,collection, callback)=>{ | 19 | exports.updateDocuments = (db, document , update ,collection, callback)=>{ |
20 | const coll = db.collection(collection); | 20 | const coll = db.collection(collection); |
21 | return coll.updateOne(document, {$set: update}, null); | 21 | return coll.updateOne(document, {$set: update}, null); |
22 | +} | ||
23 | + | ||
24 | +exports.getdata = (db, collection, input, callback) =>{ | ||
25 | + const coll = db.collection(collection); | ||
26 | + result= coll.find({"type": input}); | ||
27 | + console.log(result); | ||
28 | + return result; | ||
22 | } | 29 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -60,10 +60,10 @@ | ... | @@ -60,10 +60,10 @@ |
60 | "tweetnacl": "^0.14.3" | 60 | "tweetnacl": "^0.14.3" |
61 | } | 61 | } |
62 | }, | 62 | }, |
63 | - "capture-stack-trace": { | 63 | + "bson": { |
64 | - "version": "1.0.1", | 64 | + "version": "1.1.1", |
65 | - "resolved": "https://registry.npmjs.org/capture-stack-trace/-/capture-stack-trace-1.0.1.tgz", | 65 | + "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.1.tgz", |
66 | - "integrity": "sha512-mYQLZnx5Qt1JgB1WEiMCf2647plpGeQ2NMR/5L0HNZzGQo4fuSPnK+wjfPnKZV0aiJDgzmWqqkV/g7JD+DW0qw==" | 66 | + "integrity": "sha512-jCGVYLoYMHDkOsbwJZBCqwMHyH4c+wzgI9hG7Z6SZJRXWr+x58pdIbm2i9a/jFGCkRJqRUr8eoI7lDWa0hTkxg==" |
67 | }, | 67 | }, |
68 | "caseless": { | 68 | "caseless": { |
69 | "version": "0.12.0", | 69 | "version": "0.12.0", |
... | @@ -83,14 +83,6 @@ | ... | @@ -83,14 +83,6 @@ |
83 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", | 83 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", |
84 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" | 84 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" |
85 | }, | 85 | }, |
86 | - "create-error-class": { | ||
87 | - "version": "3.0.2", | ||
88 | - "resolved": "https://registry.npmjs.org/create-error-class/-/create-error-class-3.0.2.tgz", | ||
89 | - "integrity": "sha1-Br56vvlHo/FKMP1hBnHUAbyot7Y=", | ||
90 | - "requires": { | ||
91 | - "capture-stack-trace": "^1.0.0" | ||
92 | - } | ||
93 | - }, | ||
94 | "dashdash": { | 86 | "dashdash": { |
95 | "version": "1.14.1", | 87 | "version": "1.14.1", |
96 | "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", | 88 | "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", |
... | @@ -112,11 +104,6 @@ | ... | @@ -112,11 +104,6 @@ |
112 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", | 104 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", |
113 | "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" | 105 | "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" |
114 | }, | 106 | }, |
115 | - "duplexer3": { | ||
116 | - "version": "0.1.4", | ||
117 | - "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", | ||
118 | - "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=" | ||
119 | - }, | ||
120 | "ecc-jsbn": { | 107 | "ecc-jsbn": { |
121 | "version": "0.1.2", | 108 | "version": "0.1.2", |
122 | "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", | 109 | "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", |
... | @@ -169,16 +156,6 @@ | ... | @@ -169,16 +156,6 @@ |
169 | "mime-types": "^2.1.12" | 156 | "mime-types": "^2.1.12" |
170 | } | 157 | } |
171 | }, | 158 | }, |
172 | - "free-google-image-search": { | ||
173 | - "version": "1.0.0", | ||
174 | - "resolved": "https://registry.npmjs.org/free-google-image-search/-/free-google-image-search-1.0.0.tgz", | ||
175 | - "integrity": "sha1-emJ4vo7cZG7ZanEiUdsyZxckyBU=" | ||
176 | - }, | ||
177 | - "get-stream": { | ||
178 | - "version": "3.0.0", | ||
179 | - "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", | ||
180 | - "integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=" | ||
181 | - }, | ||
182 | "getpass": { | 159 | "getpass": { |
183 | "version": "0.1.7", | 160 | "version": "0.1.7", |
184 | "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", | 161 | "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", |
... | @@ -187,32 +164,6 @@ | ... | @@ -187,32 +164,6 @@ |
187 | "assert-plus": "^1.0.0" | 164 | "assert-plus": "^1.0.0" |
188 | } | 165 | } |
189 | }, | 166 | }, |
190 | - "google-images": { | ||
191 | - "version": "2.1.0", | ||
192 | - "resolved": "https://registry.npmjs.org/google-images/-/google-images-2.1.0.tgz", | ||
193 | - "integrity": "sha1-EnSuQEgll4Jk06T48tUmuI0L8Cw=", | ||
194 | - "requires": { | ||
195 | - "got": "^6.7.1" | ||
196 | - } | ||
197 | - }, | ||
198 | - "got": { | ||
199 | - "version": "6.7.1", | ||
200 | - "resolved": "https://registry.npmjs.org/got/-/got-6.7.1.tgz", | ||
201 | - "integrity": "sha1-JAzQV4WpoY5WHcG0S0HHY+8ejbA=", | ||
202 | - "requires": { | ||
203 | - "create-error-class": "^3.0.0", | ||
204 | - "duplexer3": "^0.1.4", | ||
205 | - "get-stream": "^3.0.0", | ||
206 | - "is-redirect": "^1.0.0", | ||
207 | - "is-retry-allowed": "^1.0.0", | ||
208 | - "is-stream": "^1.0.0", | ||
209 | - "lowercase-keys": "^1.0.0", | ||
210 | - "safe-buffer": "^5.0.1", | ||
211 | - "timed-out": "^4.0.0", | ||
212 | - "unzip-response": "^2.0.1", | ||
213 | - "url-parse-lax": "^1.0.0" | ||
214 | - } | ||
215 | - }, | ||
216 | "har-schema": { | 167 | "har-schema": { |
217 | "version": "2.0.0", | 168 | "version": "2.0.0", |
218 | "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", | 169 | "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", |
... | @@ -242,21 +193,6 @@ | ... | @@ -242,21 +193,6 @@ |
242 | "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", | 193 | "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", |
243 | "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" | 194 | "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" |
244 | }, | 195 | }, |
245 | - "is-redirect": { | ||
246 | - "version": "1.0.0", | ||
247 | - "resolved": "https://registry.npmjs.org/is-redirect/-/is-redirect-1.0.0.tgz", | ||
248 | - "integrity": "sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ=" | ||
249 | - }, | ||
250 | - "is-retry-allowed": { | ||
251 | - "version": "1.1.0", | ||
252 | - "resolved": "https://registry.npmjs.org/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz", | ||
253 | - "integrity": "sha1-EaBgVotnM5REAz0BJaYaINVk+zQ=" | ||
254 | - }, | ||
255 | - "is-stream": { | ||
256 | - "version": "1.1.0", | ||
257 | - "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", | ||
258 | - "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" | ||
259 | - }, | ||
260 | "is-typedarray": { | 196 | "is-typedarray": { |
261 | "version": "1.0.0", | 197 | "version": "1.0.0", |
262 | "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", | 198 | "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", |
... | @@ -303,10 +239,11 @@ | ... | @@ -303,10 +239,11 @@ |
303 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", | 239 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", |
304 | "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" | 240 | "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" |
305 | }, | 241 | }, |
306 | - "lowercase-keys": { | 242 | + "memory-pager": { |
307 | - "version": "1.0.1", | 243 | + "version": "1.5.0", |
308 | - "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", | 244 | + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", |
309 | - "integrity": "sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==" | 245 | + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", |
246 | + "optional": true | ||
310 | }, | 247 | }, |
311 | "mime-db": { | 248 | "mime-db": { |
312 | "version": "1.38.0", | 249 | "version": "1.38.0", |
... | @@ -321,6 +258,26 @@ | ... | @@ -321,6 +258,26 @@ |
321 | "mime-db": "~1.38.0" | 258 | "mime-db": "~1.38.0" |
322 | } | 259 | } |
323 | }, | 260 | }, |
261 | + "mongodb": { | ||
262 | + "version": "3.2.6", | ||
263 | + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.2.6.tgz", | ||
264 | + "integrity": "sha512-qnHc4tjEkHKemuzBq9R7ycYnhFE0Dlpt6+n6suoZp2DcDdqviQ+teloJU24fsOw/PLmr75yGk4mRx/YabjDQEQ==", | ||
265 | + "requires": { | ||
266 | + "mongodb-core": "3.2.6", | ||
267 | + "safe-buffer": "^5.1.2" | ||
268 | + } | ||
269 | + }, | ||
270 | + "mongodb-core": { | ||
271 | + "version": "3.2.6", | ||
272 | + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.2.6.tgz", | ||
273 | + "integrity": "sha512-i+XRVjur9D0ywGF7cFebOUnALnbvMHajdNhhl3TQuopW6QDE655G8CpPeERbqSqfa3rOKEUo08lENDIiBIuAvQ==", | ||
274 | + "requires": { | ||
275 | + "bson": "^1.1.1", | ||
276 | + "require_optional": "^1.0.1", | ||
277 | + "safe-buffer": "^5.1.2", | ||
278 | + "saslprep": "^1.0.0" | ||
279 | + } | ||
280 | + }, | ||
324 | "ms": { | 281 | "ms": { |
325 | "version": "2.1.1", | 282 | "version": "2.1.1", |
326 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", | 283 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", |
... | @@ -341,11 +298,6 @@ | ... | @@ -341,11 +298,6 @@ |
341 | "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", | 298 | "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", |
342 | "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" | 299 | "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" |
343 | }, | 300 | }, |
344 | - "prepend-http": { | ||
345 | - "version": "1.0.4", | ||
346 | - "resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz", | ||
347 | - "integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw=" | ||
348 | - }, | ||
349 | "psl": { | 301 | "psl": { |
350 | "version": "1.1.31", | 302 | "version": "1.1.31", |
351 | "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.31.tgz", | 303 | "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.31.tgz", |
... | @@ -395,6 +347,20 @@ | ... | @@ -395,6 +347,20 @@ |
395 | } | 347 | } |
396 | } | 348 | } |
397 | }, | 349 | }, |
350 | + "require_optional": { | ||
351 | + "version": "1.0.1", | ||
352 | + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", | ||
353 | + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", | ||
354 | + "requires": { | ||
355 | + "resolve-from": "^2.0.0", | ||
356 | + "semver": "^5.1.0" | ||
357 | + } | ||
358 | + }, | ||
359 | + "resolve-from": { | ||
360 | + "version": "2.0.0", | ||
361 | + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", | ||
362 | + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" | ||
363 | + }, | ||
398 | "safe-buffer": { | 364 | "safe-buffer": { |
399 | "version": "5.1.2", | 365 | "version": "5.1.2", |
400 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", | 366 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", |
... | @@ -405,6 +371,20 @@ | ... | @@ -405,6 +371,20 @@ |
405 | "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", | 371 | "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", |
406 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" | 372 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" |
407 | }, | 373 | }, |
374 | + "saslprep": { | ||
375 | + "version": "1.0.3", | ||
376 | + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", | ||
377 | + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", | ||
378 | + "optional": true, | ||
379 | + "requires": { | ||
380 | + "sparse-bitfield": "^3.0.3" | ||
381 | + } | ||
382 | + }, | ||
383 | + "semver": { | ||
384 | + "version": "5.7.0", | ||
385 | + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", | ||
386 | + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==" | ||
387 | + }, | ||
408 | "slackbots": { | 388 | "slackbots": { |
409 | "version": "1.2.0", | 389 | "version": "1.2.0", |
410 | "resolved": "https://registry.npmjs.org/slackbots/-/slackbots-1.2.0.tgz", | 390 | "resolved": "https://registry.npmjs.org/slackbots/-/slackbots-1.2.0.tgz", |
... | @@ -418,6 +398,15 @@ | ... | @@ -418,6 +398,15 @@ |
418 | "ws-heartbeat": "^1.0.4" | 398 | "ws-heartbeat": "^1.0.4" |
419 | } | 399 | } |
420 | }, | 400 | }, |
401 | + "sparse-bitfield": { | ||
402 | + "version": "3.0.3", | ||
403 | + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", | ||
404 | + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", | ||
405 | + "optional": true, | ||
406 | + "requires": { | ||
407 | + "memory-pager": "^1.0.2" | ||
408 | + } | ||
409 | + }, | ||
421 | "sshpk": { | 410 | "sshpk": { |
422 | "version": "1.16.1", | 411 | "version": "1.16.1", |
423 | "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", | 412 | "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", |
... | @@ -434,11 +423,6 @@ | ... | @@ -434,11 +423,6 @@ |
434 | "tweetnacl": "~0.14.0" | 423 | "tweetnacl": "~0.14.0" |
435 | } | 424 | } |
436 | }, | 425 | }, |
437 | - "timed-out": { | ||
438 | - "version": "4.0.1", | ||
439 | - "resolved": "https://registry.npmjs.org/timed-out/-/timed-out-4.0.1.tgz", | ||
440 | - "integrity": "sha1-8y6srFoXW+ol1/q1Zas+2HQe9W8=" | ||
441 | - }, | ||
442 | "tough-cookie": { | 426 | "tough-cookie": { |
443 | "version": "2.4.3", | 427 | "version": "2.4.3", |
444 | "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz", | 428 | "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz", |
... | @@ -473,11 +457,6 @@ | ... | @@ -473,11 +457,6 @@ |
473 | "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz", | 457 | "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz", |
474 | "integrity": "sha1-rOEWq1V80Zc4ak6I9GhTeMiy5Po=" | 458 | "integrity": "sha1-rOEWq1V80Zc4ak6I9GhTeMiy5Po=" |
475 | }, | 459 | }, |
476 | - "unzip-response": { | ||
477 | - "version": "2.0.1", | ||
478 | - "resolved": "https://registry.npmjs.org/unzip-response/-/unzip-response-2.0.1.tgz", | ||
479 | - "integrity": "sha1-0vD3N9FrBhXnKmk17QQhRXLVb5c=" | ||
480 | - }, | ||
481 | "uri-js": { | 460 | "uri-js": { |
482 | "version": "4.2.2", | 461 | "version": "4.2.2", |
483 | "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", | 462 | "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", |
... | @@ -486,14 +465,6 @@ | ... | @@ -486,14 +465,6 @@ |
486 | "punycode": "^2.1.0" | 465 | "punycode": "^2.1.0" |
487 | } | 466 | } |
488 | }, | 467 | }, |
489 | - "url-parse-lax": { | ||
490 | - "version": "1.0.0", | ||
491 | - "resolved": "https://registry.npmjs.org/url-parse-lax/-/url-parse-lax-1.0.0.tgz", | ||
492 | - "integrity": "sha1-evjzA2Rem9eaJy56FKxovAYJ2nM=", | ||
493 | - "requires": { | ||
494 | - "prepend-http": "^1.0.1" | ||
495 | - } | ||
496 | - }, | ||
497 | "uuid": { | 468 | "uuid": { |
498 | "version": "3.3.2", | 469 | "version": "3.3.2", |
499 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", | 470 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", | ... | ... |
test.js
0 → 100644
1 | +// | ||
2 | +const joke = require('./jokes/jokes.json'); | ||
3 | +const MongoClient = require('mongodb').MongoClient; | ||
4 | + | ||
5 | + | ||
6 | +MongoClient.connect('mongodb://localhost:27017', function (err, client){ | ||
7 | + if (err) throw err; | ||
8 | + var db = client.db('jokeapi'); | ||
9 | + | ||
10 | + question = []; | ||
11 | + punchline = []; | ||
12 | + count = 0; | ||
13 | + json_max = 376; | ||
14 | + random = function getRandomArbitrary() { | ||
15 | + return Math.random() * (376 - 1) + 1; | ||
16 | + } | ||
17 | + for(var i=1; i< json_max+1; i++){ | ||
18 | + result = db.collection('jokes').findOne({id: i,type:"programming"}); | ||
19 | + | ||
20 | + if(result === null) | ||
21 | + return; | ||
22 | + else if(result) | ||
23 | + { | ||
24 | + ++count; | ||
25 | + question.push(result.setup); | ||
26 | + punchline.push(result.punchline); | ||
27 | + // var n = result.count(result); | ||
28 | + // var r = Math.floor(Math.random() * n); | ||
29 | + // var randomElement = result.find().limit(1).skip(r); | ||
30 | + // var question = randomElement.setup; | ||
31 | + // var joke = randomElement.punchline; | ||
32 | + // console.log(question); | ||
33 | + // console.log(joke); | ||
34 | + // console.log(question); | ||
35 | + // console.log(punchline); | ||
36 | + //console.log(question); | ||
37 | + } | ||
38 | + } | ||
39 | + client.close(); | ||
40 | + console.log(question); | ||
41 | +}); | ||
42 | + |
test2.js
0 → 100644
1 | +const MongoClient = require('mongodb').MongoClient; | ||
2 | + | ||
3 | + | ||
4 | +//Get Random Joke | ||
5 | +MongoClient.connect('mongodb://localhost:27017', function (err, client){ | ||
6 | + if (err) throw err; | ||
7 | + var db = client.db('jokeapi'); | ||
8 | + | ||
9 | + json_max = 376; | ||
10 | + function getRandomInt() { | ||
11 | + min = Math.ceil(1); | ||
12 | + max = Math.floor(376); | ||
13 | + return Math.floor(Math.random() * (max - min + 1)) + min; | ||
14 | + } | ||
15 | + random = getRandomInt(); | ||
16 | + console.log(random); | ||
17 | + result = db.collection('jokes').findOne({id: random}); | ||
18 | + | ||
19 | + user = result; | ||
20 | + user.then(function(total){ | ||
21 | + console.log(total.setup); | ||
22 | + console.log(total.setup); | ||
23 | + }) | ||
24 | + client.close(); | ||
25 | +}) |
-
Please register or login to post a comment