This repository has been archived by the owner on Aug 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
185 lines (163 loc) · 5.36 KB
/
app.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
var superagent = require('superagent');
var facebookSignedRequest = require('./lib/facebookSignedRequest');
var countdown = require('./lib/countdown.js');
var cons = require('consolidate');
var config = require('./config');
var express = require('express');
var swig = require('swig');
var moment = require('moment');
var async = require('async');
var app = express();
// all environments
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.engine('html', cons.swig);
swig.init({
root: 'views',
allowErrors: true,
filters: require('./myfilters')
});
// development only
app.configure('development', function(){
app.set('facebookSecret', config.config.facebookDev.secret);
console.log('dev');
});
// production only
app.configure('production', function(){
app.set('facebookSecret', config.config.facebook.secret);
console.log('prod');
});
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
app.locals.currentDate = moment().format('D \\de MMMM \\de YYYY');
app.locals.urlMicrositio = config.config.links.urlMicrositio;
var requestApiData = function (url, query, callback) {
var apiRequest = superagent
.get(url)
.set('Accept', 'application/json');
async.forEach(query, function(item, callback){
apiRequest.query(item);
}, function(err) {
if (err) {
console.log(err);
}
});
apiRequest.end(function(response) {
if (response.ok) {
callback(response.body);
}
});
};
var fechaConcurso = function(req, res, next) {
var url = config.config.webServices.proximoConcurso.url;
requestApiData(url, [], function(response){
if (response) {
req.fechaConcurso = response.RESPUESTA[0];
next();
}
});
};
var middleware = [fechaConcurso];
var homepage = function(req, res) {
var signedRequest = req.body.signed_request;
var facebookSecret = app.get('facebookSecret');
var decodedSignedRequest = facebookSignedRequest.decode(signedRequest, facebookSecret);
var noLikeTemplate = 'no-like.html';
var likeTemplate = 'like.html';
var url = config.config.webServices.fotosPortada.url;
var params = config.config.webServices.fotosPortada.params;
requestApiData(url, params, function(data) {
if (decodedSignedRequest.page.liked) {
res.render(likeTemplate, {
fotosPortada: data.RESPUESTA,
fechaConcurso: req.fechaConcurso
});
}
res.render(noLikeTemplate, {
});
});
};
var homepageGet = function(req, res) {
var likeTemplate = 'like.html';
var url = config.config.webServices.fotosPortada.url;
var params = config.config.webServices.fotosPortada.params;
requestApiData(url, params, function(data) {
res.render(likeTemplate, {
fotosPortada: data.RESPUESTA,
fechaConcurso: req.fechaConcurso
});
});
};
var masleidas = function(req, res) {
var url = config.config.webServices.MasVisitadasSemana.url;
var params = config.config.webServices.MasVisitadasSemana.params;
requestApiData(url, params, function(data) {
res.render('masleidas.html', {
masPopulares: data.RESPUESTA,
fechaConcurso: req.fechaConcurso
});
});
};
var maspopulares = function(req, res) {
var url = config.config.webServices.MasPopulares.url;
var params = config.config.webServices.MasPopulares.params;
requestApiData(url, params, function(data) {
res.render('maspopulares.html', {
masPopulares: data.RESPUESTA,
fechaConcurso: req.fechaConcurso
});
});
};
var ganadores = function(req, res) {
res.render('ganadores.html', {
fechaConcurso: req.fechaConcurso
});
};
var masnoticias = function(req, res) {
var url = config.config.webServices.NoticiasPorPagina.url;
var params = config.config.webServices.NoticiasPorPagina.params;
if (req.query.page) {
params.page = req.query.page;
}
requestApiData(url, params, function(data) {
res.render('masnoticias.html', {
masPopulares: data.RESPUESTA,
fechaConcurso: req.fechaConcurso
});
});
};
var noticia = function(req, res) {
var url = config.config.webServices.NoticiasPorId.url;
var params = config.config.webServices.NoticiasPorId.params;
console.log(url);
if (req.query.id) {
params[1].ID= req.query.id;
}
console.log(params);
requestApiData(url, params, function(data) {
res.render('noticia.html', {
noticia: data.RESPUESTA[0],
fechaConcurso: req.fechaConcurso
});
console.log(data);
});
console.log(req.query.id);
};
var appInstalled = function(req, res) {
res.render('appinstalled.html', {
profileUrl: config.config.facebook.profileUrl,
appName: config.config.name
});
}
app.post('/', middleware, homepage);
app.get('/', middleware, homepageGet);
app.get('/masleidas', middleware, masleidas);
app.get('/maspopulares', middleware, maspopulares);
app.get('/masnoticias', middleware, masnoticias);
app.get('/ganadores', middleware, ganadores);
app.get('/noticia', middleware, noticia);
app.get('/appinstalled', appInstalled);
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Habladme al puerto " + port);
});