Skip to content

Commit

Permalink
<jx3>[feat]update 咸鱼 weibo subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
HornCopper committed Sep 9, 2024
1 parent 106a8d6 commit d12011f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/plugins/jx3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from src.tools.generate import generate

from .jx3 import *
from .parse import *

from .parse import get_registered_actions, parse_data, JX3APIOutputMsg
from .weibo import poll_weibo_api

import re
import shutil
Expand Down Expand Up @@ -62,4 +64,5 @@ async def on_startup():
headers = {
"token": Config.jx3.ws.token
}
asyncio.create_task(websocket_client(ws_url, headers))
asyncio.create_task(websocket_client(ws_url, headers))
asyncio.create_task(poll_weibo_api("2046281757", interval=600))
52 changes: 52 additions & 0 deletions src/plugins/jx3/weibo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from playwright.async_api import async_playwright
from datetime import datetime, timedelta

from nonebot.log import logger

import asyncio

def trim_to_last_period(s: str) -> str:
return s[:s.rfind("。") + 1] if "。" in s else s

def check_time(timestamp: str) -> bool:
given_time = datetime.strptime(timestamp, "%a %b %d %H:%M:%S %z %Y")
now = datetime.now(given_time.tzinfo)
time_difference = now - given_time
return time_difference > timedelta(hours=2)

async def execute_on_new_post(post):
data = trim_to_last_period(post.get("text_raw"))
logger.info({"data": data})
return {"text": data}

async def poll_weibo_api(uid, interval=60):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
logger.info("Start successfully!")
last_seen_id = None
while True:
logger.info("Running application.")
try:
await page.goto("https://weibo.com")
await page.wait_for_timeout(5000)
response = await page.goto(f"https://weibo.com/ajax/statuses/mymblog?uid={uid}")
data = await response.json() # type: ignore
if "data" in data and "list" in data["data"]:
posts = data["data"]["list"]
if posts:
latest_post = posts[0]
post_id = latest_post.get("idstr")

if last_seen_id != post_id and not check_time(latest_post.get("created_at")):
await execute_on_new_post(latest_post)
last_seen_id = post_id
else:
logger.info("No new information!")
else:
logger.info("No data!")

except Exception as e:
logger.info("Request failed!")

await asyncio.sleep(interval)

0 comments on commit d12011f

Please sign in to comment.