myProfileHandler (#239)

Co-authored-by: Buf Generate <buf-generate@bondee.com>
This commit is contained in:
Carlo
2025-10-16 21:17:28 +08:00
committed by GitHub
parent 23f85616b4
commit 844ff8c102
5 changed files with 92 additions and 0 deletions

View File

@@ -26,6 +26,11 @@ func (u *UserProfileAction) UserProfile(ctx context.Context, userID, xsecToken s
page.MustNavigate(searchURL)
page.MustWaitStable()
return u.extractUserProfileData(page)
}
// extractUserProfileData 从页面中提取用户资料数据的通用方法
func (u *UserProfileAction) extractUserProfileData(page *rod.Page) (*UserProfileResponse, error) {
page.MustWait(`() => window.__INITIAL_STATE__ !== undefined`)
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
@@ -69,3 +74,20 @@ func (u *UserProfileAction) UserProfile(ctx context.Context, userID, xsecToken s
func makeUserProfileURL(userID, xsecToken string) string {
return fmt.Sprintf("https://www.xiaohongshu.com/user/profile/%s?xsec_token=%s&xsec_source=pc_note", userID, xsecToken)
}
func (u *UserProfileAction) GetMyProfileViaSidebar(ctx context.Context) (*UserProfileResponse, error) {
page := u.page.Context(ctx)
// 创建导航动作
navigate := NewNavigate(page)
// 通过侧边栏导航到个人主页
if err := navigate.ToProfilePage(ctx); err != nil {
return nil, fmt.Errorf("failed to navigate to profile page via sidebar: %w", err)
}
// 等待页面加载完成并获取 __INITIAL_STATE__
page.MustWaitStable()
return u.extractUserProfileData(page)
}