Created
October 5, 2018 10:24
-
-
Save alizbazar/170ac5d090a0d899fe7fc20269d0fd19 to your computer and use it in GitHub Desktop.
Script to backup all photos and videos from Path.com feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Backup JSON data of your Path.com feed, including own and others' photos and videos | |
// Just take connect.cid cookie from browser inspector when being logged in to Path.com and place below. | |
// Then run the script, and voila! | |
const _ = require('lodash') | |
const fs = require('fs') | |
const request = require('request-promise') | |
const cookie = '<PLACE YOUR connect.cid PATH cookie here>' | |
const getUrl = olderThan => `https://path.com/a/feed/home?ww=1920&wh=815${olderThan ? `&older_than=${olderThan}` : ''}&meId=51d7cbb6eea8e987f11674fe` | |
const getOldest = res => _.chain(res['moments']).last().get('created').value() | |
const getMoments = res => { | |
return _.pick(res, ['moments', 'momentSet', 'users', 'locations']) | |
} | |
function loadJSON (olderThan, prevOlderThan) { | |
if (olderThan && olderThan === prevOlderThan) { | |
return Promise.resolve() | |
} | |
return request({ | |
uri: getUrl(olderThan), | |
headers: { | |
Cookie: cookie, | |
}, | |
json: true, | |
}) | |
} | |
fs.writeFileSync('output.json', '[\n') | |
const appendJSON = json => fs.appendFileSync('output.json', JSON.stringify(json)) | |
const addSeparator = () => fs.appendFileSync('output.json', ',\n') | |
const endJSON = () => fs.appendFileSync('output.json', '\n]\n') | |
const wait = () => new Promise(resolve => setTimeout(resolve, Math.random() * 1000)) | |
let prevOlderThan | |
let olderThan | |
const iterate = () => { | |
process.stdout.write(`Loading olderThan ${olderThan}... `) | |
if (prevOlderThan && !olderThan) { | |
console.log('All Done!') | |
endJSON() | |
process.exit() | |
} else if (olderThan) { | |
addSeparator() | |
} | |
return loadJSON(olderThan, prevOlderThan).then(res => { | |
const moments = getMoments(res) | |
appendJSON(moments) | |
process.stdout.write(`OK\n`) | |
prevOlderThan = olderThan | |
olderThan = getOldest(res) | |
}) | |
.then(wait) | |
.then(iterate) | |
} | |
iterate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Download all Path.com media files (videos, images) specified in previously created output.json | |
const _ = require('lodash') | |
const request = require('request') | |
const data = require('./output.json') | |
const fs = require('fs') | |
const files = [] | |
const wait = () => new Promise(resolve => setTimeout(resolve, Math.random() * 1000)) | |
data.forEach(row => { | |
if (!row.moments) return | |
row.moments.forEach(d => { | |
const moment = row.momentSet[d.id] | |
// video file | |
let path = _.get(moment, ['video', 'video', 'original', 'file']) | |
if (path) { | |
const url = _.get(moment, ['video', 'video', 'download_url']) + '/' + path | |
files.push(url) | |
} | |
// photo file | |
path = _.get(moment, ['photo', 'photo', 'original', 'file']) | |
if (path) { | |
const url = _.get(moment, ['photo', 'photo', 'url']) + '/' + path | |
files.push(url) | |
} | |
}) | |
}) | |
fs.writeFileSync('index.txt', files.join('\n')) | |
const getFileName = url => url.replace(/^.+?\/([^\/]+?)\/[^\/]+\.([a-z0-9]{3,5})$/i, "$1.$2") | |
let i = 0 | |
const iterate = () => { | |
const url = files[i] | |
if (!url) { | |
console.log('All Done!') | |
process.exit() | |
} | |
i++ | |
process.stdout.write(url + '... ') | |
return new Promise(resolve => { | |
request(url) | |
.on('end', resolve) | |
.pipe(fs.createWriteStream(getFileName(url))) | |
}) | |
.then(() => { | |
process.stdout.write('OK\n') | |
}) | |
.then(wait) | |
.then(iterate) | |
} | |
iterate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment