-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
157 lines (126 loc) · 4.51 KB
/
utils.js
File metadata and controls
157 lines (126 loc) · 4.51 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
const parse = require('url-parse');
const MongoClient = require('mongodb').MongoClient;
const _ = require('lodash');
const { baseUrl, mongodbConnectionString, mongodbCollection, mongodbDatabase } = require('./config');
module.exports = {
collectInternalLinks: ($) => {
const allLinks = [];
$(`a[href^='/']`).each(() => {
const link = $(this).attr('href');
link && link.includes('medium') && allLinks.push(`${baseUrl}${link}`);
});
$(`a[href^='http']`).each(function() {
const link = $(this).attr('href');
link && link.includes('medium') && allLinks.push(link);
});
return allLinks;
},
validURL: (url) => {
const pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if (!url || typeof url !== 'string') {
return false;
}
return pattern.test(url);
},
displayAllLinks: () => {
MongoClient.connect(mongodbConnectionString, (err, client) => {
if (err) {
console.log(err);
return;
}
const db = client.db(mongodbDatabase);
const collection = db.collection(mongodbCollection);
collection.distinct('pathname', function(err, uniqueUrlPathname) {
if (err) {
console.log('Unable to fetch links');
return;
}
uniqueUrlPathname.forEach((pathname) => {
collection.findOne({pathname: pathname}, (err, link) => {
if (err) {
console.log(`Unable to fetch link for ${pathname} pathname`);
return;
}
let qs = '';
console.log(`Url: ${link.origin}${pathname || ''}`);
link.query && link.query.forEach((q) => {
if (q && isNaN(q)) {
qs += q + ',';
}
});
link.query && console.log(`Url query parameters: ${qs}`);
link.reference && console.log(`Url reference count: ${link.reference}`, '\n');
});
});
});
});
},
dropLinksInDatabase: () => {
MongoClient.connect(mongodbConnectionString, (err, client) => {
if (err) {
console.log(err);
return;
}
const db = client.db(mongodbDatabase);
const collection = db.collection(mongodbCollection);
collection.remove({}, (err, result) => {
if (err) {
console.log('Unable to empty collection. Output may vary');
return;
}
console.log('\n', `Removed all documents from '${mongodbCollection}' collection`, '\n');
});
});
},
storeLinkInDatabase: (url, callback) => {
MongoClient.connect(mongodbConnectionString, (err, client) => {
if (err) {
console.log(err);
return false;
}
const db = client.db(mongodbDatabase);
const collection = db.collection(mongodbCollection);
const parsedUrl = parse(url, true);
try {
collection.findOne({ origin: parsedUrl.origin, pathname: parsedUrl.pathname }, (err, link) => {
if (err) {
console.log('Unable to access database');
return;
}
if (link && link.pathname === parsedUrl.pathname) {
link.reference = _.get(link, 'reference', 1) + 1;
}
if (link && ((_.find(link.queryList || [{}], parsedUrl.query) !== undefined) || (Array.isArray(link.queryList)
&& !(link.queryList.length) && _.isEmpty(parsedUrl.query)))) {
// no logic to update query list when similar urls exist
} else if (link && (link.href === parsedUrl.href)) {
// no logic to update query list when similar urls exist
} else {
if (!link) {
link = parsedUrl;
link.reference = 1;
}
if (!Array.isArray(_.get(link, 'queryList'))) {
link.queryList = [{}];
}
if (!Array.isArray(_.get(link, 'query'))) {
link.query = Object.keys(link.query) || [];
}
link.queryList.push(parsedUrl.query);
link.query.push(...Object.keys(parsedUrl.query));
link.query = [...new Set(link.query)];
}
collection.update({ pathname: link.pathname }, link, { upsert: true }, (err, result) => {
if (err) {
console.log('Unable to store links in database', link);
return;
}
console.log('Successfully stored url: ', link.href);
callback(link.href);
});
});
} catch (err) {
}
});
},
};