PrintWatcher/escpos-web.sh

87 lines
2.6 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
PRINTER_DEVICE="/dev/usb/lp0" # 热敏打印机设备路径
# ESC/POS 指令部分
ESC=$(echo -e "\x1b") # ESC 字符
RESET="${ESC}@"
BOLD_ON="${ESC}E\x01"
BOLD_OFF="${ESC}E\x00"
CENTER="${ESC}a\x01"
LEFT_ALIGN="${ESC}a\x00"
DOUBLE_HEIGHT="${ESC}!\x10" # 双倍高度
DOUBLE_WIDTH="${ESC}!\x20" # 双倍宽度
NORMAL_SIZE="${ESC}!\x00"
CUT_PAPER="${ESC}i" # 切纸指令
# 定义目标URL
URL="https://www.caanet.org.cn/news.mx?id=3"
LAST_URL_FILE="/volume2/docker/last_full_url.txt"
# 使用curl抓取网页内容
page_content=$(curl -s "$URL")
# 使用htmlq调用htmlq根据CSS选择器提取第一条条目的标题和相对网址
title=$(echo "$page_content" | /volume2/docker/htmlq '#AjaxList > li:nth-child(1) > a' --text)
relative_url=$(echo "$page_content" | /volume2/docker/htmlq '#AjaxList > li:nth-child(1) > a' --attribute href)
# 拼接完整网址
full_url="https://www.caanet.org.cn/$relative_url"
# 检查文件是否存在,如果不存在则创建
if [ ! -f "$LAST_URL_FILE" ]; then
touch "$LAST_URL_FILE"
fi
# 读取文件中上次的URL
last_url=$(cat "$LAST_URL_FILE")
# 比较full_url和上次的URL
if [ "$full_url" == "$last_url" ]; then
echo "条目未更新,不输出内容。"
else
# 使用curl抓取条目详情页内容
details_content=$(curl -s "$full_url")
# 调用htmlq根据CSS选择器提取内容并截取“截稿时间”向后的25个字
deadline_info=$(echo "$details_content" | /volume2/docker/htmlq '#contentWap > div.page_r' --text | sed -n 's/.*截稿时间\([^截稿时间]*\)\(.\{0,512\}\).*/\1\2/p')
# 输出结果
echo "标题: $title"
echo "相对网址: $relative_url"
echo "完整网址: $full_url"
echo "截稿时间: $deadline_info"
deadline="截稿时间""$deadline_info"
# 格式化打印内容
# 重置打印机状态
echo -ne "$RESET" > "$PRINTER_DEVICE"
# 打印五行空白
echo -e "\n\n\n" > "$PRINTER_DEVICE"
# 打印居中的加粗标题,双倍高度和宽度
echo -ne "$CENTER$BOLD_ON$DOUBLE_HEIGHT$DOUBLE_WIDTH" > "$PRINTER_DEVICE"
echo "$title" | iconv -f UTF-8 -t GBK > "$PRINTER_DEVICE"
echo -ne "$RESET" > "$PRINTER_DEVICE" # 复位到正常大小和状态
# 打印左对齐的描述
echo -ne "$LEFT_ALIGN$NORMAL_SIZE"
echo -e "\n" > "$PRINTER_DEVICE"
echo -ne "$full_url" > "$PRINTER_DEVICE"
echo -e "\n" > "$PRINTER_DEVICE"
echo "$(echo "$deadline" | iconv -f UTF-8 -t GBK)" > "$PRINTER_DEVICE"
echo -ne "$RESET" # 复位打印机状态
# 打印五行空白
echo -e "\n\n\n\n\n" > "$PRINTER_DEVICE"
# 切纸
echo -ne "$CUT_PAPER" > "$PRINTER_DEVICE"
# 更新文件中的URL
echo "$full_url" > "$LAST_URL_FILE"
fi