Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user-agent bot in ParseMetaForURL #1956

Merged
merged 5 commits into from
Sep 5, 2023
Merged

add user-agent bot in ParseMetaForURL #1956

merged 5 commits into from
Sep 5, 2023

Conversation

kaitoyama
Copy link
Contributor

TwitterのOGPが表示されないのを解消する目的です。
手元で試したところ投稿やページ、ユーザーによっては表示されないものもあるようです。
ついでにカクヨムのOGPも表示されるようになったようです。

Twitter.com(x.com)のみに絞るべきか悩んだのですが、上のカクヨムの例があったので、一旦OGPのために飛ばすGETすべてにuser-agent:botをつけています。

@kaitoyama kaitoyama self-assigned this Sep 3, 2023
Copy link
Member

@motoki317 motoki317 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あとは

// case "twitter.com":

のコメントアウトを外してテストのSkipNowも
// Twitter APIが使えなくなってしまったため、関数が使えないためスキップ

外してください。

edit: ごめん、twitterの特別分岐はいらなくなったんでしたね。上のテストのSkipNowはそのままで良いです。
実際にリクエストを外部サイトに送るテストは賛否両論あるかもしれませんが、
https://github.com/traPtitech/traQ/blob/8a2db60a416f32cf022a74c4bd795d6a4c468b84/service/ogp/parser/domain_twitter_test.go
と似た内容のテストを書けると、動作確認が他の人/あとからでもしやすいかもしれないです。

return nil, nil, ErrNetwork
}

req.Header.Add("user-agent", `bot`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user-agentはアクセスしたものが何者なのかを相手に知らせる目的もあるので、可能なら既にあるこちらがわと合わせられると嬉しいですね

req.Header.Set("User-Agent", userAgent)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

書き直さなくても、同じパッケージなので単純に変数を参照できます

Suggested change
req.Header.Add("user-agent", `bot`)
req.Header.Add("User-Agent", userAgent)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Twitterがbotという文字列が入っていないとOGPを返さないようなので、少し名前を変えました

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元の変数を書き換えて、コメントしておくとよいかもですね

@kaitoyama
Copy link
Contributor Author

テスト書いたの初めてなので、変なところあると思います。指摘お願いします。

Copy link
Member

@ikura-hamu ikura-hamu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。OGPなくて困ってたので助かります。テストの部分で1個だけお願いします。

@@ -122,3 +125,39 @@ func TestExtractTitleFromNode(t *testing.T) {
assert.Equal(t, "", result)
})
}

func TestFetchTwitterOGP(t *testing.T) {
tests := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他のところにもついているし、並行化しても問題ないと思うので、t.Parallel()付けていいと思います。もし意図があれば別です。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parallelにするなら、ループ変数のttをキャプチャしないように気をつけないといけないです
(ループの中にtt := ttを追加)
1.22とかで変更されそうな仕様だけど

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、僕が意図してたのはループの中じゃなくて関数の初めです。ループにも付けていいと思います。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

両方とも平行化できたと思います。e2ee810

Copy link
Member

@ikura-hamu ikura-hamu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いいと思います。マージ権限ありますか?

@kaitoyama
Copy link
Contributor Author

あるので、マージします。

@kaitoyama kaitoyama merged commit 08216f6 into master Sep 5, 2023
4 checks passed
@kaitoyama kaitoyama deleted the add-useragent-bot branch September 5, 2023 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants