Skip to content

Instantly share code, notes, and snippets.

@slaypni
Last active March 29, 2016 03:25
Show Gist options
  • Save slaypni/87ff2dd22687600efac4 to your computer and use it in GitHub Desktop.
Save slaypni/87ff2dd22687600efac4 to your computer and use it in GitHub Desktop.
Download all photos posted by a user on Google+
#!/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()
{
"name": "iggts",
"version": "0.1.0",
"private": true,
"dependencies": {
"cheerio": "^0.18.0",
"coffee-script": "^1.8.0",
"googleapis": "^1.0.21"
}
}
@slaypni
Copy link
Author

slaypni commented Dec 16, 2015

Example usage

mkdir -p images/108158383330906277526
coffee main.coffee 108158383330906277526 ./images/108158383330906277526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment