Created
February 27, 2021 05:57
-
-
Save step135/b8d4c6f8b53c689126a0992080cc1504 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const username = "" | |
const password = "" | |
const fs = require('fs'); | |
const libpath = require('path'); | |
const { GPhotos } = require('upload-gphotos'); | |
const gphotos = new GPhotos(); | |
const mazat=false; | |
var erroredfiles=[] | |
var args=process.argv.slice(2) | |
var dir=args.length?args[0]:null | |
var prefix=args.length>1?args[1]:null | |
if(!dir)return console.error('\x1b[36m%s\x1b[0m',"You need to specify folder where photos will be taken from!") | |
if(!fs.existsSync(dir)) return console.error('\x1b[36m%s\x1b[0m',"Folder doesn't exist!"); | |
(async () => { | |
await gphotos.signin({ | |
username, | |
password, | |
}); | |
console.log("authenticated") | |
//const album = await gphotos.searchAlbum({ title: 'TestAlbum' }); | |
//__dirname | |
fs.readdir(dir, { withFileTypes: true }, async (err, dirents) => { | |
const files = dirents | |
.filter(dirent => dirent.isFile()) | |
.map(dirent => dirent.name); | |
var i=0; | |
var l=files.length; | |
console.log("files in folder: "+l) | |
for (const file of files){ | |
if(["png","jpg"].indexOf(file.split(".").slice(-1)[0])>-1) | |
{ | |
console.log("uploading",(i+1),"file") | |
try{ | |
await upload(file) | |
console.log("for "+file,(++i)+"/"+l) | |
} | |
catch(e){ | |
console.log('\x1b[36m%s\x1b[0m','error at file '+file,e) | |
erroredfiles.push(file) | |
} | |
} | |
} | |
console.log("erroredfiles",erroredfiles) | |
/* https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop | |
files.forEach(async file => { | |
if(file.split(".").slice(-1)[0]=="jpg") | |
{ | |
console.log("uploading",(i)) | |
await upload(dir+"/"+file) | |
console.log("uploaded "+file,(++i)+"/"+l) | |
fs.unlink(dir+"/"+file, (err) => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
}) | |
} | |
});*/ | |
}); | |
//const filepath = libpath.join(dir, './example.jpg'); | |
//await album.append(photo); | |
})().catch(console.error); | |
async function upload(file){ | |
const filepath=dir+"/"+file | |
const photo = await gphotos.upload({ | |
stream: fs.createReadStream(filepath), | |
size: (await fs.promises.stat(filepath)).size, | |
filename: libpath.basename(filepath), | |
}).catch(console.error); | |
delete photo.rawUrl | |
console.log(photo); | |
console.log(photo.getInfo().width); | |
if(mazat)smaz(filepath);else movefile(file) | |
} | |
function movefile(file){ | |
var newdir=dir+"/zal" | |
var filepath=dir+"/"+file | |
if (!fs.existsSync(newdir)){ | |
fs.mkdirSync(newdir); | |
} | |
//https://stackoverflow.com/questions/8579055/how-do-i-move-files-in-node-js | |
fs.rename(filepath, newdir+"/"+file,function(e){console.log(e?e:file+" moved to zal")}) | |
} | |
function smaz(filepath){ | |
fs.unlink(filepath, (err) => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment