Skip to content

Commit

Permalink
bug: fixed ts type of google apps script
Browse files Browse the repository at this point in the history
  • Loading branch information
ega4432 committed Jan 16, 2022
1 parent 9e5ffd9 commit 85852e2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 55 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14.16.1
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"semi": false,
"arrowParens": "always",
"singleQuote": true
"singleQuote": true,
"trailingComma": "none"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
64 changes: 41 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,69 @@
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:[email protected] 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 },
payload: {
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)
}

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, {
Expand All @@ -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 => {
Expand All @@ -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(', '))
Expand All @@ -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(/>(.*)</)[1])
})
if (results) {
results.forEach((result: string) => {
const text = result.match(/>(.*)</)
if (text) {
log(asinList.join(', '), text[1])
}
})
}

thread.markRead().moveToArchive()
} else {
Expand Down
28 changes: 0 additions & 28 deletions src/util/types.ts

This file was deleted.

0 comments on commit 85852e2

Please sign in to comment.