-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
got a working script to make a gallery json
- Loading branch information
1 parent
94611ac
commit a76afb3
Showing
10 changed files
with
808 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { google } from "googleapis"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import axios from "axios"; | ||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
const drive = google.drive({ version: "v3", auth: process.env.GOOGLE_API_KEY }); | ||
|
||
const oauth2Client = new google.auth.GoogleAuth({ | ||
keyFile: "secrets/loginStuff.json", | ||
scopes: ["https://www.googleapis.com/auth/documents.readonly"], | ||
}); | ||
const docs = google.docs({ version: "v1", auth: oauth2Client }); | ||
|
||
console.log(process.env.GOOGLE_API_KEY); | ||
|
||
let galleryPath = path.join(process.cwd(), "data", "gallery", "gallery.json"); | ||
let galleryData = []; | ||
|
||
/** | ||
* | ||
* @param {string} file The full name of a file, for example: "Hello.png" | ||
* @returns {string} The file name without the extension | ||
*/ | ||
function getFileName(file = "") { | ||
return file.split(".").slice(0, -1).join("."); | ||
} | ||
/** | ||
* Takes in the goolgle drive response and returns weather its empty or not. | ||
* | ||
* True if empty. | ||
* | ||
* False if contains something. | ||
* @param {GaxiosResponse<drive_v3.Schema$FileList>} response | ||
* @returns {boolean} | ||
*/ | ||
function emptyresponse(response) { | ||
return ( | ||
!response.data || | ||
!response.data.files || | ||
response.data.files.length === 0 | ||
); | ||
} | ||
|
||
async function fetchFiles(token = "") { | ||
// List files in the public folder | ||
const response = await drive.files.list({ | ||
q: `'${process.env.GOOGLE_FOLDER_ID}' in parents AND (mimeType='image/jpeg' OR mimeType='image/jpg' OR mimeType='image/png')`, | ||
pageSize: 5, | ||
pageToken: token, | ||
key: process.env.GOOGLE_API_KEY, | ||
}); | ||
|
||
const files = response.data.files; | ||
if (files.length) { | ||
console.log("Files:"); | ||
await files.forEach(async (file) => { | ||
let data = { | ||
name: file.name || "", | ||
url: `https://lh3.googleusercontent.com/d/${file.id}`, | ||
video: ``, | ||
desc: "", | ||
}; | ||
const relatedResponse = await drive.files.list({ | ||
q: `'${ | ||
process.env.GOOGLE_FOLDER_ID | ||
}' in parents and name contains '${getFileName( | ||
file.name | ||
)}' and trashed = false`, | ||
fields: "files(id, name, mimeType)", | ||
}); | ||
relatedResponse.data.files.forEach((file) => { | ||
if ( | ||
file.mimeType.startsWith( | ||
"application/vnd.google-apps.document" | ||
) | ||
) { | ||
console.log(file.id); | ||
docs.documents | ||
.get({ | ||
documentId: file.id, | ||
fields: "body.content", | ||
}) | ||
.then((val) => { | ||
let txt = ""; | ||
val.data.body.content.forEach((el) => { | ||
if (el.paragraph && el.paragraph.elements) { | ||
el.paragraph.elements.forEach((elm) => { | ||
if (elm.textRun) { | ||
txt += elm.textRun.content; | ||
} | ||
}); | ||
} | ||
}); | ||
data.desc = txt; | ||
}); | ||
} | ||
}); | ||
galleryData.push(data); | ||
}); | ||
} else { | ||
console.log("No files found."); | ||
} | ||
if (response.data.nextPageToken) { | ||
await fetchFiles(response.data.nextPageToken); | ||
} | ||
} | ||
|
||
// Call the function to fetch images from the public folder | ||
await fetchFiles().then(); | ||
setTimeout(()=>{ | ||
fs.writeFile(galleryPath, JSON.stringify(galleryData, null, 4), (err) => { | ||
console.log(err); | ||
}); | ||
console.log("do it work") | ||
console.log("it do!!!!!!") | ||
},2000) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
[ | ||
{ | ||
"name": "IMG_1909.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1yXdIuBI9tU2PV9yap-9MHnJQEQbnBmFi", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2855.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1NxjU3IxANslShrXBdVah91EgYoY5uAHO", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2853.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1RQovm32sSGeilTmPLFV4i6p20VpU2O8B", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_0790.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1b5t6hYtXkfhj8dEyoTY-4Htrumr9ceKj", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_0861.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1VYGKcrj9PMCn8N6GBiUmSBT2jVtblF6H", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2854.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1yfIUJktV_ERyWWXfDTgHx1unO2qC_-Ox", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_0794.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1Z4C19JgnnGqwfoAdJquwkDMq9r_Y0O6N", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2882.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1EvcXYG1US4LO_nPbbTvkPIxvhhrJVAL5", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2885.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1FOLup-5KkMDrlTATgjSVyKR1h4vu195f", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2921.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1p8guaVPD4XAydAZDBT1UUvC_v-8P9nov", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2916.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/16YuNO20IzzbxJiAqbZXDaxtIjEd1mswM", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2924.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1bix7CkKi6JvIxSEytNg0wM0kA-vGZV76", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2918.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1fgCdY_amlsbdKUqyNSIby8rU1Kzw6mV7", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2923.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1EkriD6Gw1VuTYosccRJHDvl8sesS6JXe", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2920.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1I6ZOJJg_JLqoz0GekciWnfT1T13ecoTa", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_3304.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1oICUIx0C1ovO1fziRZkaZ_CpHULUP1Aq", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_3102.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1j9IEXASCVtPxaYeMx5GrMaIQcNymWNxE", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2887.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1BxN5Umf1phyUEHsGn4L6oeqHmFOpTCx8", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2928.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1bdnfVbQZcySFCFV-kjkDkJ7RZZIhAANZ", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_3104.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1WpjaonoRR4wDlpPENk6JlnD2VlTyzAdu", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2930.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1KPGAGsEjOsXWxO90bxmlIKX6UOUNj0V4", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_2926.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/14eoYfaDpeT_nchIVDEmFfQW5vxQhy0--", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_3315.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1uD10bx1DurDt7BaD66V3yxFSSyR1KJBt", | ||
"video": "", | ||
"desc": "Cool welding stuff\n" | ||
}, | ||
{ | ||
"name": "IMG_3305.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/1EKb_Ys8OVArkN1SjgxZLtnHJ7M6qmqF1", | ||
"video": "", | ||
"desc": "" | ||
}, | ||
{ | ||
"name": "IMG_3323.JPEG", | ||
"url": "https://lh3.googleusercontent.com/d/16ZU_igdLmYXCtpJzUW5ctKOrskoDaueV", | ||
"video": "", | ||
"desc": "Cool fence welding\n" | ||
} | ||
] |
Oops, something went wrong.