First Commit
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
function processSpecificFolder() {
|
||||
const DATED_FOLDER_ID = "";
|
||||
const API_URL = "https://pft2pftr.williambr.uno/process-pdf";
|
||||
|
||||
const datedFolder = DriveApp.getFolderById(DATED_FOLDER_ID);
|
||||
processPdfFilesInFolder(datedFolder, API_URL);
|
||||
}
|
||||
|
||||
function processNewPdfs() {
|
||||
// 1. Configuration
|
||||
const ACTIVE_PROVIDERS_FOLDER_ID = "12Ppr39dN-mdzRKMxBhABgFkWmfMX_f7O";
|
||||
const API_URL = "https://pft2pftr.williambr.uno/process-pdf";
|
||||
|
||||
// Set cutoff date to May 1, 2026
|
||||
const CUTOFF_DATE = new Date(2026, 5, 1); // Note: Month is 0-indexed (4 = May)
|
||||
|
||||
const activeProvidersFolder = DriveApp.getFolderById(ACTIVE_PROVIDERS_FOLDER_ID);
|
||||
const providerFolders = activeProvidersFolder.getFolders();
|
||||
|
||||
// 2. Loop through Provider Folders (e.g., NP jeong, Dr Santos)
|
||||
while (providerFolders.hasNext()) {
|
||||
const providerFolder = providerFolders.next();
|
||||
Logger.log("Scanning provider folder: " + providerFolder.getName());
|
||||
|
||||
const dateFolders = providerFolder.getFolders();
|
||||
|
||||
// 3. Loop through Date Folders (e.g., 05.21.2026)
|
||||
while (dateFolders.hasNext()) {
|
||||
const dateFolder = dateFolders.next();
|
||||
const folderName = dateFolder.getName();
|
||||
const folderDate = parseFolderNameAsDate(folderName);
|
||||
|
||||
// Skip if the folder name is not a valid date format
|
||||
if (!folderDate) {
|
||||
Logger.log(" Skipping folder (Not a valid MM.DD.YYYY date): " + folderName);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 4. Date Filter: Check if folder is 05.01.2026 or newer
|
||||
if (folderDate >= CUTOFF_DATE) {
|
||||
Logger.log(" Processing valid date folder: " + folderName);
|
||||
processPdfFilesInFolder(dateFolder, API_URL);
|
||||
} else {
|
||||
Logger.log(" Skipping older folder: " + folderName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Converts "05.21.2026" string format into a real JavaScript Date object
|
||||
function parseFolderNameAsDate(folderName) {
|
||||
const parts = folderName.split(".");
|
||||
if (parts.length !== 3) return null;
|
||||
|
||||
const month = parseInt(parts[0], 10) - 1; // 0-indexed months
|
||||
const day = parseInt(parts[1], 10);
|
||||
const year = parseInt(parts[2], 10);
|
||||
|
||||
if (isNaN(month) || isNaN(day) || isNaN(year)) return null;
|
||||
|
||||
return new Date(year, month, day);
|
||||
}
|
||||
|
||||
// Processes the actual PDF files inside the matching date folders
|
||||
function processPdfFilesInFolder(folder, apiUrl) {
|
||||
const files = folder.getFiles();
|
||||
|
||||
while (files.hasNext()) {
|
||||
const file = files.next();
|
||||
|
||||
if (file.getMimeType() === MimeType.PDF) {
|
||||
if (file.getDescription() === "API_REPORT_PROCESSED") {
|
||||
Logger.log("Skipping " + file.getName() + " - already processed.");
|
||||
continue; // Skip to the next file
|
||||
}
|
||||
sendAndOverwritePdf(file, apiUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPdfPageCount(file) {
|
||||
try {
|
||||
const blob = file.getBlob();
|
||||
const data = blob.getDataAsString();
|
||||
const matches = data.match(/\/Type\s*\/Pages[\s\S]*?\/Count\s*(\d+)/g);
|
||||
|
||||
if (matches && matches.length > 0) {
|
||||
const countMatch = matches[matches.length - 1].match(/\/Count\s*(\d+)/);
|
||||
if (countMatch) {
|
||||
return parseInt(countMatch, 10);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
} catch (e) {
|
||||
Logger.log("Error reading page count for " + file.getName() + ": " + e.message);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
function sendAndOverwritePdf(file, apiUrl) {
|
||||
const fileId = file.getId();
|
||||
const fileName = file.getName();
|
||||
|
||||
try {
|
||||
const pdfBlob = file.getBlob();
|
||||
const formData = { "file": pdfBlob };
|
||||
const options = {
|
||||
"method": "POST",
|
||||
"payload": formData,
|
||||
"muteHttpExceptions": true
|
||||
};
|
||||
|
||||
Logger.log(" Sending " + fileName + " to API...");
|
||||
const response = UrlFetchApp.fetch(apiUrl, options);
|
||||
|
||||
if (response.getResponseCode() !== 200) {
|
||||
Logger.log(" API Error for " + fileName + ": " + response.getContentText());
|
||||
return;
|
||||
}
|
||||
|
||||
const responseBlob = response.getBlob();
|
||||
if (responseBlob.getContentType() !== MimeType.PDF) {
|
||||
Logger.log(" Skipping overwrite. API did not return a valid PDF.");
|
||||
return;
|
||||
}
|
||||
|
||||
const updateUrl = "https://www.googleapis.com/upload/drive/v3/files/" + fileId + "?uploadType=media";
|
||||
const updateOptions = {
|
||||
"method": "PATCH",
|
||||
"headers": {
|
||||
"Authorization": "Bearer " + ScriptApp.getOAuthToken(),
|
||||
"Content-Type": MimeType.PDF
|
||||
},
|
||||
"payload": responseBlob.getBytes()
|
||||
};
|
||||
|
||||
UrlFetchApp.fetch(updateUrl, updateOptions);
|
||||
file.setDescription("API_REPORT_PROCESSED");
|
||||
Logger.log(" Successfully processed and overwritten: " + fileName);
|
||||
|
||||
} catch (e) {
|
||||
Logger.log(" Failed to process file " + fileName + ": " + e.message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user