forked from ziad-saab/reddit-cli-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowse-reddit.js
244 lines (216 loc) · 10.6 KB
/
browse-reddit.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var inquirer = require('inquirer');
var redditFunctions = require('./reddit');
require('colors');
require("longjohn");
var imageToAscii = require("image-to-ascii")
function displayPost(post) {
console.log("The title of the post is: ", post.data.title.bold);
console.log(('https://reddit.com' + post.data.permalink).blue.underline);
console.log('\n');
console.log(("Number of ups :" + post.data.ups).red);
}
function displayPostSelected(selection) {
console.log(("The author of the post is: ").red, selection[0].data.author);
console.log('\n');
console.log(("The link to the post is:").red, selection[0].data.url);
console.log('\n');
console.log(("The title of the post is: ").red, selection[0].data.title);
}
function displayListSubreddit(name) {
console.log(name.data.display_name.bold);
console.log(('https:reddit.com/r/' + name.data.display_name).blue.underline);
console.log('\n');
}
function displaySubRedditPost(name) {
console.log(name.data.title.bold);
console.log(('https://reddit.com' + name.data.permalink).blue.underline);
console.log('\n');
}
function checkPostImage(post) {
if (post[0].data.post_hint === "image") {
return true;
}
return false;
}
var menuChoices = [{
name: 'Show homepage',
value: 'HOMEPAGE'
}, {
name: 'Show subreddit',
value: 'SUBREDDIT'
}, {
name: 'List subreddits',
value: 'SUBREDDITS'
}];
function mainMenu() {
inquirer.prompt({
type: 'list',
name: 'menu',
message: 'What do you want to do?',
choices: menuChoices
}).then(
function(answers) {
if (answers.menu === 'HOMEPAGE') {
redditFunctions.getHomepage(function(err, homepagePosts) {
if (err) {
console.log('There was a problem, try again later');
}
else {
var mapedArrayHomePost = homepagePosts.map(function(element) {
return {
name: element.data.title,
value: element.data.permalink
}
})
inquirer.prompt({
type: 'list',
name: 'menu',
message: 'Choose a post you want to see!',
choices: mapedArrayHomePost
}).then(
function(input) {
var userSubInput = input['menu'];
redditFunctions.getHomePagePost(userSubInput, function(err, displayHomePagePost) {
if (err) {
console.log("There was a problem loading post");
}
else {
displayPostSelected(displayHomePagePost)
if (checkPostImage(displayHomePagePost)) {
imageToAscii(displayHomePagePost[0].data.url, function(err, converted) {
console.log(err || converted);
});
}
else {
console.log("No image to Ascii");
}
mainMenu();
}
})
}
)
}
});
}
else if (answers.menu === 'SUBREDDIT') {
inquirer.prompt({
type: 'input',
name: 'Subreddit Name',
message: 'Write the name of the subreddit you wish to visit'
}).then(
function(input) {
redditFunctions.getSubreddit(input['Subreddit Name'], function(err, subredditTitles) {
if (err) {
console.log("There was a problem loading subreddits");
}
else {
var mapedArraySubrPost = subredditTitles.map(function(element) {
return {
name: element.data.title,
value: element.data.permalink
}
})
inquirer.prompt({
type: 'list',
name: 'menu',
message: 'Choose a post you want to see!',
choices: mapedArraySubrPost
}).then(
function(input) {
var userSubInput = input['menu'];
redditFunctions.getSubPagePost(userSubInput, function(err, displaySubPagePost) {
if (err) {
console.log("There was a problem loading post");
}
else {
displayPostSelected(displaySubPagePost)
if (checkPostImage(displaySubPagePost)) {
imageToAscii(displaySubPagePost[0].data.url, function(err, converted) {
console.log(err || converted);
});
}
else {
console.log("No image to Ascii")
}
mainMenu();
}
})
}
)
}
});
}
);
}
else if (answers.menu === 'SUBREDDITS') {
redditFunctions.getSubreddits(function(err, listofSubr) {
if (err) {
console.log("There was a problem loading subreddits");
}
else {
var mapedArraySub = listofSubr.map(function(element) {
return {
name: element.data.title.bold,
value: element.data.display_name
}
})
inquirer.prompt({
type: 'list',
name: 'menu',
message: 'Select the subreddit you wish to visit',
choices: mapedArraySub
}).then(
function(input) {
var userInput = input['menu'];
redditFunctions.getSubreddit(userInput, function(err, res) {
if (err) {
console.log("There was a problem loading subreddit");
}
else {
var mapedArraySubPost = res.map(function(element) {
return {
name: element.data.title,
value: element.data.permalink
}
})
inquirer.prompt({
type: 'list',
name: 'menu',
message: 'Choose a post you want to see!',
choices: mapedArraySubPost
}).then(
function(input) {
var userSubInput = input['menu'];
redditFunctions.getSubredditPost(userSubInput, function(err, displaySubRedPost) {
if (err) {
console.log("There was a problem loading post");
}
else {
displayPostSelected(displaySubRedPost)
if (checkPostImage(displaySubRedPost)) {
imageToAscii(displaySubRedPost[0].data.url, function(err, converted) {
console.log(err || converted);
});
}
else {
console.log("No image to Ascii")
}
mainMenu();
}
})
}
)
}
})
}
)
.catch(function(err) {
console.log(err.stack);
})
}
});
}
}
);
}
mainMenu();