feat(wangyihao): record public article URL on publish and back-fill responses
Build https://www.163.com/dy/article/<docId>.html when wangyihao tasks finish, and normalize publish-record responses + monitoring alias inputs to surface the same public URL when the doc id is known. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -79,10 +79,15 @@ func monitoringAliasOriginalURL(input *monitoringArticleAliasInput) (string, str
|
||||
if input == nil {
|
||||
return "", ""
|
||||
}
|
||||
if strings.TrimSpace(input.PlatformID) == "baijiahao" {
|
||||
switch strings.TrimSpace(input.PlatformID) {
|
||||
case "baijiahao":
|
||||
if articleID := strings.TrimSpace(monitoringStringValue(input.ExternalArticleID)); articleID != "" {
|
||||
return baijiahaoPublishedArticleURL(articleID), "high"
|
||||
}
|
||||
case "wangyihao":
|
||||
if articleID := strings.TrimSpace(monitoringStringValue(input.ExternalArticleID)); articleID != "" {
|
||||
return wangyihaoPublishedArticleURL(articleID), "high"
|
||||
}
|
||||
}
|
||||
if value := strings.TrimSpace(monitoringStringValue(input.ExternalArticleURL)); value != "" {
|
||||
return value, "high"
|
||||
|
||||
@@ -11,26 +11,12 @@ func normalizePublishRecordForResponse(item *PublishRecordResponse) {
|
||||
return
|
||||
}
|
||||
item.ErrorMessage = normalizePublishRecordErrorMessage(item.PlatformID, item.ErrorMessage)
|
||||
if strings.TrimSpace(item.PlatformID) != "baijiahao" {
|
||||
return
|
||||
}
|
||||
|
||||
articleID := ""
|
||||
if item.ExternalArticleID != nil {
|
||||
articleID = strings.TrimSpace(*item.ExternalArticleID)
|
||||
}
|
||||
if articleID == "" {
|
||||
articleID = extractBaijiahaoArticleIDFromURL(item.ExternalArticleURL)
|
||||
}
|
||||
if articleID == "" {
|
||||
articleID = extractBaijiahaoArticleIDFromURL(item.ExternalManageURL)
|
||||
}
|
||||
if articleID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
publicURL := baijiahaoPublishedArticleURL(articleID)
|
||||
item.ExternalArticleURL = &publicURL
|
||||
item.ExternalArticleURL = normalizeExternalArticleURLForPlatform(
|
||||
item.PlatformID,
|
||||
item.ExternalArticleID,
|
||||
item.ExternalArticleURL,
|
||||
item.ExternalManageURL,
|
||||
)
|
||||
}
|
||||
|
||||
func normalizePublishRecordErrorMessage(platform string, message *string) *string {
|
||||
@@ -54,6 +40,55 @@ func baijiahaoPublishedArticleURL(articleID string) string {
|
||||
return fmt.Sprintf("https://baijiahao.baidu.com/s?id=%s", url.QueryEscape(strings.TrimSpace(articleID)))
|
||||
}
|
||||
|
||||
func wangyihaoPublishedArticleURL(articleID string) string {
|
||||
return fmt.Sprintf("https://www.163.com/dy/article/%s.html", url.PathEscape(strings.TrimSpace(articleID)))
|
||||
}
|
||||
|
||||
func normalizeExternalArticleURLForPlatform(
|
||||
platform string,
|
||||
externalArticleID *string,
|
||||
externalArticleURL *string,
|
||||
externalManageURL *string,
|
||||
) *string {
|
||||
switch strings.TrimSpace(platform) {
|
||||
case "baijiahao":
|
||||
articleID := normalizeStringPointerValue(externalArticleID)
|
||||
if articleID == "" {
|
||||
articleID = extractBaijiahaoArticleIDFromURL(externalArticleURL)
|
||||
}
|
||||
if articleID == "" {
|
||||
articleID = extractBaijiahaoArticleIDFromURL(externalManageURL)
|
||||
}
|
||||
if articleID == "" {
|
||||
return externalArticleURL
|
||||
}
|
||||
publicURL := baijiahaoPublishedArticleURL(articleID)
|
||||
return &publicURL
|
||||
case "wangyihao":
|
||||
articleID := normalizeStringPointerValue(externalArticleID)
|
||||
if articleID == "" {
|
||||
articleID = extractWangyihaoArticleIDFromURL(externalArticleURL)
|
||||
}
|
||||
if articleID == "" {
|
||||
articleID = extractWangyihaoArticleIDFromURL(externalManageURL)
|
||||
}
|
||||
if articleID == "" {
|
||||
return externalArticleURL
|
||||
}
|
||||
publicURL := wangyihaoPublishedArticleURL(articleID)
|
||||
return &publicURL
|
||||
default:
|
||||
return externalArticleURL
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeStringPointerValue(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(*value)
|
||||
}
|
||||
|
||||
func extractBaijiahaoArticleIDFromURL(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
@@ -74,3 +109,38 @@ func extractBaijiahaoArticleIDFromURL(value *string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractWangyihaoArticleIDFromURL(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
trimmed := strings.TrimSpace(*value)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(trimmed)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, key := range []string{"articleId", "article_id", "docId", "docid", "postId"} {
|
||||
if id := strings.TrimSpace(parsed.Query().Get(key)); id != "" {
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
segments := strings.Split(strings.Trim(parsed.Path, "/"), "/")
|
||||
for index, segment := range segments {
|
||||
if segment != "article" && segment != "article-publish" {
|
||||
continue
|
||||
}
|
||||
if index+1 >= len(segments) {
|
||||
continue
|
||||
}
|
||||
id := strings.TrimSpace(segments[index+1])
|
||||
id = strings.TrimSuffix(id, ".html")
|
||||
if id != "" {
|
||||
return id
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -152,6 +152,12 @@ func syncDesktopPublishTaskState(
|
||||
externalArticleID := extractStringPointer(resultPayload, "external_article_id", "externalArticleId")
|
||||
externalArticleURL := extractStringPointer(resultPayload, "external_article_url", "externalArticleUrl")
|
||||
externalManageURL := extractStringPointer(resultPayload, "external_manage_url", "externalManageUrl", "review_url", "reviewUrl")
|
||||
externalArticleURL = normalizeExternalArticleURLForPlatform(
|
||||
task.Platform,
|
||||
externalArticleID,
|
||||
externalArticleURL,
|
||||
externalManageURL,
|
||||
)
|
||||
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE publish_records
|
||||
|
||||
@@ -64,6 +64,48 @@ func TestNormalizePublishRecordForResponseUsesBaijiahaoPublicURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePublishRecordForResponseUsesWangyihaoPublicURL(t *testing.T) {
|
||||
articleID := "IK5K461I05561G2M"
|
||||
item := &PublishRecordResponse{
|
||||
PlatformID: "wangyihao",
|
||||
ExternalArticleID: &articleID,
|
||||
}
|
||||
|
||||
normalizePublishRecordForResponse(item)
|
||||
|
||||
const want = "https://www.163.com/dy/article/IK5K461I05561G2M.html"
|
||||
if item.ExternalArticleURL == nil || *item.ExternalArticleURL != want {
|
||||
t.Fatalf("ExternalArticleURL = %v, want %q", item.ExternalArticleURL, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePublishRecordForResponseExtractsWangyihaoArticleIDFromURL(t *testing.T) {
|
||||
existingURL := "https://www.163.com/dy/article/IK5K461I05561G2M.html"
|
||||
item := &PublishRecordResponse{
|
||||
PlatformID: "wangyihao",
|
||||
ExternalArticleURL: &existingURL,
|
||||
}
|
||||
|
||||
normalizePublishRecordForResponse(item)
|
||||
|
||||
if item.ExternalArticleURL == nil || *item.ExternalArticleURL != existingURL {
|
||||
t.Fatalf("ExternalArticleURL = %v, want %q", item.ExternalArticleURL, existingURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitoringAliasOriginalURLUsesWangyihaoPublicURL(t *testing.T) {
|
||||
articleID := "IK5K461I05561G2M"
|
||||
got, confidence := monitoringAliasOriginalURL(&monitoringArticleAliasInput{
|
||||
PlatformID: "wangyihao",
|
||||
ExternalArticleID: &articleID,
|
||||
})
|
||||
|
||||
const want = "https://www.163.com/dy/article/IK5K461I05561G2M.html"
|
||||
if got != want || confidence != "high" {
|
||||
t.Fatalf("monitoringAliasOriginalURL() = %q, %q; want %q, high", got, confidence, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePublishStatusKeepsPartialSuccess(t *testing.T) {
|
||||
if got := normalizePublishStatus("partial_success"); got != "partial_success" {
|
||||
t.Fatalf("normalizePublishStatus(partial_success) = %q, want partial_success", got)
|
||||
|
||||
Reference in New Issue
Block a user