Last active
November 19, 2019 20:56
-
-
Save spasiu/12dd76084932e85e026d2b0ba1db802b to your computer and use it in GitHub Desktop.
Script to rename a CSV full of Smooch apps
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
| /* | |
| How to: | |
| Run this script with node and pass filepath and username + password as command line args: | |
| node script.js "./YOUR_CSV_FILE" ACCOUNT_KEY ACCOUNT_SECRET | |
| e.g., | |
| node script.js "./apps.csv" act_5964f00fc26cb5516693ebf5 cUQIis8kGtVZNg6eWoZrvk... | |
| CSV file files should have two unlabeled columns, appId and name, e.g., | |
| 5d76820de424c8001008f2ae, sweet potato | |
| 5ca624db00a5190010da755b, russet potato | |
| ... | |
| Responses are logged as appId and status code or OK, e.g., | |
| 5ca624db00a5190010da755b OK | |
| 5d76820de424c8001008f2ae OK | |
| ... | |
| */ | |
| const fs = require('fs'); | |
| const https = require('https'); | |
| const filepath = process.argv[2]; | |
| const username = process.argv[3]; | |
| const password = process.argv[4]; | |
| const token = Buffer.from(`${username}:${password}`).toString('base64'); | |
| fs.readFileSync(filepath) | |
| .toString() | |
| .split('\n') | |
| .map(line => line.split(',')) | |
| .map(values => { | |
| const appId = values[0] && values[0].trim(); | |
| const name = values[1] && values[1].trim(); | |
| if (appId && name) { | |
| const data = JSON.stringify({ name }); | |
| const req = https.request({ | |
| hostname: 'api.smooch.io', | |
| path: `/v1.1/apps/${appId}`, | |
| method: 'PUT', | |
| headers: { | |
| 'Authorization': `Basic ${token}`, | |
| 'Content-Type': 'application/json', | |
| 'Content-Length': data.length | |
| } | |
| }, res => console.log(`${appId} ${res.statusCode == 200 ? 'OK' : res.statusCode}`)); | |
| req.on('error', error => console.error(`${appId} ${error}`)); | |
| req.write(data); | |
| req.end(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment