fix: use browser UA for link preview fetch, add error logging to bg goroutines
This commit is contained in:
parent
9879064824
commit
41407feb58
|
|
@ -118,7 +118,7 @@ func (s *LinkPreviewService) FetchPreview(ctx context.Context, rawURL string, tr
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Sojorn/1.0; +https://sojorn.net)")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
||||
req.Header.Set("Accept", "text/html")
|
||||
|
||||
resp, err := s.httpClient.Do(req)
|
||||
|
|
|
|||
|
|
@ -740,10 +740,17 @@ func (s *OfficialAccountsService) CreatePostForAccount(ctx context.Context, conf
|
|||
}
|
||||
if linkURL != "" {
|
||||
lps := NewLinkPreviewService(s.pool)
|
||||
lp, err := lps.FetchPreview(bgCtx, linkURL, true)
|
||||
if err == nil && lp != nil {
|
||||
_ = lps.SaveLinkPreview(bgCtx, postID.String(), lp)
|
||||
log.Debug().Str("post_id", postID.String()).Str("url", linkURL).Msg("Saved link preview for official account post")
|
||||
lp, lpErr := lps.FetchPreview(bgCtx, linkURL, true)
|
||||
if lpErr != nil {
|
||||
log.Warn().Err(lpErr).Str("post_id", postID.String()).Str("url", linkURL).Msg("[OfficialAccounts] Link preview fetch failed")
|
||||
return
|
||||
}
|
||||
if lp != nil {
|
||||
if saveErr := lps.SaveLinkPreview(bgCtx, postID.String(), lp); saveErr != nil {
|
||||
log.Warn().Err(saveErr).Str("post_id", postID.String()).Msg("[OfficialAccounts] Link preview save failed")
|
||||
} else {
|
||||
log.Info().Str("post_id", postID.String()).Str("url", linkURL).Str("title", lp.Title).Msg("[OfficialAccounts] Link preview saved")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
@ -814,9 +821,16 @@ func (s *OfficialAccountsService) CreatePostForArticle(ctx context.Context, conf
|
|||
if linkURL != "" {
|
||||
lps := NewLinkPreviewService(s.pool)
|
||||
lp, lpErr := lps.FetchPreview(bgCtx, linkURL, true)
|
||||
if lpErr == nil && lp != nil {
|
||||
_ = lps.SaveLinkPreview(bgCtx, postID.String(), lp)
|
||||
log.Debug().Str("post_id", postID.String()).Str("url", linkURL).Msg("Saved link preview for official account post")
|
||||
if lpErr != nil {
|
||||
log.Warn().Err(lpErr).Str("post_id", postID.String()).Str("url", linkURL).Msg("[OfficialAccounts] Link preview fetch failed")
|
||||
return
|
||||
}
|
||||
if lp != nil {
|
||||
if saveErr := lps.SaveLinkPreview(bgCtx, postID.String(), lp); saveErr != nil {
|
||||
log.Warn().Err(saveErr).Str("post_id", postID.String()).Msg("[OfficialAccounts] Link preview save failed")
|
||||
} else {
|
||||
log.Info().Str("post_id", postID.String()).Str("url", linkURL).Str("title", lp.Title).Msg("[OfficialAccounts] Link preview saved")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
Loading…
Reference in a new issue