-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdblp.js
132 lines (119 loc) · 3.83 KB
/
dblp.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
// XML to JSON mechanisms
const xml2js = require('xml2js');
const request = require('request');
// Local requests
const DBLPPerson = require('./dblp-person.js');
/**
* DBLP class
* It is responsible for requesting the XML file containing
* the user data.
*
* It also parses the XML data to JSON and does some data cleaning
* procedures to enhance the JSON object.
*/
class DBLP {
/**
* DBLP constructor
* @return {object} The DBLP object
*/
constructor() {
this.nameBaseURL = 'https://dblp.org/pers/xx/';
this.pidBaseURL = 'http://dblp.org/pid';
this.options = {
charkey: '_value',
mergeAttrs: true,
explicitArray: false,
};
}
/**
* Function that requests the user data by name
* @param {string} first First name of the dblp person according to dblp user page
* @param {string} last Last name of the dblp person according to dblp user page
* @return {object} DBLPPerson object
*/
getByName(first, last) {
return new Promise((resolve, reject) => {
// Create the xml file name to request from the function parameters
// TODO: sanitize
const xml = `${last.charAt(0).toLowerCase()}/${last}:${first}.xml`;
// Build the request url
const url = this.nameBaseURL + xml;
// Get the data in the url
DBLP.get(url, this.options).then((result) => {
resolve(result);
}, () => {
reject(new Error(`[DBLP getByName] Bad request - check requested user name - ${url}`));
});
});
}
/**
* Function that requests the user data by personal ID
* @param {string} pid Personal ID string
* @return {object} DBLPPerson object
*/
getByPID(pid) {
return new Promise((resolve, reject) => {
// Build the request url
// TODO: sanitize
const url = `${this.pidBaseURL}/${pid}.xml`;
// Get the data in the url
DBLP.get(url, this.options).then((result) => {
resolve(result);
}, () => {
reject(new Error(`[DBLP getByPID] Bad request - check requested PID - ${url}`));
});
});
}
/**
* Function that requests the user data by homepage (homepages/pid)
* @param {string} homepage Homepage string that includes the pid
* @return {object} DBLPPerson object
*/
getByHomepage(homepage) {
return new Promise((resolve, reject) => {
// Split the homepage string
const splitHomepage = homepage.split('/');
const pid = `${splitHomepage[1]}/${splitHomepage[2]}`;
// Build the request url
// TODO: sanitize
const url = `${this.pidBaseURL}/${pid}.xml`;
// Get the data in the url
DBLP.get(url, this.options).then((result) => {
resolve(result);
}, () => {
reject(new Error(`[DBLP getByHomepage] Bad request - check requested homepage - ${url}`));
});
});
}
/**
* Function that requests the user data from the url
* @param {string} url The url that points to the XML file
* @return {object} DBLPPerson object
*/
static get(url, parseOptions) {
return new Promise((resolve, reject) => {
request(url, (requestError, response, body) => {
// Check response status code
if (response && response.statusCode === 200) {
// Create parser instance
const parser = new xml2js.Parser(parseOptions);
// Parse XML
parser.parseString(body, (parseError, xml) => {
try {
// console.log(JSON.stringify(xml, null, 2));
// Create a DBLPPerson object from the raw json
const dblpp = new DBLPPerson(xml);
resolve(dblpp);
} catch (e) {
console.error(e);
reject(e);
}
});
} else {
reject(new Error(`[DBLP get] Bad request - check requested URI - ${url}`));
}
});
});
}
}
module.exports = DBLP;