I made a program to notify you by LINE when switches arrive

I made a program to notify by LINE when a switch that is difficult to obtain (?) Arrives. Here, we will target Joshin and Amazon.

environment

Things necessary

--LINE developer account --Machine that runs python scripts

Preparation

First,

pip install requests
pip install beautifulsoup4
pip install line-bot-sdk

And put in the required libraries. If you do it with Raspberry pi, you may get an error if you don't write sudo at the beginning.

Next, prepare a LINE developer account. How to make it is described in detail below. A story about making a system that notifies ip when raspi starts using linebot

What we want here is a Channel Access Token and a Your userId. Copy and paste these into LINE_ACCESS_TOKEN and LINE_USER_ID in the program below.

program

zaikochecker.py


# coding: UTF-8
import re
import requests
from bs4 import BeautifulSoup
from linebot import LineBotApi
from linebot.models import TextSendMessage
from linebot.exceptions import LineBotApiError

LINE_ACCESS_TOKEN = "Paste the LINE access token"
LINE_USER_ID = "Paste your LINE user ID"
line_bot_api = LineBotApi(LINE_ACCESS_TOKEN)

#For UA camouflage
my_header = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko"
}

#Product URL
joshin_url = [
    ""
]

amazon_url = [
    ""
]

#Character string at the time of LINE notification
result_str = ""

#For Joshin
result_str = "Joshin\n"
for i in range(len(joshin_url)):
    data = requests.get(joshin_url[i], headers = my_header)
    data.encoding = data.apparent_encoding
    data = data.text
    soup = BeautifulSoup(data, "html.parser")
    try:
        detail = soup.find("form",{"name":"cart_button"}).text.encode("UTF-8")
        print(detail) #debug
        if ("Sale" in detail) == False: # Sale休止中ですとなっていなければ在庫あり
            if(i == 0) : result_str += "Neon in stock\n"
            if(i == 1) : result_str += "Gray in stock\n"
    except AttributeError:
            print("Error")

#LINE notification for Joshin
if result_str != "Joshin\n":
    try:
        line_bot_api.push_message(LINE_USER_ID, TextSendMessage(text=result_str))
    except LineBotApiError as e:
        print(e)

#For Amazon
result_str = "Amazon\n"
for i in range(len(amazon_url)):
    data = requests.get(amazon_url[i], headers = my_header)
    data.encoding = data.apparent_encoding
    data = data.text
    soup = BeautifulSoup(data, "html.parser")
    detail = soup.find("div",id="merchant-info").find("a").text
    print(detail) #debug
    if ("Amazon" in detail) == True: # Amazon.co.In stock if jp is for sale
        if(i == 0) : result_str += "Neon in stock\n"
        if(i == 1) : result_str += "Gray in stock\n"

#LINE notifications for Amazon
if result_str != "Amazon\n":
    try:
        line_bot_api.push_message(LINE_USER_ID, TextSendMessage(text=result_str))
    except LineBotApiError as e:
        print(e)

It may be a difficult program to understand because it is inefficient because it is a push-and-pull description. Simply put, this program accesses the URL of the target product and checks whether it is ready to place an order.

