-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (112 loc) · 3.37 KB
/
server.js
File metadata and controls
131 lines (112 loc) · 3.37 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
var express = require('express');
var fs = require('fs');
var request = require('request-promise');
var cheerio = require('cheerio');
var app = express();
app.use(express.static(__dirname + '/public'));
app.set('views', './views')
app.set('view engine', 'pug')
// THE BROAD
var scrapTheBoard = function(artist){
var url = "http://www.thebroad.org/art/"+encodeURIComponent(artist)
return request(url)
.then(extractArt)
}
var shuffleArt = function(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var extractArt = function(html){
var $ = cheerio.load(html);
var paintings = []
$('.view-content > .views-row').each(function(){
var painting = $(this)
var title = painting.find('a[href]').text()
var imageURL = painting.find('img').attr('src')
paintings.push({
title: title,
imageURL: imageURL
})
})
return paintings
}
app.get('/scrape', function(req, res){
Promise.all([
scrapTheBoard('jean‐michel-basquiat'),
scrapTheBoard('keith-haring'),
scrapTheBoard('mark-bradford'),
scrapTheBoard('barbara-kruger'),
scrapTheBoard('neo-rauch'),
scrapTheBoard('frank-stella'),
scrapTheBoard('robert-therrien'),
scrapTheBoard('philip-taaffe'),
scrapTheBoard('william-kentridge'),
scrapTheBoard('kara-walker'),
scrapTheBoard('glenn-ligon'),
scrapTheBoard('takashi-murakami'),
scrapTheBoard('albert-oehlen')
])
.then(function(allPaintings){
return allPaintings.reduce(function(a,b){
return a.concat(b)
})
})
.then(function(paintings){
shuffleArt(paintings)
res.render('index', {paintings})
console.log(paintings)
})
.catch(function(err){
res.status(500).send(err.message)
})
// fs.writeFile('output.json', JSON.stringify(paintings, null, 2), function(err){
// if (err) throw err
// console.log('File successfully written! - Check your project directory for output.json file');
// })
// res.send(paintings)
// })
})
// app.get('/scrape', function(req, res){
// url = 'http://www.thebroad.org/art/keith-haring';
//
// request(url, function(error, response, html){
// if (error){
// response.status(500).send(error.message)
// return
// }
//
//
// var $ = cheerio.load(html);
// var paintings = []
//
// $('.view-content > .views-row').each(function(){
// var painting = $(this)
// var title = painting.find('a[href]').text()
// var imageURL = painting.find('img').attr('src')
// paintings.push({
// title: title,
// imageURL: imageURL
// })
// })
// fs.writeFile('output.json', JSON.stringify(paintings, null, 2), function(err){
// if (err) throw err
// console.log('File successfully written! - Check your project directory for output.json file');
// })
// res.send(paintings)
// console.log('party time?', paintings)
// })
// })
//
const port = process.env.PORT || 8081;
app.listen(port,()=>{'Guess the artist!'});
module.exports = app;