Created
August 6, 2017 10:31
-
-
Save jeffhandley/7b4117baf632186342a12632e26ba329 to your computer and use it in GitHub Desktop.
Process your OneDrive Camera Roll into year/month folders
This file contains 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
function loadCameraRoll(url, max, done) { | |
url = url || "https://graph.microsoft.com/v1.0/me/drive/special/cameraRoll/children"; | |
max = max || 200; | |
console.log('LOADING PHOTOS'); | |
$.ajax({ | |
url: url, | |
dataType: "json", | |
headers: { "Authorization": "Bearer " + window.token }, | |
accept: "application/json", | |
success: function(data) { | |
console.log(data); | |
window.photos = (window.photos || []).concat(data.value); | |
console.log("PHOTOS", window.photos.length); | |
if (data["@odata.nextLink"] && window.photos.length < max) { | |
loadCameraRoll(data["@odata.nextLink"], max, done) | |
} else if (done) { | |
done(); | |
} | |
} | |
}); | |
} | |
function parsePhotoDateToFolder(photo) { | |
var date = new Date(photo.createdDateTime); | |
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; | |
return "/SkyDrive camera roll - " + date.getFullYear() + "/" + (date.getMonth() + 1) + " - " + monthNames[date.getMonth()]; | |
} | |
function moveToYear(photo, done) { | |
var folder = parsePhotoDateToFolder(photo); | |
console.log(folder); | |
$.ajax({ | |
method: "PATCH", | |
url: "https://graph.microsoft.com/v1.0/me/drive/items/" + photo.id, | |
headers: { "Authorization": "Bearer " + window.token, "Content-Type": "application/json" }, | |
dataType: "json", | |
data: JSON.stringify({ parentReference: { path: "/drive/root:" + folder } }), | |
success: function(data) { console.log(data); if (done) { done(); } } | |
}); | |
} | |
function moveNext(count, done) { | |
count = count || 1; | |
if (count == -1) { | |
count = photos.length; | |
} | |
var until = photos.length - count; | |
function next() { | |
console.log("REMAINING", photos.length, count - 1); | |
if (photos.length > until) { | |
moveNext(photos.length - until, done); | |
} else if (done) { | |
done(); | |
} | |
} | |
console.log("MOVING"); | |
var photo = photos.shift(); | |
if (photo && photo.image) { | |
moveToYear(photo, function() { | |
next(); | |
}); | |
} else { | |
next(); | |
} | |
} | |
function moveAll(done) { | |
for (var concurrent = 0; concurrent < 200; concurrent++) { | |
moveNext(-1, done); | |
} | |
} | |
function loadAndMoveNext(done) { | |
if (!done) { | |
done = function() { console.log("ALL DONE"); } | |
} | |
loadCameraRoll(null, 1000, moveAll.bind(null, done)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Start off by cloning this repository and getting it up and running:
https://github.com/OneDrive/onedrive-explorer-js
That requires registering an application in OneDrive, getting your Client ID, and spinning the app up to listen on the redirectUri you register.
Then, load magic.js into the browser and start using the functions.
loadAndMoveNext()
will process 1000 pictures at a time.