Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.

Commit 07a5de2

Browse files
author
Frank Cash
authored
Merge pull request #92 from frankcash/frankcash/91
Adds swagger
2 parents 093bc12 + 2a41a2d commit 07a5de2

12 files changed

Lines changed: 209 additions & 41 deletions

File tree

File renamed without changes.

app.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
/*
22
* * Module dependencies
33
* */
4-
var express = require('express');
4+
let express = require('express');
55
require('dotenv').config();
6-
var lobstersRoute = require('./routes/lobsters.js');
7-
var redditRoute = require('./routes/reddit.js');
8-
var hackerRoute = require('./routes/hackernews.js');
6+
let lobstersRoute = require('./routes/lobsters.js');
7+
let redditRoute = require('./routes/reddit.js');
8+
let hackerRoute = require('./routes/hackernews.js');
9+
let swaggerUi = require('swagger-ui-express'),
10+
swaggerDocument = require('./swagger/swagger.json');
911

1012
var app = express(); // sets up the server
1113

1214
var PORT = process.env.PORT || 3000;
1315

16+
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
17+
1418
app.get('/ycomb', hackerRoute.htop);
1519

1620

db/schema/crawls.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ CREATE TABLE crawls(
55
title VARCHAR(255),
66
comments VARCHAR(255),
77
crawled_at TIMESTAMP
8-
9-
);
8+
);

docker-compose.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3'
22
services:
33
postgres:
44
container_name: hackerqueue-pg
5-
image: postgres:9.6.3
5+
image: postgres:12-alpine
66
environment:
77
POSTGRES_PASSWORD: hackerx
88
POSTGRES_USER: hacker
@@ -13,7 +13,8 @@ services:
1313
- ./.data/pg:/var/lib/postgresql/data
1414
web:
1515
build: .
16-
image: ledgehog:latest
16+
image: hackerqueue:latest
17+
container_name: hackerqueue-web
1718
environment:
1819
DB_HOST: postgres
1920
DATABASE_URL: "postgres://hacker:hackerx@postgres:5432/hackerqueue"
@@ -22,4 +23,4 @@ services:
2223
depends_on:
2324
- postgres
2425
links:
25-
- postgres:postgres
26+
- postgres:postgres

