LoginSignup
14
9

More than 5 years have passed since last update.

[Python]ニンテンドースイッチの在庫情報をつぶやくスクリプト

Last updated at Posted at 2017-06-13

追記 (2017/11/28)

マイニンテンドーストアで直接入荷しなくなった(予約販売のみ)のと、
レイアウトが変わってしまったので以下のスクリプトはコピペでは使えなくなりました
(ちょこっといじれば直せると思うのでがんばって

概要

標題の通り、ニンテンドースイッチの在庫を取得して入荷してたら呟くスクリプトを作る

ニンテンドースイッチほしいのに手にはいらないのでむかつくからスクリプトを作ったけどすでに便利なbotがTwitterにいたのは作ったあとに気づいた。:angry::angry::angry:

かなしみ。

めんどくさかったのでこのスクリプトはマイニンテンドーしか対応してません

というか、正直ちゃんと動くかわかんないです

だってつくってから一回も入荷してないから!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!なんで?はあ

環境

python3.5.3
pip導入済み
centos6.9

導入

いろいろインポートする

スクレイピングするときに使うライブラリ 参考:PythonでさくっとWebスクレイピングする (JavaScript読み込みにも対応しつつ)

pip install lxml
pip install selenium
pip install cssselect

Twitterでつぶやくためのやつ 参考:PythonでTwitterしてみた

pip install requests requests_oauthlib

PhantomJSを導入参考:CentOS6にyumでphantomjs 2.1.1をインストール
性的なページのスクレイピングをするときはいらないが、今回はajaxコンテンツも読みたいのでこれを使う

sudo yum install epel-release
sudo rpm -ivh http://repo.okay.com.mx/centos/6/x86_64/release/okay-release-1-1.noarch.rpm
sudo yum search all phantomjs
sudo yum install phantomjs

環境設定は以上でおわり

スクリプトを書く

switch_tenbaiya_shine.py
#coding: UTF-8
from requests_oauthlib import OAuth1Session
import json
import settings
import sys
import lxml.html
from selenium import webdriver
from datetime import datetime

# マイニンテンドーストアでのスイッチ入荷情報を取得し、在庫があればTwitterにつぶやくスクリプト

# Twitter 初期設定
twitter = OAuth1Session(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, settings.ACCESS_TOKEN, settings.ACCESS_TOKEN_SECRET)
# タイムスタンプ作成
time = datetime.now().strftime("%Y/%m/%d %H:%M:%S")


target_url = 'https://store.nintendo.co.jp/customize.html'
driver = webdriver.PhantomJS()
driver.get(target_url)
root = lxml.html.fromstring(driver.page_source)
result = root.cssselect('.customize_priceArea.item > .stock')[0]

# 売り切れの場合、実行終了
if result.text == "SOLD OUT":
        print("売り切れてる")
        sys.exit()

# 売り切れじゃない場合、ツイートする
print("入荷してる")


status_body = "マイニンテンドーストアにnintendo switch入荷しました https://store.nintendo.co.jp/customize.html " + time

params = {"status": status_body }
req = twitter.post("https://api.twitter.com/1.1/statuses/update.json",params = params)

同ディレクトリ内にTwitterのキーが記述してあるスクリプトファイルを作る

settings.py
CONSUMER_KEY = "xxxxxxx"
CONSUMER_SECRET = "xxxxxxxx"
ACCESS_TOKEN = "xxxxxx-xxxxxxx"
ACCESS_TOKEN_SECRET = "xxxxxxx"

あとはcronでもなんでも登録すれば入荷次第呟きます

なお、この記事公開後に転売屋は全員心臓麻痺になるものとする。

参考

http://qiita.com/yayohei/items/87a09d54e7e65483bb59
http://qiita.com/konojunya/items/59a68d35e44db8b87186
http://qiita.com/f-akazawa/items/5ec741f8a5cca999d7b3
http://qiita.com/beatinaniwa/items/72b777e23ef2390e13f8

追記 2017/09/13

このスクリプトを動かしているBOT

14
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9