Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatatsu committed Oct 6, 2021
0 parents commit a6438e8
Show file tree
Hide file tree
Showing 13 changed files with 798 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID=''
PW=''
NOTIFY_URL=''
66 changes: 66 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

caps/*.png
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
5 changes: 5 additions & 0 deletions bin/syukkin
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

cd `dirname $0`/../

yarn syukkin
5 changes: 5 additions & 0 deletions bin/taikin
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

cd `dirname $0`/../

yarn taikin
Empty file added caps/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "kintai",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"syukkin": "rm -f caps/*.png && node -r dotenv/config -r esbuild-register src/syukkin.ts",
"taikin": "rm -f caps/*.png && node -r dotenv/config -r esbuild-register src/taikin.ts"
},
"devDependencies": {
"@types/node-fetch": "^3.0.3",
"@types/puppeteer": "^5.4.4",
"dotenv": "^10.0.0",
"esbuild": "^0.13.3",
"esbuild-register": "^3.0.0",
"prettier": "^2.4.1",
"typescript": "^4.4.3"
},
"dependencies": {
"node-fetch": "^3.0.0",
"puppeteer": "^10.4.0"
}
}
43 changes: 43 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import puppeteer from "puppeteer";
// import fetch from "node-fetch";

const fetch = (...args: any[]) =>
// @ts-ignore
import("node-fetch").then(({ default: fetch }) => fetch(...args));

export async function login(page: puppeteer.Page, id: string, pw: string) {
const _cap = cap(page);

// ページ表示
await page.goto("https://s2.kingtime.jp/independent/recorder/personal/");
await _cap("0-1-login1");

// 入力
await page.type("input#id", id);
await page.type("input#password", pw);
await _cap("0-2-login2");

// ログイン
await page.click(".btn-control-message");

// TOPページ表示を待つ
await page.waitForSelector("#buttons");
await _cap("0-3-top1");
}

export const cap =
(page: puppeteer.Page) =>
async (imageName: string, captureOptions?: puppeteer.ScreenshotOptions) => {
await page.screenshot({ path: `caps/${imageName}.png`, ...captureOptions });
console.info(`capture! ${imageName}`);
};

export const notify = async (text: string, url: string) => {
const body = JSON.stringify({ text });
const headers = { "Content-Type": "application/json" };

const res = await fetch(url, { method: "POST", headers, body });
if (!res.ok) {
console.warn(res.status.toString());
}
};
45 changes: 45 additions & 0 deletions src/syukkin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import puppeteer from "puppeteer";
import { login, cap, notify } from "./lib";

const { ID, PW, NOTIFY_URL } = process.env;

console.info("start");
main()
.then(() => {
console.info("end");
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});

async function main() {
if (!ID) throw new Error("環境変数ないよ ID");
if (!PW) throw new Error("環境変数ないよ PW");
if (!NOTIFY_URL) throw new Error("環境変数ないよ NOTIFY_URL");

const browser = await puppeteer.launch();
const page = await browser.newPage();
const _cap = cap(page);

try {
// ログインしてトップページの表示待つ
await login(page, ID, PW);

// 勤怠ボタン
await page.click(".record-clock-in");
await page.waitForFunction(
'document.querySelector("body").innerText.includes("出勤が完了しました")'
);
await page.waitForTimeout(1000); // wait fade-in animation
await _cap("1-0kintai");
} catch (err) {
await _cap("99-error");
throw err;
}

await browser.close();

await notify("しゅっきん", NOTIFY_URL);
}
45 changes: 45 additions & 0 deletions src/taikin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import puppeteer from "puppeteer";
import { login, cap, notify } from "./lib";

const { ID, PW, NOTIFY_URL } = process.env;

console.info("start");
main()
.then(() => {
console.info("end");
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});

async function main() {
if (!ID) throw new Error("環境変数ないよ ID");
if (!PW) throw new Error("環境変数ないよ PW");
if (!NOTIFY_URL) throw new Error("環境変数ないよ NOTIFY_URL");

const browser = await puppeteer.launch();
const page = await browser.newPage();
const _cap = cap(page);

try {
// ログインしてトップページの表示待つ
await login(page, ID, PW);

// 勤怠ボタン
await page.click(".record-clock-out");
await page.waitForFunction(
'document.querySelector("body").innerText.includes("退勤が完了しました")'
);
await page.waitForTimeout(1000); // wait fade-in animation
await _cap("1-0kintai");
} catch (err) {
await _cap("99-error");
throw err;
}

await browser.close();

await notify("たいきん", NOTIFY_URL);
}
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
}
}
Loading

0 comments on commit a6438e8

Please sign in to comment.