helpers/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ exports.url_refer = (url)=>{
33
return url+="?utm_source=hackerqueue"
44
}
55
return url
6-
};
6+
};
7+
8+
exports.wrap = (arr)=>{
9+
return {Crawls: arr}
10+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "HackerQueue",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"private": "false",
55
"license": "MIT",
66
"contributors": [
@@ -33,7 +33,9 @@
3333
"jade": "*",
3434
"nib": "*",
3535
"pg": "^7.4.1",
36-
"request": "^2.88.0"
36+
"request": "^2.88.0",
37+
"swagger-node-express": "^2.1.3",
38+
"swagger-ui-express": "^4.1.2"
3739
},
3840
"devDependencies": {
3941
"chai": "*",

public/app.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,50 @@ app.controller("AppTest", function($scope, $http, $location, $anchorScroll){
2020
For top posts
2121
**/
2222
$http.get('/ycomb').success(function(data) {
23-
$scope.hackerTop = data;
23+
$scope.hackerTop = data.Crawls;
2424
});
2525
$http.get('/lobster').success(function(data) {
26-
$scope.lobTop = data;
26+
$scope.lobTop = data.Crawls;
2727
});
2828
$http.get('/rp').success(function(data) {
29-
$scope.rTop = data;
29+
$scope.rTop = data.Crawls;
3030
});
3131

3232
/**
3333
For new posts
3434
**/
3535
$http.get('/ynew').success(function(data) {
36-
$scope.hackerNew = data;
36+
$scope.hackerNew = data.Crawls;
3737
});
3838
$http.get('/lnew').success(function(data) {
39-
$scope.lNew = data;
39+
$scope.lNew = data.Crawls;
4040
});
4141
$http.get('/rnew').success(function(data) {
42-
$scope.rNew = data;
42+
$scope.rNew = data.Crawls;
4343
});
4444

4545
$scope.refresh = function(){
4646
$http.get('/ycomb').success(function(data) {
47-
$scope.hackerTop = data;
47+
$scope.hackerTop = data.Crawls;
4848
});
4949
$http.get('/lobster').success(function(data) {
50-
$scope.lobTop = data;
50+
$scope.lobTop = data.Crawls;
5151
});
5252
$http.get('/rp').success(function(data) {
53-
$scope.rTop = data;
53+
$scope.rTop = data.Crawls;
5454
});
5555

5656
/**
5757
For new posts
5858
**/
5959
$http.get('/ynew').success(function(data) {
60-
$scope.hackerNew = data;
60+
$scope.hackerNew = data.Crawls;
6161
});
6262
$http.get('/lnew').success(function(data) {
63-
$scope.lNew = data;
63+
$scope.lNew = data.Crawls;
6464
});
6565
$http.get('/rnew').success(function(data) {
66-
$scope.rNew = data;
66+
$scope.rNew = data.Crawls;
6767
});
6868
};
6969

routes/hackernews.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ function parse(html){
88
let $ = cheerio.load(html);
99
$('.athing').each(function(){
1010
let $storylink = $(this).find('.storylink');
11-
const rank = $(this).find('.rank').text();
1211
const title = $storylink.text();
1312
const url = helpers.url_refer($storylink.attr('href'));
1413
let $subtext = $(this).next();
15-
const points = $subtext.find('.score').text();
16-
const username = $subtext.find('.hnuser').text();
1714
let $comments = $subtext.find('a').last();
1815
const comments = $comments.text();
1916
const YCOMB_COMMENT_URL = "https://news.ycombinator.com/";
@@ -28,12 +25,9 @@ function parse(html){
2825
});
2926

3027
let metadata = { // creates a new object
31-
rank: parseInt(rank),
3228
site: "HN",
3329
title: title,
3430
url: url,
35-
points: parseInt(points),
36-
username: username,
3731
comments: parseInt(comments),
3832
comments_link: comments_link
3933
};
@@ -45,7 +39,7 @@ function parse(html){
4539
exports.htop = function(req,res){
4640
request('https://news.ycombinator.com', function(error, response, html){
4741
if(!error && response.statusCode === 200){
48-
res.send(parse(html, "news.ycombinator.com"));
42+
res.send(helpers.wrap(parse(html, "news.ycombinator.com")));
4943
}
5044
});
5145

@@ -54,7 +48,7 @@ exports.htop = function(req,res){
5448
exports.hnew = function(req,res){
5549
request('https://news.ycombinator.com/newest', function(error, response, html){
5650
if(!error && response.statusCode === 200){
57-
res.send(parse(html, "news.ycombinator.com/newest"));
51+
res.send(helpers.wrap(parse(html, "news.ycombinator.com/newest")));
5852
}
5953
});
6054

routes/lobsters.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const parseLobsterElement = function(a) {
3131
const url = helpers.url_refer(fixSelfPost(a.children().attr('href')));
3232

3333
// parses link title
34-
const title = a.text();
34+
const title = a.text().replace(/(\r\n|\n|\r)/gm, "");
3535

3636
let commentsLabel = a.parent().children('.byline').children('span.comments_label');
3737
let commentsMatch = commentsLabel.text().match("[0-9]+");
@@ -59,7 +59,7 @@ const parseLobsterElement = function(a) {
5959

6060
const metadata = {
6161
site: source,
62-
title:title,
62+
title: title,
6363
url:url,
6464
comments:comments,
6565
comments_link:comments_link
@@ -90,8 +90,8 @@ const parseLobsterResponse = function(html) {
9090
exports.ltop = function(req, res){
9191
request('https://lobste.rs', function(error, response, html){
9292
if(!error && response.statusCode === 200){
93-
let metadataArray = parseLobsterResponse(html);
94-
res.send(metadataArray);
93+
const metadataArray = parseLobsterResponse(html);
94+
res.send(helpers.wrap(metadataArray));
9595
}
9696
});
9797
};
@@ -100,7 +100,7 @@ exports.lnew = function(req, res){
100100
request('https://lobste.rs/recent', function(error, response, html){
101101
if(!error && response.statusCode === 200){
102102
const metadataArray = parseLobsterResponse(html);
103-
res.send(metadataArray);
103+
res.send(helpers.wrap(metadataArray));
104104
}
105105
});
106106
};

routes/reddit.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ function parse(html, source){
2424
const comments_link = "https://www.reddit.com" + comments_tag.attr("href");
2525
const comments = parseInt(comments_tag.text());
2626

27-
const points = post.find('button[aria-label=upvote]').parent().children('div').text();
2827

2928
const title = title_tag.text();
3029
const url = helpers.url_refer(fixSelfPost(link_tag.attr('href')));
@@ -44,7 +43,6 @@ function parse(html, source){
4443
url: url,
4544
comments: comments,
4645
comments_link: comments_link,
47-
points: points
4846
};
4947

5048
metadataArray.push(metadata);
@@ -56,7 +54,7 @@ function parse(html, source){
5654
exports.rtop = function(req,res){
5755
request('http://www.reddit.com/r/programming', function(error, response, html){
5856
if(!error && response.statusCode === 200){
59-
res.send(parse(html, "reddit.com/r/programming"));
57+
res.send(helpers.wrap(parse(html, "reddit.com/r/programming")));
6058
}
6159
});
6260
};
@@ -65,7 +63,7 @@ exports.rtop = function(req,res){
6563
exports.rnew = function(req,res){
6664
request('http://www.reddit.com/r/programming/new/', function(error, response, html){
6765
if(!error && response.statusCode === 200){
68-
res.send(parse(html, "reddit.com/r/programming/new"));
66+
res.send(helpers.wrap(parse(html, "reddit.com/r/programming/new")));
6967
}
7068
});
7169
};

0 commit comments

Comments
 (0)