-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (138 loc) · 6.34 KB
/
server.js
File metadata and controls
160 lines (138 loc) · 6.34 KB
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
var http = require('http');
var restify = require('restify');
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var jsx = require('node-jsx');
const got = require('got');
var cheerio = require('cheerio');
var port = process.env.port || 1337;
var knex = require('knex')({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : 'branko',
database : 'Klix'
}
});
jsx.install();
var Index = require('./Views/index.jsx');
var server = restify.createServer();
server.get({ path : '/updatearticles/' } , updateArticles);
server.get(/\/mainstyle\//, restify.serveStatic({
directory: './styles',
file: 'main.css'
}));
server.get({ path : '/' } , getArticles);
function getArticles(req, res , next) {
res.setHeader('Access-Control-Allow-Origin', '*');
knex('article')
.join('subcategory', 'article.subCategoryName', '=', 'subcategory.name')
.join('category', 'subcategory.categoryName', '=', 'category.name')
.select('article.*', 'subcategory.*', 'category.*')
.then(function (articles) {
var grouped = [];
for (var i = 0; i < articles.length; i++) {
if (!grouped.some(function (item) { return item.categoryName == (articles[i].isPopular ? "Popular" : articles[i].categoryName); })) {
var toInsert = {
categoryName: (articles[i].isPopular ? "Popular" : articles[i].categoryName), categoryId: articles[i].siteIdentifer,
subCategories: [ articles[i].subCategoryName], categoryColor: articles[i].color, rows: [{ articles: [articles[i]] }]
};
if (articles[i].isPopular)
grouped.unshift(toInsert);
else
grouped.push(toInsert);
}
else {
var index = grouped.findIndex(function (item) {
return item.categoryName == (articles[i].isPopular ? "Popular" : articles[i].categoryName);
});
var big = articles[i].isBig || grouped[index].rows[0].articles.some(function (article) { return article.isBig; }) ? 0 : 1;
if (grouped[index].rows[0].articles.length > (2 + big)) {
if (grouped[index].rows[0].articles.length == (3 + big) && grouped[index].rows.length == 1) {
grouped[index].rows.push({ articles: [] });
}
grouped[index].rows[1].articles.push(articles[i]);
}
else {
grouped[index].rows[0].articles.push(articles[i]);
}
if (articles[i].subCategoryName != "None" && grouped[index].subCategories.indexOf(articles[i].subCategoryName) == -1)
grouped[index].subCategories.push(articles[i].subCategoryName);
}
}
return grouped;
})
.then(function (grouped) {
res.end(ReactDOMServer.renderToString(React.createElement(Index, { articles: grouped })));
});
}
function updateArticles(req, res) {
var newsServer = 'http://www.klix.ba';
res.writeHead(200, { 'Content-Type': 'text/html' });
got(newsServer)
.then(function (response) {
var articles = [];
var subCategories = [{ name: "None", categoryName: "Popular" }];
var categories = [{ name: 'Popular', siteIdentifer: "", color: "" }];
$ = cheerio.load(response.body, { decodeEntities: false });
$("article").each(function () {
if ($(this).closest(".nightlifeBlock").length == 0) {
var $block = $(this).closest(".block-dyn");
var $blockHeading = $block.find(".block-head .headingUrl");
if ($block.length > 0 && $blockHeading.length > 0 && !categories.some(function (category) { return category.name.indexOf($blockHeading.text()) > -1 })) {
categories.push({
name: $blockHeading.text(),
siteIdentifer: $blockHeading.attr("class").split(" ")[1].replace("tch_", ""),
color: $block.find(".kocka").css('background-color')
});
}
var subCategory = $(this).find("span.kategorija").contents(":not(i)").text() || "None";
if (!subCategories.some(function (category) { return category.name.indexOf(subCategory) > -1 })) {
subCategories.push({
name: subCategory,
categoryName: $blockHeading.text() || $(this).find(".kategorija").attr("class").split(" ")[1].replace("tc_", "").capitalize()
});
}
articles.push({
title: $(this).find("h1").html(),
link: newsServer + $(this).find("a").attr("href"),
image: $(this).find("img").attr("src"),
summary: $(this).find(".uvod").html(),
time: $(this).find(".date").first().contents(":not(i)").text().replace("prije ", ""),
commentsCount: $(this).find(".comments").contents(":not(i)").text(),
isPopular: $(this).closest(".block.first.struct").length == 1,
isBig: $(this).find("h1").is(".veliki"),
isFlagged: $(this).is(".obojena"),
subCategoryName: subCategory
});
}
});
console.log(articles);
knex("article").del()
.then(function () {
return knex("subcategory").del()
})
.then(function () {
return knex("category").del()
})
.then(function () {
return knex('category').insert(categories);
})
.then(function () {
return knex('subcategory').insert(subCategories);
})
.then(function () {
return knex('article').insert(articles);
})
.then(function () {
res.end("Done");
});
})
.catch(function (error) { console.log(error) });
}
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
}
server.listen(port);
console.log('Listening on http://localhost:' + port);