-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj-api.js
116 lines (91 loc) · 3.25 KB
/
proj-api.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const reddit_scraper = require('./reddit_scraper')
async function wrapper() {
const app = express();
const port = 5000;
test = {
postTitle: "HELLLOO THERE",
postDescription: "IS THEREW ANYBODY OUT THERE",
tag: "masks"
}
//keep post data
let projects = [];
//whew do the scraping thing here
let results = []
try{
await reddit_scraper.initialize('facemasks');
var posts = await reddit_scraper.getResults("masks", "facemasks");
results.push.apply(results, posts)
} catch (err){
console.log("uh oh facemasks promise failed reddit scraper brokin :(")
}
try{
await reddit_scraper.initialize('Sourdough');
var posts = await reddit_scraper.getResults("baking", "Sourdough");
results.push.apply(results, posts)
} catch (err){
console.log("uh oh sourdough promise failed reddit scraper brokin :(")
}
try{
await reddit_scraper.initialize('Breadit');
var posts = await reddit_scraper.getResults("baking", "Breadit");
results.push.apply(results, posts)
} catch (err){
console.log("uh oh Breadit promise failed reddit scraper brokin :(")
}
try{
await reddit_scraper.initialize('gardening');
var posts = await reddit_scraper.getResults("gardening", "gardening");
results.push.apply(results, posts)
} catch (err){
console.log("uh oh gardening promise failed reddit scraper brokin :(")
}
//go through scraped posts and make them into
//miniprojs and add to the projects
for (let index = 2; index < results.length; index++) {
let title = results[index][0];
let postURL = results[index][1];
let postTag = results[index][2];
let author = results[index][3];
let subreddit = results[index][4];
let description = "Post by " + author + ", scraped from r/" + subreddit + "\n"
console.log("post: ", title, description)
post = {
postTitle: title,
postDescription: description,
tag: postTag,
link: postURL,
}
projects.push(post)
}
app.use(cors());
//I assume it's turning data into JSON format
//it grabs http body, decodes, and passes as JSON to req.body
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.post('/', (req, res) => {
const proj = req.body;
console.log(proj);
projects.push(proj);
//res.send('Project is added to the database');
});
//random comment
//DELETE THIS PART IF HEROKU DOESN"T WORK
app.delete('/', (req, res) => {
projects = projects.filter((value) => {
if(req.body.postTitle == value.postTitle
&& req.body.postDescription == value.postDescription
&& req.body.tag == value.tag)
return false;
else return true;
});
});
app.get("/", (req, res) => {
res.json(projects);
});
app.listen(/*process.env.PORT || */port);
console.log(`Hello world app listening on port ${port}!`);
}
wrapper();