Run an Auto Sign-in Script on a Raspberry Pi

Nexmoe September 16, 2021
This article is an AI translation and may contain semantic inaccuracies.

Because a certain learning platform blocked Tencent Cloud and Alibaba Cloud IPs, I dug out my old Raspberry Pi 3B from 2015 to run the script.

My Pi isn’t powered 24/7, so I wanted my script to auto‑start on boot.

I also wanted Server酱 notifications only when there is a sign‑in.

Flash the system

SD cards are rare these days. I finally found a random 8G card at home—good enough. Flashing Raspberry Pi OS is easy now with the official tool.

Replace Python2 with Python3

I thought I could run the script directly, but the Pi defaulted to Python2. So step two was removing Python2 and using Python3:

sudo apt remove python # uninstall Python2
sudo apt autoremove # clean Python2
sudo apt install python3 # usually already installed
sudo ln -s /usr/bin/python3.7 /usr/bin/python # link Python to Python3

Clone the script

git clone https://hub.fastgit.org/mkdir700/chaoxing_auto_sign.git # GitHub mirror

Configure and test

Edit chaoxing_auto_sign/local/config.py.

Then in terminal: cd {your path}/chaoxing_auto_sign/local/ and run python main.py timing to test.

gif

All set—now the main part.

Install Screen

sudo apt install screen

Auto‑run on boot

Create /home/pi/Desktop/start.sh:

#!/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 /home/pi/Desktop/chaoxing.sh:

#!/bin/sh

cd {your path}/chaoxing_auto_sign/local/
python main.py timing

Edit rc.local:

sudo nano /etc/rc.local

Insert this before exit 0 to run start.sh at boot:

su pi -c "exec /home/pi/Desktop/start.sh"

Save with Ctrl+O, then reboot. After reboot, check:

screen -r chaoxing

(Optional) Notify via Server酱 only on sign‑in

2021‑09‑17: Not good at Python. After editing, it seems it doesn’t notify even when sign‑in succeeds.

By default the script notifies every run, which is too noisy. So I made this change.

Modify 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):
    """server 酱将消息推送"""
    if SERVER_CHAN_SEND_KEY == '':
        return
    
    msg = ("| 账号 | 课程名 | 签到时间 | 签到状态 |\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 = "当前暂无签到任务!\{}".format(datetime.now().strftime('%Y年%m月%d日 %H:%M:%D'))
            break