Due to the blocking of Tencent Cloud and Alibaba Cloud by a certain learning platform, I dug out the antique Raspberry Pi 3B that I bought in 2015 to run scripts.
Since my Raspberry Pi is not powered 24/7, I cannot guarantee that it will be permanently online, so I want my script to automatically start when the Raspberry Pi boots up.
And I want my script to notify me only when there is a check-in through Server Chan.
Flashing the System#
To be honest, SD cards are rare nowadays. I searched everywhere at home and finally found a random 8GB card. It works, so I can use it. Flashing the system on the Raspberry Pi is really convenient, just use the official software.
Getting Rid of Python 2 and Switching to Python 3#
Just when I thought I could run the script directly, I realized that the Raspberry Pi defaults to Python 2. So the second step is to get rid of Python 2 and switch to Python 3. Run the following commands in the terminal:
sudo apt remove python # Uninstall Python 2
sudo apt autoremove # Clean up Python 2
sudo apt install python3 # Python 3 is usually already installed in the system, this step can be skipped
sudo ln -s /usr/bin/python3.7 /usr/bin/python # Create a new link pointing to Python 3
Clone the Script#
git clone https://hub.fastgit.org/mkdir700/chaoxing_auto_sign.git # Using GitHub acceleration source
Configure and Test the Script#
Go to chaoxing_auto_sign/local/config.py
to configure the script.
Then in the terminal, navigate to {your path}/chaoxing_auto_sign/local/
and use python main.py timing
to run the script for testing.
Everything is ready, let's move on to the main part of this article.
Install Screen#
Run the following command in the terminal:
sudo apt install screen
Automatically Run the Script on Startup#
Create a file named start.sh
in /home/pi/Desktop/
for easy editing and searching. The content is as follows:
#!/bin/sh
CreateScreen()
{
screen -dmS $1
screen -x -S $1 -p 0 -X stuff "$2"
screen -x -S $1 -p 0 -X stuff '\n'
}
CreateScreen "chaoxing" "/home/pi/Desktop/chaoxing.sh"
Create a file named chaoxing.sh
in /home/pi/Desktop/
for easy editing and searching. The content is as follows:
#!/bin/sh
cd {your path}/chaoxing_auto_sign/local/
python main.py timing
Run the following command in the terminal:
sudo nano /etc/rc.local
Insert the following code above exit 0
to make the system automatically run start.sh
on startup:
su pi -c "exec /home/pi/Desktop/start.sh"
After editing, press the key combination Ctrl+O
and press Enter to save. Then you can restart the Raspberry Pi. After restarting, enter the following command in the terminal:
screen -r chaoxing
to check if the script is running properly.
(Extension) Notify Only When There is a Check-in via Server Chan#
September 17, 2021: I don't really understand Python, but it seems that even after making changes, it won't notify even if the check-in is successful.
By default, the script sends notifications every time it runs, which is very annoying. So I made this modification.
Modify the code in chaoxing_auto_sign\local\message.py
to:
from datetime import datetime
import aiohttp
from config import SERVER_CHAN_SEND_KEY
async def server_chan_send(dataset):
"""Send messages via Server Chan"""
if SERVER_CHAN_SEND_KEY == '':
return
msg = ("| Account | Course Name | Check-in Time | Check-in Status |\n"
"| :----: | :----: | :------: | :------: |\n")
msg_template = "| {} | {} | {} | {} |"
for datas in dataset:
if datas:
for data in datas:
msg += msg_template.format(data['username'], data['name'], data['date'], data['status'])
params = {
'title': msg,
'desp': msg
}
async with aiohttp.ClientSession() as session:
async with session.request(
method="GET",
url="https://sctapi.ftqq.com/{}.send?title=messagetitle".format(SERVER_CHAN_SEND_KEY),
params=params
) as resp:
text = await resp.text()
else:
msg = "No check-in tasks at the moment!\{}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
break