getRootFolderID = (callback) ->
request = drive.about.get((err, req, res) ->
if err
callback null
else
callback res.body.rootFolderId
return
)
return
createFolder = (parentId, title, callback) ->
drive.files.insert
resource:
parents: [id: parentId]
title: title
mimeType: "application/vnd.google-apps.folder"
, (e, rq, rs) ->
return callback(null) if e or not rs.body or not rs.body.id
console.log "folder \"" + title + "\" created with id:", rs.body.id
callback rs.body
return
getOrCreateFolder = (parentId, title, callback) ->
drive.files.list
q: "title=\"" + title + "\" and mimeType=\"application/vnd.google-apps.folder\" and \"" + parentId + "\" in parents"
, (e, rq, rs) ->
for i of rs.body.items
item = rs.body.items[i]
continue if item.labels.trashed
return callback(item)
console.log "folder \"" + title + "\" does not exist, creating new one"
createFolder parentId, title, callback
return
return
uploadFileToFolder = (folder, file, callback) ->
mimeType = mime.lookup(file.fullpath)
console.log "Detected mime type:", mimeType
request =
resource:
parents: [folder]
title: file.filename
mimeType: mimeType
media:
mimeType: mimeType
body: fs.createReadStream(file.fullpath)
console.log "performing files.insert request:", request
drive.files.insert request, (e, rq, rs) ->
callback e, rs.body
return
return
uploadToGoogleDrive = (file, callback) ->
getRootFolderID (rootFolderId) ->
console.log "got root folder id:", rootFolderId
getOrCreateFolder rootFolderId, "Photos", (photosFolder) ->
unless photosFolder
console.error "failed to create /Photos"
return callback(null)
console.log "got /Photos folder id:", photosFolder.id
getOrCreateFolder photosFolder.id, "Photostream", (photostreamFolder) ->
unless photostreamFolder
console.error "failed to create /Photos/Photostream"
return callback(null)
console.log "got /Photos/Photostream folder id:", photostreamFolder.id
console.log "getting creation date for file", file.fullpath
getPhotoDate file, (date) ->
date = "Unsorted" unless date
console.log "got photo date:", date
getOrCreateFolder photostreamFolder.id, date, (dateFolder) ->
unless dateFolder
console.error "failed to create /Photos/Photostream/" + date
return callback(null)
console.log "got photostream date folder with id:", dateFolder.id
uploadFileToFolder dateFolder, file, callback
return
return
return
return
return
return
extractExifCreationDate = (file, callback) ->
try
new ExifImage(
image: file.fullpath
, ((error, exifData) ->
if error
console.log "Failed to extract photo date from exif: " + error.message
callback null
else if not exifData or not exifData.exif or not exifData.exif.CreateDate
callback null
else
callback exifData.exif.CreateDate
return
).bind(this))
catch error
console.log "Error:", error.message
callback null
return
getPhotoDate = (file, callback) ->
extractExifCreationDate file, ((creationDate) ->
unless creationDate
callback creationDate
else
date = moment(creationDate, "YYYY:MM:DD HH:mm:ss")
return callback(null) unless date.isValid()
callback date.format("YYYY MM MMMM")
return
).bind(this)
return
googleapis = require("googleapis")
mime = require("mime")
fs = require("fs")
moment = require("moment")
ExifImage = require("exif").ExifImage
CLIENT_ID = "..."
CLIENT_SECRET = "..."
REDIRECT_URL = "urn:ietf:wg:oauth:2.0:oob"
SCOPE = "https://www.googleapis.com/auth/drive"
AUTH_CODE = "..."
googleDrive = require("google-drive")
TOKENS =
access_token: "..."
token_type: "Bearer"
refresh_token: "..."
expiry_date: ""
auth = new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL)
auth.setCredentials TOKENS
googleapis.options auth: auth
drive = googleapis.drive(version: "v2")
require("osx-photostream")().on "update", (file) ->
console.log "New image: " + file.filename
uploadToGoogleDrive file, (error, result) ->
if error
console.error "upload failed:", error
else
console.log "upload succeeded"
return
return