Skip to content

Instantly share code, notes, and snippets.

@freakynit
Created March 28, 2022 06:45
Show Gist options
  • Save freakynit/28eab03bd5e04c20690343ed866017d6 to your computer and use it in GitHub Desktop.
Save freakynit/28eab03bd5e04c20690343ed866017d6 to your computer and use it in GitHub Desktop.
Exports pocket items as csv compatible with raindrop.io. Exported fields: 'url', 'folder', 'title', 'description', 'tags', 'created'
import axios from 'axios'
import {createArrayCsvWriter as createCsvWriter} from 'csv-writer'
// Add your consumer key and access token.
const pocketConsumerKey = '';
const pocketAccessToken = '';
const pocketItemsFilePath = './pocket_items.csv';
const axiosConfig = {
'headers': {
'Content-Type': 'application/json'
}
};
const csvHeaders = ['url', 'folder', 'title', 'description', 'tags', 'created'];
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const csvWriter = createCsvWriter({
path: pocketItemsFilePath,
alwaysQuote: false,
append: true,
header: csvHeaders
});
const run = async () => {
await csvWriter.writeRecords(new Array(csvHeaders));
let count = 100;
let offset = 0;
while(offset != -1) {
console.log(`Fetching with count: ${count} and offset: ${offset}`);
try {
let response = await axios.post('https://getpocket.com/v3/get', {
"consumer_key": pocketConsumerKey,
"access_token": pocketAccessToken,
"state": "all",
"sort": "oldest",
"count": count,
"offset": offset,
"detailType": "complete"
}, axiosConfig);
if(response['data']['list']) {
let items = Object.values(response['data']['list']);
console.log(`Got ${items.length} items`);
if(items.length > 0) {
for (const item of items) {
let tags = '';
if (item['tags'] && Object.keys(item['tags']).length > 0) {
tags = Object.keys(item['tags']).join(",");
}
let csvRecords = new Array([
item['given_url'],
'all',
item['given_title'] || item['resolved_title'],
item['excerpt'],
`"${tags}"`,
item['time_added']
]);
await csvWriter.writeRecords(csvRecords);
}
offset += count;
await sleep(1000)
} else {
offset = -1;
}
} else {
offset = -1;
}
} catch (err) {
console.log(`Error: ${err}`);
offset = -1;
}
}
}
await run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment