Nexmoe

Nexmoe

一个开发者。关于勇敢与热爱,互联网/创造/赛博朋克
twitter
github

Run a self-check-in script on Raspberry Pi.

Due to the blocking of Tencent Cloud and Alibaba Cloud by a certain learning platform, I dug out my antique Raspberry Pi 3B that I bought in 2015 to run scripts.

Since my Raspberry Pi is not powered 24/7 and cannot guarantee permanent online access, I want my script to automatically start when the Pi boots up.

And I want my script to notify me through Server Chan only when there is a check-in.

Flashing the System#

To be honest, SD cards are rare nowadays. I rummaged around at home and found a random 8GB card, but it works. Now it's really convenient to flash the system on the Raspberry Pi, 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 on the system, so 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 the Github acceleration source

Configure the Script and Run a Test#

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.

gif

Everything is ready, let's get 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 on the /home/pi/Desktop/ directory 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 on the /home/pi/Desktop/ directory 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 then 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) Only Notify Through Server Chan When There Is a Check-In#

September 17, 2021: I don't really understand Python, but it seems that even after making changes, it won't notify when 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 through 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 = "There are currently no check-in tasks!\n{}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            break
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.