Created
August 5, 2024 22:34
-
-
Save kanjieater/260b37b08e7b6491914b51a9b987e724 to your computer and use it in GitHub Desktop.
agent script catalog generator
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
import { promises as fs } from 'fs'; | |
import path from 'path'; | |
import axios from 'axios'; | |
import { createObjectCsvWriter } from 'csv-writer'; | |
const ACCESS_TOKEN = 'x'; // Replace with your GitHub access token | |
const GIST_ID = '3f3901a8f132b5761dca5316fb62f702'; // Replace with your Gist ID | |
const parentDir = path.resolve('./'); // Adjust the path as needed to get one level up | |
async function getJsFiles() { | |
const files = await fs.readdir(parentDir); | |
const jsFiles = files.filter(file => file.endsWith('.js') && file.includes('_')); | |
return jsFiles.map(file => file.replace('.js', '')); | |
} | |
function processFileName(fileName) { | |
const nameParts = fileName.split('_'); | |
const processedName = nameParts.length > 1 ? nameParts.slice(1).join('_') : fileName; | |
return `${processedName} official cover`; | |
} | |
async function queryApi(fileName) { | |
const url = 'x'; | |
const processedFileName = processFileName(fileName); | |
const payload = { | |
keywords: [processedFileName], | |
total: 1, | |
count: 0, | |
params: { | |
lang: 'ja' | |
}, | |
user: 'somuser' | |
}; | |
try { | |
const response = await axios.get(url, { | |
params: { payload: JSON.stringify(payload) } | |
}); | |
const responseData = response.data; | |
const imageUrl = responseData[processedFileName] || ''; | |
return { name: fileName, url: imageUrl }; | |
} catch (error) { | |
console.error(`Error querying API for ${fileName}:`, error); | |
return { name: fileName, url: '' }; | |
} | |
} | |
async function createCsv(data, outputFile) { | |
const csvWriter = createObjectCsvWriter({ | |
path: outputFile, | |
header: [ | |
{ id: 'name', title: 'Name' }, | |
{ id: 'url', title: 'URL' } | |
] | |
}); | |
await csvWriter.writeRecords(data); | |
console.log('CSV file has been written successfully.'); | |
} | |
async function uploadGist(gistId, csvPath) { | |
const gistUrl = `https://api.github.com/gists/${gistId}`; | |
const headers = { | |
Authorization: `Bearer ${ACCESS_TOKEN}`, | |
'Content-Type': 'application/json' | |
}; | |
try { | |
const csvData = await fs.readFile(csvPath, 'utf-8'); | |
const fileName = path.basename(csvPath); | |
const response = await axios.patch(gistUrl, { | |
files: { | |
[fileName]: { content: csvData } | |
} | |
}, { headers }); | |
if (response.status === 200) { | |
console.log(`Gist updated successfully: ${response.data.html_url}`); | |
} else { | |
console.error(`Failed to update Gist: ${response.statusText}`); | |
} | |
} catch (error) { | |
console.error('Error updating Gist:', error.message); | |
} | |
} | |
async function main() { | |
try { | |
const jsFiles = await getJsFiles(); | |
const results = []; | |
for (const fileName of jsFiles) { | |
const result = await queryApi(fileName); | |
results.push(result); | |
} | |
const outputFile = 'agent_scripts.csv'; | |
await createCsv(results, outputFile); | |
await uploadGist(GIST_ID, outputFile); | |
} catch (error) { | |
console.error('An error occurred in main:', error); | |
} | |
} | |
main().catch(error => console.error('An error occurred:', error)); |
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
{ | |
"name": "images", | |
"version": "1.0.0", | |
"type": "module", | |
"scripts": { | |
"main": "node images/index.js" | |
}, | |
"license": "MIT", | |
"dependencies": { | |
"axios": "^1.7.3", | |
"csv-writer": "^1.6.0", | |
"puppeteer": "^22.15.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment