新手实践 First Practice

这里展示一些Project Podris的实际使用示例,可供大家参考哦ww
Here are some practical examples of using Project Podris for everyone to refer to.

这是一个基于Python的Project Podris情报接收示例。目标实现:通过令牌持续连接到Project Podris,并实时输出解析出的情报字典,同时使用指数退避来处理连接错误。
This is a Python-based example for receiving Project Podris intelligence. The goal is to maintain a continuous connection to Project Podris using a token, output the parsed intelligence dictionary in real-time, and handle connection errors using exponential backoff.

需要安装的Python模块 Required Python modules :

websockets
在Windows上 On Windows :
pip install websockets
pip install brotli
在Linux上 On Linux :
python3 -m venv ./.venv # 在Linux上进行Python开发时,我们推荐在虚拟环境中操作 When developing with Python on Linux, we recommend operating within a virtual environment
source .venv/bin/activate
pip install websockets
pip install brotli

接下来的程序编写请保证都在该虚拟环境下进行。Please ensure that all subsequent program development is carried out within this virtual environment.

新建一个新文件 podris_first_practice.py 并在文件内写入以下内容并保存:
Create a new file named podris_first_practice.py and write the following content into the file, then save it:

import asyncio
import websockets
import json
import base64
import brotli
import time

server = "<PROJECT PODRIS SERVER ADDRESS>"
token = "<YOUR PROJECT PODRIS TOKEN>"
def main():
    """
    This is my first practice with Project Podris in Python.
    """
    async def main():
        retry_time = 0 # 重试次数 retry time
        while True:
            try:
                async with websockets.connect(server, additional_headers=[("Authorization", f"Bearer " + token)]) as websocket:
                    while True:
                            message = await websocket.recv()
                            decompressed_data = base64.b64decode(message) # 解码 decode
                            message = json.loads(str(brotli.decompress(decompressed_data).decode('utf-8'))) # 解压 decompress
                            if "ver" in message: # 握手 handshake
                                print("[HANDSHAKE] Latency: " + str((time.time_ns() // 1000000) - message["time"]) + "ms Server Version: " + str(message["ver"]))
                                retry_time = 0 # 握手成功, 重置重试次数 reset retry time
                                continue
                            print("Got message:" + str(message))
            except Exception as e:
                print("ERROR: " + str(e))
                # 使用指数退避处理连接错误 exponential backoff handling connection errors
                if retry_time < 8: # 最大可忍受错误次数 maximum tolerated errors
                    retry_time += 1
                    print("RETRY TIME: [" + str(retry_time) + "], SLEEP " + str(2 ** retry_time) + " Sec")
                else:
                    # 到达最大可忍受错误次数 maximum tolerated errors reached
                    print("Reciever Maximum error attempts reached: [" + str(retry_time) + "], SLEEP " + str(2 ** retry_time) + " Sec")
                time.sleep(2 ** retry_time)
    asyncio.run(main())

# 运行 run
if __name__ == "__main__":
    asyncio.run(main())

笨...笨蛋!不要直接运行它!您还需要将其中的 <PROJECT PODRIS SERVER ADDRESS> 替换为您获得的Project Podris服务器地址(包含协议头)。其中的 <YOUR PROJECT PODRIS TOKEN> 替换为您获得的Project Podris令牌。
Silly... idiot! Don't run it directly! You also need to replace <PROJECT PODRIS SERVER ADDRESS> with the Project Podris server address you obtained (including the protocol header). Replace <YOUR PROJECT PODRIS TOKEN> with the Project Podris token you obtained.

完成这些之后运行它。如果一切都十分顺利,您应该会得到以下输出:
After completing these steps, run it. If everything goes smoothly, you should get the following output:

[HANDSHAKE] Latency: 1145ms Server Version: 0.1

当Project Podris分发给您新情报时,程序应该会继续输出:
When Project Podris distributes new intelligence to you, the program should continue to output:

Got message:{'msg_id': 0, 'event_id': 0, 'event_type': 'EQR', 'event_source': '中国地震台网[正式测定]', 'time': '2025-02-12 01:10:23', 'region': '西藏那曲市双湖县', 'location': [33.81, 89.15], 'magnitude': 3.9, 'mag_type': 'Ms', 'intensity': 6.0, 'int_type': 'CSIS', 'depth': 10, 'area_intensity': [], 'detail_link': 'https://www.ceic.ac.cn/'}