Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~

What to do in this article

Make sure Slack is notified when the switch sales page is updated as below

スクリーンショット 2017-07-06 22.20.49.png

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.

method

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:

Program (my_mention.py)

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)

Where I had a hard time

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 ...

Relation

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

Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~
Automatic JIRA ticket creation with slack bot ~ slack bot development with python ① ~
Write a script in Shell and Python to notify you in Slack when the process is finished
Notify Slack when the linux command finishes
Notify using Notification Center when the execution environment is macOS in Python
How to automatically notify by phone when the python system is down
Notify Slack when the machine learning process running on GCP is finished
When the log acquired by rsyslog is not updated after rotation with logrotate
[Web development with Python] query is also redirect
[Web development with Python] Precautions when saving cookies
Ask the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development with python ④ ~
[Python Data Frame] When the value is empty, fill it with the value of another column.
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
Electron is the best solution for Python multi-platform development
Read and write files with Slackbot ~ Bot development with Python ~
Check when the version does not switch with pyenv
Let's use AWS Lambda to create a mechanism to notify slack when the value monitored by CloudWatch is exceeded on Python
Use Heroku in python to notify Slack when a specific word is muttered on Twitter
The story of implementing the popular Facebook Messenger Bot with python
Unify the environment of the Python development team starting with Poetry
A memo when face is detected with Python + OpenCV quickly
[Part.2] Crawling with Python! Click the web page to move!
Create a Twitter BOT with the GoogleAppEngine SDK for Python
How to get into the python development environment with Vagrant
[Python x Zapier] Get alert information and notify with Slack
Get the source of the page to load infinitely with python.
How to deal with the problem that the current directory moves when Python is executed from Atom
The story of creating a bot that displays active members in a specific channel of slack with python
Workaround for the problem that? Is displayed when checking the version when using non-standard perl | python with pkg version