Last active
August 1, 2021 04:05
-
-
Save freddiefujiwara/50e0437b51dfbe3706eca3066661c80b to your computer and use it in GitHub Desktop.
Sync ROOM and Google Spread Sheet + API provider
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
| { | |
| "timeZone": "America/New_York", | |
| "dependencies": {}, | |
| "exceptionLogging": "STACKDRIVER", | |
| "runtimeVersion": "V8", | |
| "webapp": { | |
| "executeAs": "USER_DEPLOYING", | |
| "access": "ANYONE_ANONYMOUS" | |
| } | |
| } |
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
| const SPREADSHEET_ID = "1yxnHhfAT2cEF50hvajHoLMsW_sf7JlxnjFKeDFLNAkc"; | |
| const sheet = SpreadsheetApp.openById(SPREADSHEET_ID); | |
| const room = sheet.getSheetByName("room"); | |
| const roomLastRaw = room.getLastRow(); | |
| const workman = sheet.getSheetByName("workman"); | |
| const workmanLastRaw = workman.getLastRow(); | |
| const variety = sheet.getSheetByName("variety"); | |
| const varietyLastRaw = variety.getLastRow(); | |
| function crawlRoom() { | |
| room.getRange(`A2:G${roomLastRaw}`).clear(); | |
| let row = 2; | |
| const collections = JSON.parse(UrlFetchApp.fetch('https://room.rakuten.co.jp/api/1000000000182401/collections')); | |
| collections.data.forEach(collection => { | |
| const genre = collection.name; | |
| const collects = JSON.parse(UrlFetchApp.fetch(`https://room.rakuten.co.jp/api/${collection.id}/collects?limit=100`)); | |
| collects.data.forEach(collect => { | |
| const data = JSON.parse(UrlFetchApp.fetch(`https://app.rakuten.co.jp/services/api/IchibaItem/Search/20170706?format=json&affiliateId=0ca3304d.a811038d.0ca3304e.80024f1e&applicationId=1070485698719039334&itemCode=${collect.item.key.replace(';','%3A')}`)); | |
| if (data.Items && data.Items[0] && data.Items[0].Item) { | |
| collect.item.name = data.Items[0].Item.itemName; | |
| collect.item.url = data.Items[0].Item.affiliateUrl; | |
| collect.item.price = data.Items[0].Item.itemPrice; | |
| } | |
| room.getRange(row, 1).setValue(collect.item.name); // title | |
| room.getRange(row, 2).setValue(collect.item.picture.url); // image | |
| room.getRange(row, 3).setValue(collect.item.url); // url | |
| room.getRange(row, 4).setValue(collect.item.price); // price | |
| room.getRange(row, 5).setValue(1); // unit | |
| room.getRange(row, 6).setValue(collection.name); // genre | |
| room.getRange(row, 7).setValue(collect.content); // comment | |
| Logger.log(collect.item.name); | |
| row++; | |
| }); | |
| }); | |
| } | |
| function doGet(e) { | |
| let result = []; | |
| ["room", "workman", "variety", "others"].forEach(tab => { | |
| const target = sheet.getSheetByName(tab); | |
| const values = target.getDataRange().getValues(); | |
| const headers = values.shift(); | |
| result = result.concat(values.map((row) => { | |
| let data = {}; | |
| row.map((column, index) => { | |
| data[headers[index]] = column; | |
| }); | |
| return data; | |
| })); | |
| }); | |
| const output = ContentService.createTextOutput(); | |
| if (e.parameter.callback === undefined) { | |
| output.setMimeType(ContentService.MimeType.JSON); | |
| output.setContent(JSON.stringify(result)); | |
| } else { | |
| output.setMimeType(ContentService.MimeType.JAVASCRIPT); | |
| output.setContent(e.parameter.callback + "&&" + e.parameter.callback + "(" + JSON.stringify(result) + ");"); | |
| } | |
| return output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment