Created
December 15, 2016 04:14
-
-
Save kateinoigakukun/eaad89b144a9a3356ca5e636e1c004eb to your computer and use it in GitHub Desktop.
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
DATABASE_ID_KEY = "table_id" | |
DATABASE_PRIMATY_COLUMN = "reportCalendarId" | |
function main() { | |
} | |
function crawler() { | |
var calendarData = parseCalendarRow(); | |
var database = new Database(); | |
calendarData.forEach(function(data) { | |
if(idValidation(data)) return | |
var sqlQuery = createSqlQuery(data); | |
Logger.log(sqlQuery); | |
database.insert(sqlQuery); | |
}) | |
} | |
function idValidation(data) { | |
return database.validation(data[DATABASE_PRIMATY_COLUMN], | |
DATABASE_PRIMATY_COLUMN); | |
} | |
function createSqlQuery(calendarData) { | |
var columns = [ | |
"reportCalendarId", "title", "eventImage", | |
"categoryId", "categoryName", "eventStartDate", | |
"eventEndDate", "entryStartDate", "entryEndDate", | |
"publishStartDate", "publishEndDate", "displayType", | |
"link", "targetLink", "priority", | |
"readFlg", "entryLabel", "entryDispFlg", | |
"eventSameDateFlg", "entrySameDateFlg", | |
"targetIcons", "eventStartDateHideFlg", | |
"eventStartTimeHideFlg", "eventEndDateHideFlg", | |
"eventEndTimeHideFlg", "entryStartDateHideFlg", | |
"entryStartTimeHideFlg", "entryEndDateHideFlg", | |
"entryEndTimeHideFlg" | |
]; | |
//29 Colums | |
var values = columns.map(function(key) { | |
//escape "value" | |
var value = calendarData[key]; | |
if(key == "eventImage") { | |
return '"' + 'https://n-3ds-pgl-contents.pokemon-gl.com' + value + '"'; | |
} | |
return '"' + value + '"' | |
}) | |
return values.join(","); | |
} | |
function parseCalendarRow() { | |
var pokemon = new PGL(); | |
var result = pokemon.getReport(0, 1, 1); | |
return result.reportInfo; | |
} | |
PGL = function() { | |
//Singleton | |
if (typeof PGL.instance === "object"){ | |
return PGL.instance; | |
} | |
} | |
PGL.prototype.baseURL = "https://3ds.pokemon-gl.com/"; | |
PGL.prototype.request = function(url, options, parameters) { | |
var param_str = jsonToQueryString(parameters); | |
var response = UrlFetchApp.fetch(url+param_str, options); | |
return JSON.parse(response); | |
} | |
PGL.prototype.getReport = function (period, languageId, countryId) { | |
var headers = {"Referer" : "https://3ds.pokemon-gl.com/calendar/"}; | |
var parameters = { | |
"languageId": languageId, | |
"countryId" : countryId, | |
"period" : period | |
}; | |
var options = { | |
"method" : "post", | |
"headers": headers | |
}; | |
var path = "frontendApi/report/getReport"; | |
return this.request(this.baseURL+path, options, parameters); | |
} | |
var Database = function() { | |
if (typeof Database.instance === "object"){ | |
return Database.instance; | |
} | |
var propertiesService = PropertiesService.getScriptProperties(); | |
this.table_id = propertiesService.getProperties()[DATABASE_ID_KEY]; | |
} | |
Database.prototype.insert = function(query) { | |
var rowsBlob = Utilities.newBlob(query, "application/octet-stream"); | |
try{ | |
FusionTables.Table.importRows(this.table_id, rowsBlob); | |
} catch(e) { | |
Logger.log(e); | |
} | |
} | |
// return Bool | |
Database.prototype.validation = function validation(primaryKey, column) { | |
var select = "SELECT ROWID FROM " + this.table_id | |
+ " WHERE " + column + "='" + primaryKey + "'"; | |
var rows = FusionTables.Query.sql(select).rows; | |
return rows != undefined | |
} | |
function testValidation() { | |
var database = new Database(); | |
var result = database.validation("0b46874e-bb96-4cf4-b39a-82fa45dd82f1","reportCalendarId"); | |
Logger.log(result); | |
} | |
//Helper | |
function jsonToQueryString(json) { | |
return '?' + | |
Object.keys(json).map(function(key) { | |
return encodeURIComponent(key) + '=' + | |
encodeURIComponent(json[key]); | |
}).join('&'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment