-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<jx3>[feat]update 咸鱼 weibo subscribe
- Loading branch information
1 parent
106a8d6
commit d12011f
Showing
2 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |