I made a program to notify by LINE when a switch that is difficult to obtain (?) Arrives. Here, we will target Joshin and Amazon.
--LINE developer account --Machine that runs python scripts
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.
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.
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.
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