twitter API V2で、retweetした時間を取得する


キャンペーンのためretweetされた時間別の取得ができないかという話がでて調べました。ドキュメントを見ても不明だったため、試行錯誤の結果をメモ代わりに。アカウントは、Developで、Standard Search APIのみの使用です。academicは使用できません。Twitter API V2の使用に必要なtokenの設定などは省いています。

結論的には、retweetのIDを取得して、個別にTweets lookup APIで取得できます。ただしretweetsのAPIでは、reweetをリスト形式で取得できますが、retweet用のIDやcreated_atは取得できません。そのため工夫が必要です。（他に良い方法があればコメント頂ければ幸いです）

Tweets lookup API
hhttps://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference/get-tweets-id

expansionsにreferenced_tweets、tweet.fieldsにcreated_atを設定する。（以下は参考までに、可能なオプションを設定しています）

例
1469223982708957186を指定して、twitterで確認すると、retweetのIDとわかります。
https://twitter.com/clfrncfight/status/1469223982708957186

```
curl https://api.twitter.com/2/tweets/1469223982708957186?expansions=referenced_tweets.id&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld  -H "Authorization: Bearer $BEARER_TOKEN"
```

```
{
	"data": {
		"conversation_id": "1469223982708957186",
		"entities": {
			"mentions": [
				{
					"start": 3,
					"end": 17,
					"username": "biathlonworld",
					"id": "49975451"
				}
			],
			"annotations": [
				{
					"start": 35,
					"end": 37,
					"probability": 0.665,
					"type": "Organization",
					"normalized_text": "IBU"
				}
			]
		},
		"created_at": "2021-12-10T08:34:28.000Z",
		"reply_settings": "everyone",
		"text": "RT @biathlonworld: Every year, the IBU provides material support for all developing members through cooperation with its supplying partners…",
		"author_id": "934056413726367750",
		"referenced_tweets": [
			{
				"type": "retweeted",
				"id": "1468993500213653506"
			}
		],
		"source": "Twitter for iPhone",
		"id": "1469223982708957186",
		"public_metrics": {
			"retweet_count": 8,
			"reply_count": 0,
			"like_count": 0,
			"quote_count": 0
		},
		"possibly_sensitive": false,
		"lang": "en"
	},
	"includes": {
		"tweets": [
			{
				"attachments": {
					"media_keys": [
						"3_1468993369724567558",
						"3_1468993399533576205"
					]
				},
				"conversation_id": "1468993500213653506",
				"created_at": "2021-12-09T17:18:37.000Z",
				"entities": {
					"urls": [
						{
							"start": 279,
							"end": 302,
							"url": "https://t.co/XNG4WCsVCz",
							"expanded_url": "https://twitter.com/biathlonworld/status/1468993500213653506/photo/1",
							"display_url": "pic.twitter.com/XNG4WCsVCz"
						},
						{
							"start": 279,
							"end": 302,
							"url": "https://t.co/XNG4WCsVCz",
							"expanded_url": "https://twitter.com/biathlonworld/status/1468993500213653506/photo/1",
							"display_url": "pic.twitter.com/XNG4WCsVCz"
						}
					],
					"annotations": [
						{
							"start": 16,
							"end": 18,
							"probability": 0.6319,
							"type": "Organization",
							"normalized_text": "IBU"
						},
						{
							"start": 150,
							"end": 159,
							"probability": 0.7603,
							"type": "Place",
							"normalized_text": "Hochfilzen"
						}
					]
				},
				"reply_settings": "everyone",
				"text": "Every year, the IBU provides material support for all developing members through cooperation with its supplying partners. \n\nDuring the past 2 days in Hochfilzen, this equipment was distributed to the member federations to support the development of biathlon on a national level. https://t.co/XNG4WCsVCz",
				"author_id": "49975451",
				"source": "Twitter Web App",
				"id": "1468993500213653506",
				"public_metrics": {
					"retweet_count": 8,
					"reply_count": 1,
					"like_count": 72,
					"quote_count": 5
				},
				"possibly_sensitive": false,
				"lang": "en"
			}
		]
	}
}
```

では、あるが、このretweet用のIDを取得が難しい。

https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/introduction

retweets用のAPIでは対象のtweetに対して、retweetしたユーザ一覧しか取得できません。

```
https://api.twitter.com/2/tweets/1468993500213653506/retweeted_by?&user.fields=id,name,created_at
```

```
{
	"data": [
		{
			"username": "maxkcobb",
			"name": "Max Cobb",
			"id": "69670910",
			"created_at": "2009-08-28T19:23:17.000Z"
		},
		{
			"username": "Winkler_Chr",
			"name": "Christian Winkler",
			"id": "984086797981544448",
			"created_at": "2018-04-11T15:12:30.000Z"
		},
		{
			"username": "halsbruecke",
			"name": "Holm Holmsen 🇸🇪",
			"id": "46539657",
			"created_at": "2009-06-12T01:00:38.000Z"
		},
		{
			"username": "BiatlonPokljuka",
			"name": "Biathlon Pokljuka",
			"id": "2363102827",
			"created_at": "2014-02-26T19:20:15.000Z"
		}
	],
	"meta": {
		"result_count": 9
	}
}
```

そこからユーザIDを取得してユーザのタイムラインを取得して、
referenced_tweetsを見て、該当のretweetと判断する……。ライセンス的に、7日間しか取得できない、APIを叩く制限がある、ため、現実的ではないかも知れませんが、一応できるよという説明でした。

https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/introduction

```
curl https://api.twitter.com/2/users/934056413726367750/tweets?tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld&expansions=referenced_tweets.id&user.fields=created_at&max_results=15 -H "Authorization: Bearer $BEARER_TOKEN"
```

```
		{
			"text": "RT @biathlonworld: Every year, the IBU provides material support for all developing members through cooperation with its supplying partners…",
			"id": "1469223982708957186",
			"referenced_tweets": [
				{
					"type": "retweeted",
					"id": "1468993500213653506"
				}
			]
		},

```