Wip post photos

This commit is contained in:
Esteban Uscanga
2016-01-11 00:44:49 +01:00
parent d964de402b
commit f071aa2d5e
3 changed files with 87 additions and 31 deletions

BIN
example/photos/jake.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

23
example/postPhoto.js Normal file
View File

@@ -0,0 +1,23 @@
'use strict'
const fs = require('fs')
const Picasa = require('../.')
const accessToken = 'ya29.ZQLb0DRxf7PzBvkok6DGoSime3lym2ZNCzFPhyU1HGzkzwXaSwffivulMXcGFcgM4x2w'
const albumId = '6236607441185654065'
const picasa = new Picasa()
fs.readFile(__dirname + '/photos/jake.jpg', (err, binary) => {
const photoData = {
title : 'Jake the dog',
summary : 'Corgis ftw!',
contentType : 'image/jpeg',
binary : binary
}
picasa.postPhoto(accessToken, albumId, photoData, (error, response) => {
console.log(error, response)
})
})

View File

@@ -12,54 +12,89 @@ const PICASA_HOST = 'https://picasaweb.google.com'
const PICASA_SCOPE = '/data'
const PICASA_API_PATH = '/feed/api/user/default'
const FETCH_AS_JSON = 'json'
function Picasa () {
this.executeRequest = executeRequest
}
Picasa.prototype.getPhotos = getPhotos
Picasa.prototype.postPhoto = postPhoto
// Auth utilities
Picasa.prototype.getAuthURL = getAuthURL
Picasa.prototype.getAccessToken = getAccessToken
function postPhoto (accessToken, albumId, photoData, callback) {
const accessTokenQuery = querystring.stringify({
alt : FETCH_AS_JSON,
access_token : accessToken
})
const photoInfoXML = `<entry xmlns="http://www.w3.org/2005/Atom">
<title>${photoData.title}</title>
<summary>${photoData.summary}</summary>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/photos/2007#photo"/>
</entry>`
const requestOptions = {
url : `${PICASA_HOST}${PICASA_SCOPE}${PICASA_API_PATH}/albumid/${albumId}?${accessTokenQuery}`,
multipart : [
{'Content-Type' : 'application/atom+xml', body : photoInfoXML},
{'Content-Type' : photoData.contentType, body : photoData.binary}
]
}
this.executeRequest('post', requestOptions, (error, body) => {
if (error) return callback(error)
const photo = getPhotoByEntry(body.entry)
callback(error, photo)
})
}
function getPhotos (accessToken, options, callback) {
const requestOptions = buildPicasaRequestOptions(accessToken, 'photo', options)
this.executeRequest('get', requestOptions, (error, body) => {
if (error) return callback(error)
const photoSchema = {
'gphoto$id' : 'id',
'gphoto$albumid' : 'album_id',
'gphoto$access' : 'access',
'gphoto$width' : 'width',
'gphoto$height' : 'height',
'gphoto$size' : 'size' ,
'gphoto$checksum' : 'checksum',
'gphoto$timestamp' : 'timestamp',
'gphoto$imageVersion' : 'image_version',
'gphoto$commentingEnabled' : 'commenting_enabled',
'gphoto$commentCount' : 'comment_count',
'content' : 'content',
'title' : 'title',
'summary' : 'summary'
}
const photos = body.feed.entry.map(entry => {
let photo = {}
Object.keys(photoSchema).forEach(schemaKey => {
const key = photoSchema[schemaKey]
if (key) photo[key] = checkParam(entry[schemaKey])
})
return photo
})
const photos = body.feed.entry.map(getPhotoByEntry)
callback(null, photos)
})
}
const photoSchema = {
'gphoto$id' : 'id',
'gphoto$albumid' : 'album_id',
'gphoto$access' : 'access',
'gphoto$width' : 'width',
'gphoto$height' : 'height',
'gphoto$size' : 'size' ,
'gphoto$checksum' : 'checksum',
'gphoto$timestamp' : 'timestamp',
'gphoto$imageVersion' : 'image_version',
'gphoto$commentingEnabled' : 'commenting_enabled',
'gphoto$commentCount' : 'comment_count',
'content' : 'content',
'title' : 'title',
'summary' : 'summary'
}
function getPhotoByEntry (entry) {
let photo = {}
Object.keys(photoSchema).forEach(schemaKey => {
const key = photoSchema[schemaKey]
if (key) photo[key] = checkParam(entry[schemaKey])
})
return photo
}
function getAuthURL (config) {
const authenticationParams = {
access_type : 'offline',
@@ -96,10 +131,8 @@ function getAccessToken (config, code, callback) {
}
function buildPicasaRequestOptions (accessToken, kind, options) {
const fetchKind = 'json'
const accessTokenParams = {
alt : fetchKind,
alt : FETCH_AS_JSON,
kind : kind,
access_token : accessToken
}