joshin_url and ʻamazon_url` are an array of product URLs, and the target URL is described here.

If you do not impersonate the UA at the time of access, it will be played on Amazon, so the impersonation UA is described in my_header.

How to use request and beautiful soup will come out if you google, so I will omit it. If it is Joshin, if there is a character string "Unsold" in cart_button, it means that you cannot order. In other words, if there is no word "sale" in the acquired character string, you can order. By the way, the reason why only Joshin is processed by try except is that the product page is not displayed well when accessing too much with Joshin. (Is the order button not displayed if there are many accesses from a specific IP within a certain time?) Also, if it is Amazon, the seller is written in the a tag of merchant-info, but if this is Amazon.co.jp for sale, it is judged that it is ready to order.

You can read more about LINE notifications below. LINE API Reference It should work if the correct token and ID are entered.

When the notification actually comes, it looks like this. スクリーンショット 2017-03-23 13.41.19.png

Program execution

Even if the above program is executed only once, only the state when it is executed can be obtained. Therefore, it is necessary to check the inventory at certain time intervals. Here, I use cron to check the inventory every minute.

For how to use cron, I referred to the following. [Process at a fixed time](http://make.bcde.jp/raspberry-pi/%E6%B1%BA%E3%81%BE%E3%81%A3%E3%81%9F%E6% 99% 82% E9% 96% 93% E3% 81% AB% E5% 87% A6% E7% 90% 86% E3% 81% 99% E3% 82% 8B /)

If you check every minute

*/1 * * * * python /home/hoge/zaikochecker.py

It should be done.

Summary

With this program, I was actually notified and was able to place an order safely. However, there are some problems. The first is to access every minute, so there is a risk that the server of the other party will be overloaded and played. The second is to check every minute, so if the stock is in stock for a long time, you will receive a LINE notification every minute.

I think that Yodobashi and Bic Camera can be made by the same procedure as long as you know how to use beautiful soup.

Recommended Posts

I made a program to notify you by LINE when switches arrive
I made a system that allows you to tweet just by making a phone call
I made you to execute a command from a web browser
I made a program to solve (hint) Saizeriya's spot the difference
Don't you want to say that you made a face recognition program?
I made a LINE stamp where you can study Linux commands
When you want to sort a multidimensional list by multiple lines
I made a script to display emoji
When you want a long line break
When you want to replace a column with a missing value (NaN) column by column
Allow Slack to notify you of the end of a time-consuming program process
I made a program to input what I ate and display calories and sugar
I made a library konoha that switches the tokenizer to a nice feeling
I made a program to check the size of a file in Python
I made a stamp substitute bot with line
I tried to program bubble sort by language
Send a message to LINE with Python (LINE Notify)
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I made a tool to easily display data as a graph by GUI operation.
Creating a LINE BOT to notify you of additional AtCoder contests using AWS
I made a LINE Bot with Serverless Framework!
How to remember when you forget a word
I made a module in C language to filter images loaded by Python
I made a tool to get new articles
I made a program to look up words on the window (previous development)
A story that I was addicted to when I made SFTP communication with python
I made a Caesar cryptographic program in Python.
I made a program to convert images into ASCII art with Python and OpenCV
Use AWS Lambda + LINE Notify to notify LINE not to forget your umbrella when you get home
I forgot to operate VIM, so I made a video for memorization. 3 videos by level
[AWS] I made a reminder BOT with LINE WORKS
I made a prime number generation program in Python
I made a household account book bot with LINE Bot
I made a library to separate Japanese sentences nicely
I made a script to put a snippet in README.md
I made a Python module to translate comment outs
I made a code to convert illustration2vec to keras model
I made a LINE BOT with Python and Heroku
When you want to play a game via Proxy
[Valentine special project] I made a LINE compatibility diagnosis!
I made a prime number generation program in Python 2
I made a command to markdown the table clipboard
I made a python library to do rolling rank
When you want to plt.save in a for statement
I tried to notify the honeypot report on LINE
〇✕ I made a game
I'm always impatient when ordering a cafe, so I made a React app to solve it.
I tried to automatically generate OGP of a blog made with Hugo with tcardgen made by Go
I made a class to get the analysis result by MeCab in ndarray with python
I made a program to collect images in tweets that I liked on twitter with Python
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
A story that stumbled when I made a chatbot with Transformer
I made a LINE BOT that returns parrots with Go
I want to save the photos sent by LINE to S3
I tried to notify the train delay information with LINE Notify
I made a package to filter time series with python
I wrote a program quickly to study DI with Python ①
I made a box to rest before Pepper gets tired
I made a command to generate a table comment in Django
I made a tool to create a word cloud from wikipedia
I made a function to check the model of DCGAN