var googleapis require googleapis var mime require mime var fs require

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var googleapis = require('googleapis');
var mime = require('mime');
var fs = require('fs');
var moment = require('moment');
var ExifImage = require('exif').ExifImage;
var CLIENT_ID = '...';
var CLIENT_SECRET = '...';
var REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob';
var SCOPE = 'https://www.googleapis.com/auth/drive';
var AUTH_CODE = '...';
var googleDrive = require('google-drive')
var TOKENS = {
access_token: '...',
token_type: 'Bearer',
refresh_token: '...',
expiry_date: ...
};
var auth = new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
auth.setCredentials(TOKENS);
googleapis.options({
auth: auth
});
var drive = googleapis.drive({
version: 'v2'
});
function getRootFolderID(callback) {
var request = drive.about.get(function(err, req, res) {
if (err) {
callback(null);
} else {
callback(res.body.rootFolderId);
}
});
}
function createFolder(parentId, title, callback) {
drive.files.insert({
resource: {
parents: [{
id: parentId
}],
title: title,
mimeType: "application/vnd.google-apps.folder"
}
},
function(e, rq, rs) {
if (e || !rs.body || !rs.body.id) {
return callback(null);
}
console.log('folder "' + title + '" created with id:', rs.body.id);
return callback(rs.body);
});
}
function getOrCreateFolder(parentId, title, callback) {
drive.files.list({
q: 'title="' + title + '" and mimeType="application/vnd.google-apps.folder" and "' + parentId + '" in parents'
},
function(e, rq, rs) {
for (var i in rs.body.items) {
var item = rs.body.items[i];
if (item.labels.trashed) {
continue;
}
return callback(item);
}
console.log("folder \"" + title + "\" does not exist, creating new one");
createFolder(parentId, title, callback);
});
}
function uploadFileToFolder(folder, file, callback) {
var mimeType = mime.lookup(file.fullpath);
console.log("Detected mime type:", mimeType);
var 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,
function(e, rq, rs) {
callback(e, rs.body);
});
}
function uploadToGoogleDrive(file, callback) {
getRootFolderID(function(rootFolderId) {
console.log('got root folder id:', rootFolderId);
getOrCreateFolder(rootFolderId, 'Photos', function(photosFolder) {
if (!photosFolder) {
console.error('failed to create /Photos');
return callback(null);
}
console.log('got /Photos folder id:', photosFolder.id);
getOrCreateFolder(photosFolder.id, 'Photostream', function(photostreamFolder) {
if (!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, function(date) {
if (!date) {
date = 'Unsorted';
}
console.log('got photo date:', date);
getOrCreateFolder(photostreamFolder.id, date, function(dateFolder) {
if (!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);
});
});
});
});
});
}
function extractExifCreationDate(file, callback) {
try {
new ExifImage({
image: file.fullpath
}, function(error, exifData) {
if (error) {
console.log('Failed to extract photo date from exif: ' + error.message);
callback(null);
} else if (!exifData || !exifData.exif || !exifData.exif.CreateDate) {
callback(null);
} else {
callback(exifData.exif.CreateDate);
}
}.bind(this));
} catch (error) {
console.log('Error:', error.message);
callback(null);
}
}
function getPhotoDate(file, callback) {
extractExifCreationDate(file, function(creationDate) {
if (!creationDate) {
callback(creationDate);
} else {
var date = moment(creationDate, 'YYYY:MM:DD HH:mm:ss');
if (!date.isValid()) {
return callback(null);
}
return callback(date.format('YYYY MM MMMM'));
}
}.bind(this));
}
require('osx-photostream')().on('update', function(file) {
console.log('New image: ' + file.filename);
uploadToGoogleDrive(file, function(error, result) {
if (error) {
console.error('upload failed:', error);
} else {
console.log('upload succeeded');
}
});
});