This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
165 lines (130 loc) · 5.87 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
var express = require('express');
var app = express();
var alexaAPI = require('alexa');
var alexa = new alexaAPI(process.env.ALEXA_ID, process.env.ALEXA_SECRET);
var bw = require('./builtwith.js');
var _ = require('underscore');
var parseString = require('xml2js').parseString;
var keys = process.env.SECRET_KEYS.split(',');
var Mozscape = require('mozscape').Mozscape;
var moz = new Mozscape(process.env.MOZ_ACCESS_ID, process.env.MOZ_SECRET);
// Serve static content for the app from the "public" directory in the application directory.
app.use(express.static(__dirname + '/public'));
// do our super simple authentication to prevent randos from spending our $.
app.use(function(req, res, next) {
if(!_.contains(keys, req.query.key)) {
res.status(401).json({error: 'Key missing or incorrect'});
} else if(typeof req.query.domain === 'undefined') {
res.status(400).json({error: 'Domain is a required parameter'});
// } else if(typeof req.query.rg === 'undefined') {
// res.status(400).json({error: 'ResponseGroup is a required parameter'});
} else {
next();
}
});
// a simple status call.
app.get('/', function(req, res){
res.send('A-okay');
});
app.get('/UrlInfo', function(req, res) {
// use this to validate that a
var responsegroups = ['RelatedLinks', 'Categories', 'Rank', 'RankByCountry', 'RankByCity', 'UsageStats','ContactInfo', 'Speed', 'Keywords', 'OwnedDomains', 'LinksInCount','SiteData'];
request = alexa.makeRequest('UrlInfo', req.query.domain, req.query.rg);
// alexa.get(request, function(err, results){
// if(!err) {
// parseString(results.body, function(err, body){
// res.send(body);
// });
// }
// });
});
app.get('/UrlMetrics', function(req, res){
// use this to check Moz URL Metrics
// https://github.com/scott-wyatt/node-mozscape/blob/master/lib/mozscape.js#L225
var cols = ['mozRank','mozTrust','page_authority','domain_authority'];
moz.urlMetrics(req.query.domain, cols, function(err, results){
// if no errors
if(!err) {
// build a response for humans from the moz response based on the mozbar
response = {};
response.mozRank = results.umrp.toFixed(2);
response.mozTrust = results.utrp.toFixed(2);
response.pageAuthority = results.upa.toFixed(0);
response.domainAuthority = results.pda.toFixed(0);
// send the response
res.send(response);
} else {
// log the error to console
console.log(err);
// and respond with an error saying something is wrong
res.send(JSON.stringify(err));
}
});
})
app.get('/simple', function(req, res) {
request = alexa.makeRequest('UrlInfo', req.query.domain, 'RelatedLinks,Categories,Rank,RankByCountry,RankByCity,UsageStats,ContactInfo,Speed,Keywords,OwnedDomains,LinksInCount,SiteData');
var response = new Object;
response.contactInfo = {};
response.trafficData = {};
response.moz = {};
// do alexa first
alexa.get(request, function(err, results){
if(!err) {
parseString(results.body, function(err, body){
// make the data more meaningful
console.log(response);
// contact info
response.contactInfo.dataUrl = body["aws:UrlInfoResponse"]["aws:Response"][0]["aws:UrlInfoResult"][0]["aws:Alexa"][0]["aws:ContactInfo"][0]["aws:DataUrl"][0]["_"];
response.contactInfo.phoneNumber = body["aws:UrlInfoResponse"]["aws:Response"][0]["aws:UrlInfoResult"][0]["aws:Alexa"][0]["aws:ContactInfo"][0]["aws:PhoneNumbers"][0]["aws:PhoneNumbers"];
response.contactInfo.ownerName = body["aws:UrlInfoResponse"]["aws:Response"][0]["aws:UrlInfoResult"][0]["aws:Alexa"][0]["aws:ContactInfo"][0]["aws:OwnerName"][0];
response.contactInfo.email = body["aws:UrlInfoResponse"]["aws:Response"][0]["aws:UrlInfoResult"][0]["aws:Alexa"][0]["aws:ContactInfo"][0]["aws:Email"][0];
if(typeof body["aws:UrlInfoResponse"]["aws:Response"][0]["aws:UrlInfoResult"][0]["aws:Alexa"][0]["aws:ContactInfo"][0]["aws:CompanyStockTicker"][0]["aws:Symbol"] !== "undefined") {
response.contactInfo.public = true;
} else {
response.contactInfo.public = false;
}
// content data
// related
// traffic data
response.trafficData.awsRank = body["aws:UrlInfoResponse"]["aws:Response"][0]["aws:UrlInfoResult"][0]["aws:Alexa"][0]["aws:TrafficData"][0]["aws:Rank"][0];
// use this to check Moz URL Metrics
// https://github.com/scott-wyatt/node-mozscape/blob/master/lib/mozscape.js#L225
var cols = ['mozRank','mozTrust','page_authority','domain_authority'];
moz.urlMetrics(req.query.domain, cols, function(err, results){
// if no errors
if(!err) {
// build a response for humans from the moz response based on the mozbar
response.moz.mozRank = results.umrp.toFixed(2);
response.moz.mozTrust = results.utrp.toFixed(2);
response.moz.pageAuthority = results.upa.toFixed(0);
response.moz.domainAuthority = results.pda.toFixed(0);
// send the response
res.send(response);
} else {
// log the error to console
console.log(err);
// and respond with an error saying something is wrong
res.send(JSON.stringify(err));
}
});
}); //!err
}
}); // alexa.get
});
app.get('/TrafficHistory', function(req, res) {
if (req.query.rg !== 'History') {
res.status(400).json({error: 'History is the only ResponseGroup'});
} else {
request = alexa.makeRequest('TrafficHistory', req.query.domain, req.query.rg, req.query.range);
alexa.get(request, function(err, results){
if(!err) {
parseString(results.body, function(err, body){
res.send(body);
});
}
});
}
});
var server = app.listen(process.env.PORT || 3000, function() {
console.log('Listening on port %d', server.address().port);
});