Last active
March 29, 2016 03:25
-
-
Save slaypni/87ff2dd22687600efac4 to your computer and use it in GitHub Desktop.
Download all photos posted by a user on Google+
This file contains hidden or 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 coffee | |
API_KEY = 'Your API Key' | |
util = require 'util' | |
path = require 'path' | |
fs = require 'fs' | |
exec = (require 'child_process').exec | |
google = require 'googleapis' | |
cheerio = require 'cheerio' | |
plus = google.plus 'v1' | |
main = -> | |
userId = process.argv[2] | |
dirpath = process.argv[3] | |
if (not userId) or (not dirpath) | |
util.print 'args: userId dirpath' | |
return | |
#plus.people.get {auth: API_KEY, userId: userId}, (err, user) -> | |
#console.log user | |
ids = {} | |
try | |
ids[item.id] = true for item in (JSON.parse fs.readFileSync path.join dirpath, 'list.json') | |
items = [] | |
loadActivitiesList = (callback) -> | |
_loadActivitiesList = (nextPageToken = undefined) -> | |
plus.activities.list {auth: API_KEY, userId: userId, collection: 'public', maxResults: 100, pageToken: nextPageToken}, (err, resp) -> | |
util.error JSON.stringify err if err | |
for item in resp.items | |
items.push item | |
callback item | |
if resp.nextPageToken | |
setTimeout -> | |
_loadActivitiesList resp.nextPageToken | |
, 500 | |
else | |
fs.writeFile (path.join dirpath, 'list.json'), JSON.stringify items | |
_loadActivitiesList() | |
loadActivitiesList (item) -> | |
return if ids.hasOwnProperty item.id | |
for attachment in item.object.attachments ? [] | |
switch attachment.objectType | |
when 'photo' | |
downloadImage attachment.fullImage.url, (attachment.id.split '.')[-1..][0] | |
when 'album' | |
[_userId, albumId] = attachment.id.split '.' | |
loadAlbumImageUrls _userId, albumId, (url, name) -> | |
downloadImage url, name | |
loadAlbumImageUrls = (userId, albumId, callback) -> | |
url = "https://picasaweb.google.com/data/feed/api/user/#{userId}/albumid/#{albumId}?imgmax=1600" | |
exec "curl '#{url}'", (error, stdout, stderr) -> | |
$ = cheerio.load stdout | |
$('entry').each (i, elem) -> | |
url = $(@).find('media\\:group media\\:content').attr 'url' | |
name = $(@).find('gphoto\\:id').text() | |
callback url, name | |
downloadImage = (url, name) -> | |
fpath = path.join dirpath, "#{name}.jpg" | |
exec "curl -o '#{fpath}' '#{url}'" | |
main() |
This file contains hidden or 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
{ | |
"name": "iggts", | |
"version": "0.1.0", | |
"private": true, | |
"dependencies": { | |
"cheerio": "^0.18.0", | |
"coffee-script": "^1.8.0", | |
"googleapis": "^1.0.21" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage