Skip to content

Instantly share code, notes, and snippets.

@tomotomo9696
Last active July 23, 2017 17:21
Show Gist options
  • Save tomotomo9696/b125074890d9a7ee268d6cee7213b82e to your computer and use it in GitHub Desktop.
Save tomotomo9696/b125074890d9a7ee268d6cee7213b82e to your computer and use it in GitHub Desktop.
nodeを理解してない人が書いたクソコード
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();
})();
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();
}());
@tomotomo9696
Copy link
Author

ありがとうございます。
page.evaluate(() => { return (...); });
にすると PhantomJS は arrow関数対応してないぞ的な文句を言われますた。。。

そもそもasync/await使っているのはphantomjs-nodeのサンプルがそうだったからせっかくの機会だし勉強しようかとおもいまして。

@him0
Copy link

him0 commented Jul 23, 2017

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