-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocket-archiver.js
61 lines (49 loc) · 1.97 KB
/
pocket-archiver.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
//Configure CONSUMER_KEY and ACCESS_TOKEN in the .env file. dotenv will import them here.
import * as dotenv from 'dotenv';
dotenv.config();
//Node.js does not support the fetch() method. To run this program in Node.js, node-fetch is imported and a package.json file is provided with "module" type configured.
import fetch from 'node-fetch';
//This sets the imported keys as variables.
const {CONSUMER_KEY, ACCESS_TOKEN} = process.env;
//This variable stores the root of the API url for use in the following functions.
const pocketBaseUrl = 'https://getpocket.com/v3';
//The getIds function retrieves a list of all item IDs from Pocket.
const getIds = async () => {
const retrieveEndpoint = '/get';
const requestParams = `?consumer_key=${CONSUMER_KEY}&access_token=${ACCESS_TOKEN}`;
const urlToFetch = `${pocketBaseUrl}${retrieveEndpoint}${requestParams}`;
try {
const response = await fetch(urlToFetch);
if (response.ok) {
const jsonResponse = await response.json();
return Object.keys(jsonResponse.list);
}
} catch(error) {
console.log(error);
}
}
const ids = await getIds();
//This section renders the list of IDs that we will archive in the required JSON format for the Pocket API.
const actionsArray = [];
for (let i = 0; i < ids.length; i++) {
actionsArray.push({
"action": "archive",
"item_id": ids[i]
})
}
const jsonActionsArray = JSON.stringify(actionsArray);
//The archive function archives all items retrieved from Pocket.
const archive = async () => {
const modifyEndpoint = '/send';
const requestParams = `?consumer_key=${CONSUMER_KEY}&access_token=${ACCESS_TOKEN}&actions=${jsonActionsArray}`;
const urlToFetch = `${pocketBaseUrl}${modifyEndpoint}${requestParams}`;
try {
const response = await fetch(urlToFetch);
if (response.ok) {
console.log(response);
}
} catch(error) {
console.log(error);
}
}
await archive();