Make sure Slack is notified when the switch sales page is updated as below
From the conclusion, I was notified properly, but I couldn't buy it. I checked the site immediately after the sales page was updated, but immediately after the update, it was down due to access concentration, and when it was restored, it was already sold out ...
At present, it seems that it will be sold out in about 10 minutes. Does the person who can buy not only receive the page update notification, but also automate the purchasing process? I thought I would give up until the supply increased.
I thought about doing web scraping while studying, but in the end I got the content with curl -o every 30 minutes and notified slack if there was a difference from the previously acquired content. ..
It's not a very good implementation because you will be notified even if another update is made on the sales page.
This time as well, we will add it to the program created in ①. Make some modifications to the directory structure. Just create the files_dir directory directly under plugins.
Automatically create JIRA tickets with slack bot ~ slack bot development with python ① ~
slackbot #A directory that organizes programs. Any name is fine
├─ run.py #Start the bot by running this program
├─ slackbot_settings.py #File to write settings related to bot
└─ plugins #Add bot functionality to this directory
├─ __init__.py #A file to indicate the module. Sky is fine
└─ my_mention.py #Features each file. Any name is fine
└─ files_dir #Store the contents acquired by curl in this directory
├─ switch.html_1 #Content acquired with curl
└─ switch.html_2 #Content acquired with curl
└─ switch.html_3..4..5..6..
The target to edit is my_mention.py again. Add the following to the script:
import time
import subprocess
from subprocess import check_output, CalledProcessError
1 @respond_to('get_switch')
2 def get_switch(message):
3 print('get_switch_contents')
4 script_dir = os.path.abspath(os.path.dirname(__file__))
5 files_dir = os.path.join(script_dir, "files_dir")
6 file_number = 1
7 dest_url = "https://store.nintendo.co.jp/customize.html"
8 #Execute command every 30 minutes
9 sleep_time = 1800
10 try:
11 while True:
12 curl_cmd = "curl -o ./plugins/files_dir/swith.html_%s %s" %(file_number, dest_url)
13 diff_cmd = "diff -u ./plugins/files_dir/swith.html_%s ./plugins/files_dir/swith.html_%s" %((file_number - 1), file_number)
14
15 #Execute the curl command to get the content. Since there is no diff target at the first execution, skip
16 subprocess.call(curl_cmd, shell=True)
17 if file_number == 1:
18 file_number += 1
19 continue
20
21 #Check the result of diff. diff the return code_Store in result
22 diff_result = subprocess.call(diff_cmd, shell=True, stderr=subprocess. STDOUT, timeout=3, universal_newlines=True)
23 print("return code of diff command is %s" % diff_result)
24
25 if diff_result == 0:
26 if os.path.exists(os.path.join(files_dir, "swith.html_%s") %(file_number - 3)):
27 os.remove(os.path.join(files_dir, "swith.html_%s" % (file_number - 3)))
28 file_number += 1
29 time.sleep(sleep_time)
30 continue
31 else:
32 message.reply('The switch page has been updated!Urgent confirmation! https://store.nintendo.co.jp/customize.html %s' % (admin))
33 break
34 except subprocess.CalledProcessError as e:
35 print("Exception args:", e.args)
I use the subprocess library to run a shell in python, but I'm having a hard time figuring out how to receive and process the return code. If it is a shell, you can get 0 if there is no difference with echo $? After issuing the diff command, and 1 if there is ...
[root@target slackbot]# echo $?
0
[root@target slackbot]#
After all, it is implemented as follows
subprocess.call(diff_cmd, shell=True, stderr=subprocess.STDOUT ,timeout=3, universal_newlines=True)
Looking at the official document, there was a description as follows
stdout Captured stdout from the child process. A bytes sequence, or a string if run() was called with an encoding or errors. None if stdout was not captured. If the process was run with stderr = subprocess.STDOUT, a mixture of standard and standard error will be stored in this attribute and stderr will be None. https://docs.python.jp/3/library/subprocess.html
This time, the received result is stored in a variable called diff_result, if there is no difference, it is repeated with continue, the file 3 generations ago is deleted, and if there is a difference = that is, if the page is updated, it is notified by slack. I made it
However, it seems that I can't buy it for the time being ...
I think that I will be denied if I access every minute or do something with a low degree of people, so it is absolutely NG. Good sense is important ...
Automatically create JIRA tickets with slack bot ~ slack bot development with python ① ~ JIRA ticket with slack bot → Automation of Slack notification ~ slack bot development with python② ~ Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~ Get the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development ④ ~ with python
Recommended Posts