Skip to content

Instantly share code, notes, and snippets.

@takapiro99
Last active December 2, 2021 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takapiro99/070c0efe133fa91b11bdfa198c1826e3 to your computer and use it in GitHub Desktop.
Save takapiro99/070c0efe133fa91b11bdfa198c1826e3 to your computer and use it in GitHub Desktop.
/*
https://takapiro99.github.io/2021/rurubu-fuyuwari-coupon のコード
*/
// puppeteer, axios, form-data を npm で入れる
// 下記を crontab に登録して 10 分ごとに実行した
// */10 * * * * cd /home/takapiro/rurubu-scraper; /usr/local/bin/node ./main.js >> /home/takapiro/cron.log 2>&1
const { default: axios } = require("axios");
const puppeteer = require("puppeteer");
const FormData = require("form-data");
const fs = require("fs");
const sendMessage = async (imagePath) => {
const url = process.env.DISCORD_WEBHOOK_URL;
const data = new FormData();
data.append("image", fs.createReadStream(imagePath));
data.append(
"payload_json",
JSON.stringify({
content: imagePath.startsWith("./yes")
? "クーポンがあるよ!"
: "冬割クーポン情報",
})
);
try {
await axios.post(url, data, {
headers: {
...data.getHeaders(),
},
});
} catch (e) {
console.log(e.toString());
}
};
(async function () {
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox"],
});
const page = await browser.newPage();
await page.goto("https://www.rurubu.travel/deals", {
waitUntil: "load",
});
await page.waitForSelector(".container");
await page.waitForTimeout(2000);
const boundingClientRect = await page.evaluate(async () => {
const sleep = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
const couponCardSelector = ".CouponsGroupCard__title";
const popupSelector = `[data-selenium="coupons-popup"]`;
const couponAvailableSelector = `[data-selenium="coupon-card-popup-available"]`;
const targetCardElement = Array.from(document.querySelectorAll(couponCardSelector)).filter((ele) => ele.textContent.startsWith("【さぁ!サッポロ冬割】"))[0];
if (targetCardElement) {
targetCardElement.parentElement.parentElement.click();
await sleep(1000);
const popupElement = document.querySelector(popupSelector);
const isCouponAvailable = popupElement.querySelector(couponAvailableSelector);
const t = popupElement.getBoundingClientRect();
return {
result: !!isCouponAvailable,
x: t.left,
y: t.top,
width: t.width,
height: t.height,
};
}
return {
result: "no",
};
});
await page.waitForTimeout(2000);
if (boundingClientRect.result === "no") {
console.log("キャンペーンおわり");
} else if (boundingClientRect.result) {
console.log("yes!");
await page.screenshot({ path: "yes.png", clip: boundingClientRect });
await sendMessage("./yes.png");
} else {
console.log("sold out");
await page.screenshot({ path: "out.png", clip: boundingClientRect });
// await sendMessage("./out.png");
}
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment