Last active
July 23, 2017 17:21
-
-
Save tomotomo9696/b125074890d9a7ee268d6cee7213b82e to your computer and use it in GitHub Desktop.
nodeを理解してない人が書いたクソコード
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
const twitter = require('twitter'); | |
const phantom = require('phantom'); | |
require('date-utils'); | |
var client = new twitter({ | |
consumer_key: 'RCGI0SmrWovkd17fY5EEesfha', | |
consumer_secret: 'cPJB6f4rxSQoMUbCMvu5MhFFzDge3QWqLEPuWujm4cJVQGcHXB', | |
access_token_key: '732170942-ynGRcxDvtQcZxqGorIcKJUZaT7rRZEgSZHNWJeQl', | |
access_token_secret: 'YkgAP12cSx5sRJ3KiTmx0hgI9NyQfYcBrcg25ZRHNa0U0', | |
}); | |
const tweet = t => { | |
client.post('statuses/update', {status: `Switchの在庫復活した可能性が微レ存 ( ${t} ) \nhttps://store.nintendo.co.jp/customize.html`}); | |
} | |
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec)); | |
(async () => { | |
const instance = await phantom.create(); | |
const page = await instance.createPage(); | |
while(1){ | |
const status = await page.open('https://store.nintendo.co.jp/customize.html'); | |
const stock = await page.evaluate(function(){ | |
return document.querySelector("#HAC_S_KAYAA > .stock").innerHTML; | |
}); | |
let time = new Date().toFormat("YYYY/MM/DD HH24時MI分SS秒"); | |
console.log("%s : %s / %s", status, stock, time); | |
if(stock !== "SOLD OUT") tweet(time); | |
await sleep(1000 * 60 * 10); | |
} | |
await instance.exit(); | |
})(); |
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
const twitter = require('twitter'); | |
const phantom = require('phantom'); | |
require('date-utils'); | |
var client = new twitter({ | |
consumer_key: '', | |
consumer_secret: '', | |
access_token_key: '', | |
access_token_secret: '', | |
}); | |
const tweet = function(t){ | |
client.post('statuses/update', {status: `Switchの在庫復活した可能性が微レ存 ( ${t} ) \nhttps://store.nintendo.co.jp/customize.html`}, | |
function(error, tweet, response){}); | |
} | |
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec)); | |
(async function() { | |
const instance = await phantom.create(); | |
const page = await instance.createPage(); | |
while(1){ | |
const status = await page.open('https://store.nintendo.co.jp/customize.html'); | |
await page.evaluate(function() { | |
return document.querySelector("#HAC_S_KAYAA > .stock").innerHTML; | |
}).then(function(stock){ | |
let time = new Date().toFormat("YYYY/MM/DD HH24時MI分SS秒"); | |
console.log("%s : %s / %s", status, stock, time); | |
if(stock !== "SOLD OUT") tweet(time); | |
}); | |
await sleep(1000 * 60 * 10); | |
} | |
await instance.exit(); | |
}()); |
https://gist.github.com/tomotomo9696/b125074890d9a7ee268d6cee7213b82e#file-kusocode-js-L31
無理にasync/await使わず、callbackで書いても良いかと思う。
page.evaluate(() => {
return (...);
});
https://gist.github.com/tomotomo9696/b125074890d9a7ee268d6cee7213b82e#file-kusocode-js-L31
sleep使わずに、実行後にsetTimeoutで、次の実行を仕込めば良さそう。
自分の環境では、hantomjsがちゃんと動いてくれないので、試せてない。
npm init から package.json 生成して、環境を共有する方法を知っておくと良い
npmでnode.jsのpackageを管理する - Qiita
ありがとうございます。
page.evaluate(() => { return (...); });
にすると PhantomJS は arrow関数対応してないぞ的な文句を言われますた。。。
そもそもasync/await使っているのはphantomjs-nodeのサンプルがそうだったからせっかくの機会だし勉強しようかとおもいまして。
require('date-utils');
const phantom = require('phantom');
const NINTENDO_SWITCH_STORE = 'https://store.nintendo.co.jp/customize.html';
const INTERVAL_MS = 10 * 1000;
const makeMessage = (status,stock) => {
const time = new Date().toFormat("YYYY/MM/DD HH24時MI分SS秒");
console.log("%s : %s / %s", status, stock, time);
// client.post('statuses/update',
// {
// status: 'Switchの在庫復活した可能性が微レ存 ( ${time} ) \nhttps://store.nintendo.co.jp/customize.html'
// },
// (error, tweet, response) => {
// console.log('エラー');
// });
}
const check = async function() {
const instance = await phantom.create();
const page = await instance.createPage();
const status = await page.open(NINTENDO_SWITCH_STORE);
const stock = await page.evaluate(function() {
return document.querySelector('#HAC_S_KAYAA > .stock').innerHTML;
});
makeMessage(status, stock);
await instance.exit();
setTimeout(check, INTERVAL_MS);
};
check();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
構文について
もうはじめよう、ES6 ECMAScript6の基本構文まとめ(JavaScript) - Qiita
async/await について
Node 7.6、async/awaitをデフォルトでサポート