-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (129 loc) · 3.48 KB
/
index.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
const express = require('@runkit/runkit/express-endpoint/1.0.0');
const axios = require('axios');
const cheerio = require('cheerio');
const moment = require('moment');
const app = express(exports);
/**
* Loads data from GreasyFork.
*
* In the url "https://greasyfork.org/en/scripts/93312-my-script",
* the `scriptId` is "93312".
*
* Sample response:
* {
* "version": "1.0.0",
* "updatedDateTime": "2018-11-17T17:50:02+00:00",
* "updated": "11/17/2018",
* "totalInstalls": "151",
* "dailyInstalls": "0"
* }
*/
app.get('/greasyfork/:scriptId', async (req, res) => {
try {
const scriptId = req.params.scriptId;
const url = `https://greasyfork.org/en/scripts/${scriptId}`;
const result = await axios.get(url);
const $ = cheerio.load(result.data);
let version = 'N/A';
const versionEl = $('dd.script-show-version');
if (versionEl.length) {
version = versionEl.text();
}
let updated = 'N/A';
let updatedDateTime = 'N/A';
const updatedEl = $('dd.script-show-updated-date time');
if (updatedEl.length) {
updatedDateTime = updatedEl.attr('datetime');
if (updatedDateTime) {
updated = moment(updatedDateTime).calendar();
}
}
let totalInstalls = 'N/A';
const totalInstallsEl = $('dd.script-show-total-installs');
if (totalInstallsEl.length) {
totalInstalls = totalInstallsEl.text();
}
let dailyInstalls = 'N/A';
const dailyInstallsEl = $('dd.script-show-daily-installs');
if (dailyInstallsEl.length) {
dailyInstalls = dailyInstallsEl.text();
}
res.json({
version,
updatedDateTime,
updated,
totalInstalls,
dailyInstalls,
});
} catch (e) {
res.json({
errors: [
{
status: 400,
detail: `Unable to load data from GreasyFork: ${e}`,
},
],
});
}
});
/**
* Loads data from OpenUserJS.
*
* In the url "https://openuserjs.org/scripts/username/My_Script",
* the `namespace` is "username" and the `name` is "My_Script".
*
* Sample response:
*
* {
* "version": "1.0.0",
* "updatedDateTime": "2019-07-21T05:41:58.134Z",
* "updated": "07/21/2019",
* "totalInstalls": "254168"
* }
*/
app.get('/openuserjs/:namespace/:name', async (req, res) => {
try {
const namespace = req.params.namespace;
const name = req.params.name;
const url = `https://openuserjs.org/scripts/${namespace}/${name}`;
const result = await axios.get(url);
const $ = cheerio.load(result.data);
let version = 'N/A';
const versionEl = $('.panel-body .script-meta code');
if (versionEl.length) {
version = versionEl.text().split('+')[0];
}
let updated = 'N/A';
let updatedDateTime = 'N/A';
const updatedEl = $('.panel-body .script-meta time');
if (updatedEl.length == 2) {
updatedDateTime = updatedEl.eq(1).attr('datetime');
if (updatedDateTime) {
updated = moment(updatedDateTime).calendar();
}
}
let totalInstalls = 'N/A';
const totalInstallsEl = $('.navbar .navbar-text');
if (totalInstallsEl.length) {
const match = totalInstallsEl.text().match(/[0-9]+/);
if (match !== undefined && match.length) {
totalInstalls = match[0];
}
}
res.json({
version,
updatedDateTime,
updated,
totalInstalls,
});
} catch (e) {
res.json({
errors: [
{
status: 400,
detail: `Unable to load data from OpenUserJS: ${e}`,
},
],
});
}
});