-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch-download-data.js
More file actions
116 lines (95 loc) · 2.8 KB
/
Copy pathfetch-download-data.js
File metadata and controls
116 lines (95 loc) · 2.8 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
const got = require("got");
const R = require("ramda");
const Promise = require("bluebird");
const moment = require("moment");
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
const LIMIT = 64;
async function getPackageData(rows)
{
const fetchedData = await fetchDataForPackages(rows);
return Promise.map(fetchedData, (record) => {
return prisma.npm_downloads_count.create({
data: {
name: record.package,
downloads: record.downloads,
retrieved_at: moment().format("YYYY-MM-DD")
},
});
});
}
async function fetchDataForPackage(row)
{
const url = apiEndpoint(row.name);
try
{
const response = await got(url).json();
return [response];
}
catch (err)
{
if (err.message === "Response code 404 (Not Found)")
return [{ package: row.name, downloads: 0 }];
else
throw err;
}
}
async function fetchDataForPackages(rows)
{
if (rows.length < 1)
return [];
if (rows.length === 1)
return fetchDataForPackage(rows[0]);
const url = apiEndpoint(R.pluck("name", rows));
const response = await got(url).json();
return R.map(([packageName, data]) => {
let result = data;
if (data === null)
result = { package: packageName, downloads: 0 };
return result;
}, R.toPairs(response));
}
async function main()
{
const npmDownloads = await prisma.npm_install_scripts_checker.findMany();
let packageGroup = [];
for (var i = 0; i < npmDownloads.length; i++)
{
const record = npmDownloads[i];
const existingRow = await prisma.npm_downloads_count.findUnique({
where: { name: record.name }
});
if (existingRow)
{
console.log(`Found existing record for ${existingRow.name}. Skipping…`);
continue;
}
// scoped package
// https://api.npmjs.org/downloads/point/last-month/@slack/client
if (record.name.charAt(0) === "@")
{
await getPackageData([record]);
}
else
{
packageGroup.push(record);
}
// for non-scoped packages, we can request data for up to 128 packages at a time.
// to be safe, I use a LIMIT of 64
// https://api.npmjs.org/downloads/point/last-day/npm,express
if (packageGroup.length >= LIMIT)
{
await getPackageData(packageGroup);
packageGroup = [];
}
}
await getPackageData(packageGroup);
}
function apiEndpoint(packages)
{
let url = "https://api.npmjs.org/downloads/point/last-month/";
if (R.is(String, packages))
return url += packages;
return url += packages.join(",");
}
main();