-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·251 lines (246 loc) · 7.4 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env node
"use strict";
const rp = require("request-promise");
const cheerio = require("cheerio");
const { exec } = require("child_process");
const chalk = require("chalk");
const express = require("express");
const cloudflareScraper = require("cloudflare-scraper");
const app = express();
const port = 3000;
var pjson = require("./package.json");
const fs = require("fs");
const apkData = require("./functions/apkData.js");
var ArgumentParser = require("argparse").ArgumentParser;
var parser = new ArgumentParser({
version: "1.0",
addHelp: true,
description: "F-Droid Scraper",
});
parser.addArgument(["-path", "--path"], {
help: "Path for outputs to go to, do NOT include trailing slash ex: /home/user/Downloads",
required: true,
});
parser.addArgument(["-s", "--search"], {
help: "Search term",
required: true,
});
parser.addArgument(["-w", "--website"], {
help: "Website to search for. Available: (fdroid)",
required: true,
});
var args = parser.parseArgs();
console.log("💡 STARTING UP " + pjson.name + " v" + pjson.version + " 💡");
/* Functions */
const getDownloadFromLink = (link, site) => {
let ogPackage;
let jsonParsed;
let downloadLink;
switch (site) {
case "fdroid":
ogPackage = link.split("https://f-droid.org/en/packages/")[1];
rp("https://f-droid.org/api/v1/packages/" + ogPackage).then(function (
apiHTML
) {
jsonParsed = JSON.parse(apiHTML);
downloadLink =
"https://f-droid.org/repo/" +
jsonParsed.packageName +
"_" +
jsonParsed.suggestedVersionCode +
".apk";
console.log("📥 Downloading " + jsonParsed.packageName);
if (
fs.existsSync(
args.path +
"/" +
jsonParsed.packageName +
"_" +
jsonParsed.suggestedVersionCode +
".apk"
)
) {
console.log(
"🗃️ Cancelling download, already exists: " + jsonParsed.packageName
);
return;
}
exec(
"cd " + args.path + " && wget " + downloadLink,
(error, stdout, stderr) => {
if (error) {
console.log("❌ Error downloading " + jsonParsed.packageName);
return;
}
if (stderr) {
if (
fs.existsSync(
args.path +
"/" +
jsonParsed.packageName +
"_" +
jsonParsed.suggestedVersionCode +
".apk"
)
) {
console.log("✅ Downloaded " + jsonParsed.packageName);
}
return;
}
}
);
});
break;
case "apkmirror":
console.log(link);
if (true) return;
ogPackage = link.split("https://f-droid.org/en/packages/")[1];
rp("https://f-droid.org/api/v1/packages/" + ogPackage).then(function (
apiHTML
) {
jsonParsed = JSON.parse(apiHTML);
downloadLink =
"https://f-droid.org/repo/" +
jsonParsed.packageName +
"_" +
jsonParsed.suggestedVersionCode +
".apk";
console.log("📥 Downloading " + jsonParsed.packageName);
if (
fs.existsSync(
args.path +
"/" +
jsonParsed.packageName +
"_" +
jsonParsed.suggestedVersionCode +
".apk"
)
) {
console.log(
"🗃️ Cancelling download, already exists: " + jsonParsed.packageName
);
return;
}
exec(
"cd " + args.path + " && wget " + downloadLink,
(error, stdout, stderr) => {
if (error) {
console.log("❌ Error downloading " + jsonParsed.packageName);
return;
}
if (stderr) {
if (
fs.existsSync(
args.path +
"/" +
jsonParsed.packageName +
"_" +
jsonParsed.suggestedVersionCode +
".apk"
)
) {
console.log("✅ Downloaded " + jsonParsed.packageName);
}
return;
}
}
);
});
break;
default:
console.log("🛑 Invalid site! Please try: fdroid or apkmirror");
}
};
const getAPKsFromTerm = (term, site) => {
let keepLooping;
let downloaded;
let url;
let $;
switch (site) {
case "fdroid":
keepLooping = true;
downloaded = [];
for (let i = 1; i < 5; i++) {
if (!keepLooping) return;
url = `https://search.f-droid.org/?q=${term}&lang=en&page=${i}`;
rp(url)
.then(function (html) {
//success!
$ = cheerio.load(html);
for (let pack in $("a.package-header", html)) {
if ($("a.package-header", html)[pack].attribs == undefined)
return;
if (
downloaded.includes(
$("a.package-header", html)[pack].attribs.href
)
)
return;
downloaded.push($("a.package-header", html)[pack].attribs.href);
getDownloadFromLink(
$("a.package-header", html)[pack].attribs.href,
site
);
}
})
.catch(function (err) {
console.log(err);
keepLooping = false;
});
}
break;
case "apkmirror":
keepLooping = true;
downloaded = [];
for (let i = 1; i < 150; i++) {
if (!keepLooping) return;
url = `https://www.apkmirror.com/?post_type=app_release&searchtype=apk&page=${i}&s=${term}`;
cloudflareScraper
.get(url)
.then(function (html) {
//success!
$ = cheerio.load(html);
for (let pack in $(".appRowTitle a", html)) {
if ($(".appRowTitle a", html)[pack].attribs == undefined) return;
if (
downloaded.includes(
$(".appRowTitle a", html)[pack].attribs.href
)
)
return;
downloaded.push($(".appRowTitle a", html)[pack].attribs.href);
getDownloadFromLink(
$(".appRowTitle a", html)[pack].attribs.href,
site
);
}
})
.catch(function (err) {
console.log("🛑 Cloudflare Blocked Request on: " + url);
if (err.length < 1000) keepLooping = false;
});
}
break;
default:
console.log("🛑 Invalid site! Please try: fdroid or apkmirror");
break;
}
};
switch (args.website.toLowerCase()) {
case "fdroid":
console.log("👾 Going to use " + chalk.bold("FDroid"));
console.log(
"🔎 Going to try to download APK's related to: " + args.search + "..."
);
getAPKsFromTerm(args.search, args.website.toLowerCase());
break;
/*case "apkmirror":
console.log("👾 Going to use " + chalk.bold("APKMirror"));
console.log(
"🔎 Going to try to download APK's related to: " + args.search + "..."
);
getAPKsFromTerm(args.search, args.website.toLowerCase());
break;*/
default:
console.log("🛑 Invalid site! Please try: fdroid");
}