-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathusage-example.ts
66 lines (56 loc) · 2.04 KB
/
usage-example.ts
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
import pc from "picocolors"
import { AudioBookSearchResult } from "../src/interface/search";
import { search } from "../src/index";
import { getAudiobook } from "../src/utils/getAudiobook";
// search params
const searchTerm = 'John Bierce';
const titleFilters = ['mage'];
const getMagnetLink = true; // set to false to skip magnet link retrieval
async function main() {
// setup variables
const maxPages = 5;
let currentPage = 0;
let searchResult = {
data: [],
pagination: { currentPage: 0, totalPages: 1 },
} as AudioBookSearchResult;
// search for audio books
while (currentPage < searchResult.pagination.totalPages && currentPage < maxPages) {
currentPage += 1;
const nextPage = await search(searchTerm.toLowerCase().trim(), currentPage);
searchResult.data = searchResult.data.concat(nextPage.data); // add results to original array
searchResult.pagination = nextPage.pagination; // update pagination
}
// apply additional filtering
if (titleFilters.length > 0) {
searchResult.data = searchResult.data.filter(book => {
// filter by title
let allMatch = true;
titleFilters.forEach(titleFilter => {
if(book.title.toLowerCase().indexOf(titleFilter.toLowerCase().trim()) < 0) {
allMatch = false;
}
});
return allMatch;
});
}
let pos = 1;
for (const item of searchResult.data) {
console.log(`${pos} - ${pc.green(item.title)} - ${pc.gray(item.id)}`)
if (getMagnetLink) {
// todo: do these in batches for faster performance
const book = await getAudiobook(item.id);
console.log('magnetUrl: ' + pc.yellow(pc.dim(book.torrent.magnetUrl)));
console.log(pc.blue(`---------------`))
}
pos += 1;
}
console.log(`${pc.yellow('Search term:')} ${searchTerm}`)
console.log(`${pc.yellow('Title Filters:')} ${titleFilters.join(',')}`)
console.log(`${pc.yellow('Result Count:')} ${searchResult.data.length}`)
}
main().then(() => {
console.log(pc.green('--Done--'));
}).catch((ex) => {
console.error(pc.red(`Error: ${ex}`))
});