diff --git a/.node-version b/.node-version new file mode 100644 index 0000000..6b17d22 --- /dev/null +++ b/.node-version @@ -0,0 +1 @@ +14.16.1 diff --git a/.prettierrc b/.prettierrc index 59639c5..95bf7f6 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,6 @@ { "semi": false, "arrowParens": "always", - "singleQuote": true + "singleQuote": true, + "trailingComma": "none" } diff --git a/package.json b/package.json index 42c9256..2c155f7 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "kindle-booklog-sync", "version": "1.0.0", - "main": "Code.ts", + "main": "main.ts", "repository": "github.com/ysmtegsr/kindle-booklog-sync", - "author": "@ysmtegsr", + "author": "@ega4432", "license": "MIT", "private": true, "scripts": { - "lint": "npx eslint ./**/*.ts", + "lint": "eslint ./src/**/*.ts", "push": "clasp push", "watch": "clasp push --watch" }, diff --git a/src/main.ts b/src/main.ts index cc725a6..b705dcd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,19 +1,24 @@ +type Headers = { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [key: string]: any +} + const booklogId: string = PropertiesService.getScriptProperties() - .getProperty('BOOKLOG_ID') + .getProperty('BOOKLOG_ID') || "" const booklogPassword: string = PropertiesService.getScriptProperties() - .getProperty('BOOKLOG_PASSWORD') + .getProperty('BOOKLOG_PASSWORD') || "" -const logSheet: Sheet = SpreadsheetApp.getActiveSpreadsheet() +const logSheet = SpreadsheetApp.getActiveSpreadsheet() .getSheetByName('log') -const searchEmail = (): GmailThread[] => { +const searchEmail = () => { return GmailApp.search("from:digital-no-reply@amazon.co.jp in:inbox") } const loginToBooklog = (): string[] => { const url = 'https://booklog.jp/login' - const res = UrlFetchApp.fetch(url, { + const params = { method: 'post', followRedirects: false, headers: { Referer: url }, @@ -21,30 +26,36 @@ const loginToBooklog = (): string[] => { account: booklogId, password: booklogPassword } - }) - - const headers = res.getAllHeaders() + } as GoogleAppsScript.URL_Fetch.URLFetchRequestOptions + const res = UrlFetchApp.fetch(url, params) - if (typeof headers['Set-Cookie'] === 'undefined') { - return [] - } + const headers = res.getAllHeaders() as Headers - const cookies = headers['Set-Cookie'] === 'string' + if ('Set-Cookie' in headers) { + const cookies = headers['Set-Cookie'] === 'string' ? [headers['Set-Cookie']] : headers['Set-Cookie'] - for (let i = 0; i < cookies.length; i++) { - cookies[i] = cookies[i].split(';')[0] - } + for (let i = 0; i < cookies.length; i++) { + cookies[i] = cookies[i].split(';')[0] + } - return cookies + return cookies + return [] + } else { + return [] + } } -const getAsinList = (thread: GmailThread): string[] => { +const getAsinList = (thread: GoogleAppsScript.Gmail.GmailThread): string[] => { const asinList = thread.getMessages()[0] .getBody() .match(/dp%2F.{10}/g) + if (!asinList) { + return [] + } + for (let i = 0; i < asinList.length; i++) { asinList[i] = asinList[i].slice(-10) } @@ -52,7 +63,7 @@ const getAsinList = (thread: GmailThread): string[] => { return asinList } -const uploadBook = (cookies: string[], asinList: string[]): HTTPResponse => { +const uploadBook = (cookies: string[], asinList: string[]): GoogleAppsScript.URL_Fetch.HTTPResponse => { const url = 'https://booklog.jp/input' return UrlFetchApp.fetch(url, { @@ -70,7 +81,9 @@ const uploadBook = (cookies: string[], asinList: string[]): HTTPResponse => { } const log = (asin: string, text: string): void => { - logSheet.appendRow([new Date(), asin, text]) + if (logSheet && text) { + logSheet.appendRow([new Date(), asin, text]) + } } export const main = (): void => { @@ -88,7 +101,7 @@ export const main = (): void => { return } - threads.forEach((thread: GmailThread) => { + threads.forEach((thread: GoogleAppsScript.Gmail.GmailThread): void => { const asinList = getAsinList(thread) if (typeof asinList !== 'undefined') { Logger.log('asin is: ' + asinList.join(', ')) @@ -97,9 +110,14 @@ export const main = (): void => { const results = res.getContentText() .match(/.*tc(?:pink|blue) t10M.*/g) - results.forEach((result: string) => { - log(asinList.join(', '), result.match(/>(.*) { + const text = result.match(/>(.*)