A Google Script that grabs webcam images via URL every 5 minutes and stores them in Google Drive. There is also a function that will delete old image folders after 14 days. This all can be customizable to fit your needs.
To use this script:
- Replace "YOUR_WEBCAM_IMAGE_URL_HERE" with the URL of the webcam image you want to fetch.
- Replace "YOUR_FOLDER_ID_HERE" in both functions with the ID of the folder in your Google Drive where you want to store the images.
- Run the setUpTrigger() function once to set up the trigger for fetching images every 5 minutes.
- Run the setUpDeleteTrigger() function once to set up the trigger for deleting old images daily.
Please note that you need to enable the "Google Drive API" in your Google Apps Script project for this script to work properly. Also, make sure that the folder where you want to store images has appropriate permissions for the script to create and delete files.
//Grabs webcam image from the website and stores it in my Google Drive
function getWebcam() {
var d = new Date()
var timeStamp = d.getTime()
var timeZone = Session.getScriptTimeZone();
var dString = Utilities.formatDate(d, timeZone, 'yyyy-MM-dd')
var url = "YOUR_WEBCAM_IMAGE_URL_HERE"
var response = UrlFetchApp.fetch(url).getBlob()
//set file name to unix timestamp
response.setName(timeStamp)
var folder = DriveApp.getFolderById("YOUR_FOLDER_ID_HERE")
//check if folder exists
// Log the name of every folder in the user's Drive that you own and is starred.
var folders = DriveApp.getFoldersByName(dString)
if(folders.hasNext() == false){
folder = folder.createFolder(dString)
return folder.createFile(response)
}else{
folder = folders.next()
return folder.createFile(response)
}
}
//Delete 2 week old files
function cleanDrive() {
var folder = DriveApp.getFolderById("1XyO7DUh6Ty1mt27ernO0G7vtXxWQwmOQ")
var folderNames = folder.getFolders()
//timestamp for two weeks out
const MILLIS_PER_DAY = 1000 * 60 * 60 * 24
const d = new Date()
var old = new Date(d.getTime() - 14 * MILLIS_PER_DAY)
var timeZone = Session.getScriptTimeZone();
var dateString = Utilities.formatDate(old, timeZone, 'yyyy-MM-dd')
while (folderNames.hasNext()){
var folders = folderNames.next()
var fNdate = new Date(folders)
var dSdate = new Date(dateString)
if(folders < dateString){
folders.setTrashed(true)
}
}
}