-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あとは
traQ/service/ogp/parser/domain.go
Line 19 in 8a2db60
// case "twitter.com": |
// Twitter APIが使えなくなってしまったため、関数が使えないためスキップ |
edit: ごめん、twitterの特別分岐はいらなくなったんでしたね。上のテストのSkipNowはそのままで良いです。
実際にリクエストを外部サイトに送るテストは賛否両論あるかもしれませんが、
https://github.com/traPtitech/traQ/blob/8a2db60a416f32cf022a74c4bd795d6a4c468b84/service/ogp/parser/domain_twitter_test.go
と似た内容のテストを書けると、動作確認が他の人/あとからでもしやすいかもしれないです。
service/ogp/parser/parser.go
Outdated
return nil, nil, ErrNetwork | ||
} | ||
|
||
req.Header.Add("user-agent", `bot`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user-agentはアクセスしたものが何者なのかを相手に知らせる目的もあるので、可能なら既にあるこちらがわと合わせられると嬉しいですね
traQ/service/ogp/parser/domain_vrchat.go
Line 57 in 8a2db60
req.Header.Set("User-Agent", userAgent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
書き直さなくても、同じパッケージなので単純に変数を参照できます
req.Header.Add("user-agent", `bot`) | |
req.Header.Add("User-Agent", userAgent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Twitterがbotという文字列が入っていないとOGPを返さないようなので、少し名前を変えました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
元の変数を書き換えて、コメントしておくとよいかもですね
テスト書いたの初めてなので、変なところあると思います。指摘お願いします。 |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他のところにもついているし、並行化しても問題ないと思うので、t.Parallel()
付けていいと思います。もし意図があれば別です。
There was a problem hiding this comment.
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とかで変更されそうな仕様だけど
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、僕が意図してたのはループの中じゃなくて関数の初めです。ループにも付けていいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
両方とも平行化できたと思います。e2ee810
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いいと思います。マージ権限ありますか?
あるので、マージします。 |
TwitterのOGPが表示されないのを解消する目的です。
手元で試したところ投稿やページ、ユーザーによっては表示されないものもあるようです。
ついでにカクヨムのOGPも表示されるようになったようです。
Twitter.com(x.com)のみに絞るべきか悩んだのですが、上のカクヨムの例があったので、一旦OGPのために飛ばすGETすべてに
user-agent:bot
をつけています。