{"id":1782,"date":"2023-01-20T06:16:45","date_gmt":"2023-01-19T21:16:45","guid":{"rendered":"https:\/\/ubun2m.com\/?p=1782"},"modified":"2023-01-21T16:34:20","modified_gmt":"2023-01-21T07:34:20","slug":"twitterapi%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e7%89%b9%e5%ae%9a%e3%81%ae%e3%83%af%e3%83%bc%e3%83%89%e3%82%92%e6%a4%9c%e7%b4%a2%e3%81%97%e3%80%81%e3%83%96%e3%83%ad%e3%82%b0%e5%9f%8b%e3%82%81%e8%be%bc","status":"publish","type":"post","link":"https:\/\/ubun2m.com\/?p=1782","title":{"rendered":"TwitterAPI\u3092\u4f7f\u3063\u3066\u7279\u5b9a\u306e\u30ef\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3001\u30d6\u30ed\u30b0\u57cb\u3081\u8fbc\u307f\u7528\u306eHTML\u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3010Python\u3011"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u306f\u3058\u3081\u306b<\/h2>\n\n\n\n<p>\u3000<a href=\"?p=1671\" title=\"\u524d\u56de\">\u524d\u56de<\/a>\u306f\u3001OpenAI\u306eAPI\u3092\u4f7f\u7528\u3057\u3066\u3001\u8981\u7d04\u8a18\u4e8b\u3092\u751f\u6210\u3057\u3001WordPress\u306b\u81ea\u52d5\u6295\u7a3f\u3059\u308bPython\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u3002 \u4eca\u56de\u306f\u3001Twitter\u306eAPI\u3092\u4f7f\u7528\u3057\u3066\u3001\u7279\u5b9a\u306e\u30ef\u30fc\u30c9\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u691c\u7d22\u3057\u3001HTML\u5f62\u5f0f\u306b\u6574\u5f62\u3059\u308bPython\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5185\u5bb9<\/h2>\n\n\n\n<p>\u3000\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001Twitter API\u3092\u4f7f\u7528\u3057\u3001\u7279\u5b9a\u306e\u30ef\u30fc\u30c9\u306b\u30de\u30c3\u30c1\u3059\u308b\u30c4\u30a4\u30fc\u30c8\u3092\u691c\u7d22\u3057\u3001WordPress\u306a\u3069\u306e\u30d6\u30ed\u30b0\u306b\u63b2\u8f09\u3059\u308b\u305f\u3081\u306eHTML\u5f62\u5f0f\u3067\u30c7\u30fc\u30bf\u3092\u51fa\u529b\u3059\u308b\u3082\u306e\u3067\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u3000\u307e\u305a\u3001\u30ea\u30b9\u30c8\u304b\u3089\u691c\u7d22\u30ef\u30fc\u30c9\u3092\u9806\u756a\u306b\u53d6\u308a\u51fa\u3057\u3001\u305d\u308c\u305e\u308c\u306b\u5bfe\u3057\u3066Twitter API\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3002API\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306b\u306f\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304c\u5fc5\u8981\u306a\u306e\u3067\u3001query_params\u5909\u6570\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u3000API\u304b\u3089\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306f\u3001filterd_tweets\u95a2\u6570\u306b\u6e21\u3057\u3066\u3001\u30d6\u30ed\u30b0\u306b\u63b2\u8f09\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u60c5\u5831\u3092\u62bd\u51fa\u3059\u308b\u3002\u305d\u3057\u3066\u3001\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306fjson_data\u306b\u8ffd\u52a0\u3055\u308c\u3001\u6700\u7d42\u7684\u306b\u30c4\u30a4\u30fc\u30c8\u30c7\u30fc\u30bf\u3084HTML\u306a\u3069\u3092\u542b\u3093\u3060json\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u304c\u51fa\u529b\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\"><p lang=\"ja\" dir=\"ltr\">ChatGPT\u304c\u9762\u767d\u3059\u304e\u3066\u3001\u9031\u672b\u306f\u307b\u3068\u3093\u3069\u5bdd\u305a\u306b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3057\u3066\u305f\u3002\u9700\u8981\u304c\u3042\u308b\u304b\u8b0e\u3060\u304c\u3001\u8a18\u4e8b\u306b\u3057\u3066\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3082\u516c\u958b\u3057\u305f\u3002<a href=\"https:\/\/t.co\/aS1RLcHxBx\">https:\/\/t.co\/aS1RLcHxBx<\/a><\/p>&mdash; \u308c\u304c\u30fc\u3068 \/ legato (@legato_ip4) <a href=\"https:\/\/twitter.com\/legato_ip4\/status\/1614687652963401729?ref_src=twsrc%5Etfw\">January 15, 2023<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><figcaption class=\"wp-element-caption\">\u4f8b\u3048\u3070\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306a\u57cb\u3081\u8fbc\u307f\u7528\u306eHTML\u3092\u51fa\u529b\u3059\u308b\u305f\u3081\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3042\u308b\u3002<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">filterd_tweets\u95a2\u6570<\/h3>\n\n\n\n<p>\u3000\u4eca\u56de\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30ad\u30fc\u3068\u306a\u308b\u95a2\u6570\u3067\u3042\u308b\u3002Twitter API\u304b\u3089\u53d6\u5f97\u3057\u305f\u30c4\u30a4\u30fc\u30c8\u30c7\u30fc\u30bf\u304b\u3089\u3001HTML\u5f62\u5f0f\u306b\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u60c5\u5831\u3092\u62bd\u51fa\u3059\u308b\u3002\u5f15\u6570\u3068\u3057\u3066\u53d7\u3051\u53d6\u3063\u305f\u30c7\u30fc\u30bf\u306b\u306f\u3001\u691c\u7d22\u306b\u30de\u30c3\u30c1\u3057\u305f\u8907\u6570\u306e\u30c4\u30a4\u30fc\u30c8\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u305d\u308c\u3089\u306e\u30c4\u30a4\u30fc\u30c8\u304b\u3089\u3001\u6295\u7a3f\u8005\u306eusername\u3001\u30c4\u30a4\u30fc\u30c8\u306eURL\u3001oEmbed\u5f62\u5f0f\u306eHTML\u3092\u62bd\u51fa\u3057\u3001\u8fd4\u308a\u5024\u3068\u3057\u3066\u8f9e\u66f8\u578b\u3067\u307e\u3068\u3081\u3066\u8fd4\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def filterd_tweets(data):\n    &quot;&quot;&quot;\n    \u30c4\u30a4\u30fc\u30c8\u304b\u3089\u5fc5\u8981\u306a\u60c5\u5831\u3092\u62bd\u51fa\u3059\u308b\u95a2\u6570\n    Args:\n        data (dict): \u30c4\u30a4\u30fc\u30c8\u3092\u542b\u3080\u30c7\u30fc\u30bf\n    Returns:\n        filtered_tweets (dict): \u62bd\u51fa\u3057\u305f\u60c5\u5831\u3092\u683c\u7d0d\u3057\u305f\u8f9e\u66f8\n    &quot;&quot;&quot;\n    filtered_tweets = {}\n    if not &#39;data&#39; in data: return\n    for tweet in data[&#39;data&#39;]:\n        try:\n            if &#39;public_metrics&#39; in tweet:\n                # \u30c4\u30a4\u30fc\u30c8\u3092\u6295\u7a3f\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u306e\u60c5\u5831\u3092\u53d6\u5f97\n                search_user_url = &quot;https:\/\/api.twitter.com\/2\/users\/{}&quot;.format(tweet[&#39;author_id&#39;])\n                user_data = connect_to_endpoint(search_user_url,&#39;&#39;)\n                username = user_data[&#39;data&#39;][&#39;username&#39;]\n                tweet_url = &quot;https:\/\/twitter.com\/{}\/status\/{}&quot;.format(username,tweet[&#39;id&#39;])\n                # oEmbed\u5f62\u5f0f\u306eHTML\u3092\u53d6\u5f97\n                oembed = requests.get(&#39;https:\/\/publish.twitter.com\/oembed?url{}&#39;.format(tweet_url)).json()\n                tweet[&#39;html&#39;] = oembed[&#39;html&#39;]\n                filtered_tweets = tweet\n        except ValueError:\n            continue\n\n    return filtered_tweets<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d7\u30ed\u30b0\u30e9\u30e0\u5168\u4f53\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"recent_search.py\" data-lang=\"Python\"><code># coding: utf-8\nimport requests\n\n# Twitter API\u306eBearer Token\nbearer_token = &quot;&lt;your_bearer_token&gt;&quot;\n\n# \u691c\u7d22\u7528\u306eURL\nsearch_url = &quot;https:\/\/api.twitter.com\/2\/tweets\/search\/recent&quot;\n\n# \u691c\u7d22\u3057\u305f\u3044\u30ad\u30fc\u30ef\u30fc\u30c9\u306e\u30ea\u30b9\u30c8\nkeyword_list = [&#39;Keyword1&#39;, &#39;Keyword2&#39;,&#39;Keyword3&#39;]\n\ndef filterd_tweets(data):\n    &quot;&quot;&quot;\n    \u30c4\u30a4\u30fc\u30c8\u304b\u3089\u5fc5\u8981\u306a\u60c5\u5831\u3092\u62bd\u51fa\u3059\u308b\u95a2\u6570\n    Args:\n        data (dict): \u30c4\u30a4\u30fc\u30c8\u3092\u542b\u3080\u30c7\u30fc\u30bf\n    Returns:\n        filtered_tweets (dict): \u62bd\u51fa\u3057\u305f\u60c5\u5831\u3092\u683c\u7d0d\u3057\u305f\u8f9e\u66f8\n    &quot;&quot;&quot;\n    filtered_tweets = {}\n    if not &#39;data&#39; in data: return\n    for tweet in data[&#39;data&#39;]:\n        try:\n            if &#39;public_metrics&#39; in tweet:\n                # \u30c4\u30a4\u30fc\u30c8\u3092\u6295\u7a3f\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u306e\u60c5\u5831\u3092\u53d6\u5f97\n                search_user_url = &quot;https:\/\/api.twitter.com\/2\/users\/{}&quot;.format(tweet[&#39;author_id&#39;])\n                user_data = connect_to_endpoint(search_user_url,&#39;&#39;)\n                username = user_data[&#39;data&#39;][&#39;username&#39;]\n                tweet_url = &quot;https:\/\/twitter.com\/{}\/status\/{}&quot;.format(username,tweet[&#39;id&#39;])\n                # oEmbed\u5f62\u5f0f\u306eHTML\u3092\u53d6\u5f97\n                oembed = requests.get(&#39;https:\/\/publish.twitter.com\/oembed?url{}&#39;.format(tweet_url)).json()\n                tweet[&#39;html&#39;] = oembed[&#39;html&#39;]\n                filtered_tweets = tweet\n        except ValueError:\n            continue\n\n    return filtered_tweets\n\ndef bearer_oauth(r):\n    &quot;&quot;&quot;\n    Twitter API\u306e\u8a8d\u8a3c\u306b\u5fc5\u8981\u306a\u30d8\u30c3\u30c0\u30fc\u3092\u8ffd\u52a0\u3059\u308b\u95a2\u6570\n    Args:\n        r (requests.PreparedRequest): API\u306b\u9001\u4fe1\u3059\u308b\u30ea\u30af\u30a8\u30b9\u30c8\n    Returns:\n        r (requests.PreparedRequest): API\u306b\u9001\u4fe1\u3059\u308b\u30ea\u30af\u30a8\u30b9\u30c8(\u30d8\u30c3\u30c0\u30fc\u3092\u8ffd\u52a0\u3057\u305f)\n    &quot;&quot;&quot;\n    r.headers[&quot;Authorization&quot;] = f&quot;Bearer {bearer_token}&quot; # Bearer\u30c8\u30fc\u30af\u30f3\u3092\u8ffd\u52a0\n    r.headers[&quot;User-Agent&quot;] = &quot;v2RecentSearchPython&quot; # User-Agent\u3092\u8ffd\u52a0\n    return r\n\ndef connect_to_endpoint(url, params):\n    &quot;&quot;&quot;\n    Twitter API\u306b\u63a5\u7d9a\u3057\u3001\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570\n    Args:\n        url (str): \u30a2\u30af\u30bb\u30b9\u3059\u308bAPI\u306eURL\n        params (dict): \u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\n    Returns:\n        json: API\u304b\u3089\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\n    &quot;&quot;&quot;\n    # API\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\n    response = requests.get(url, auth=bearer_oauth, params=params)\n    # HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u304c200\u3067\u306a\u304b\u3063\u305f\u5834\u5408\u306f\u4f8b\u5916\u3092\u767a\u751f\n    if response.status_code != 200:\n        raise Exception(response.status_code, response.text)\n    # JSON\u5f62\u5f0f\u3067\u30c7\u30fc\u30bf\u3092\u8fd4\u3059\n    return response.json()\n\ndef main():\n    &quot;&quot;&quot;\n    main\u95a2\u6570\n    &quot;&quot;&quot;\n    json_data = []\n    # \u30ad\u30fc\u30ef\u30fc\u30c9\u30ea\u30b9\u30c8\u304b\u3089\u691c\u7d22\u3092\u5b9f\u884c\n    for keyword in keyword_list:\n        # \u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u8a2d\u5b9a\n        query_params = {&#39;query&#39;: keyword,&#39;tweet.fields&#39;: &#39;author_id,public_metrics&#39;,&#39;user.fields&#39;:&#39;username&#39;,&#39;max_results&#39;:100}\n        # API\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\n        data = filterd_tweets(connect_to_endpoint(search_url, query_params))\n        # \u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092json_data\u306b\u8ffd\u52a0\n        if data:\n            json_data.append(data)\n    # \u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u51fa\u529b\n    print(json_data)\n    \nif __name__ == &quot;__main__&quot;:\n    main()\n<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u304a\u308f\u308a\u306b<\/h2>\n\n\n\n<p>\u3000Twitter\u306eAPI\u306e\u4ed5\u69d8\u306f\u8907\u96d1\u3067\u3042\u308a\u3001\u3053\u3093\u306a\u77ed\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306f\u3042\u308b\u304c\u4f5c\u6210\u306b\u306f\u6642\u9593\u304c\u304b\u304b\u3063\u305f\u3002\u4eca\u56de\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f5c\u6210\u306b\u3082ChatGPT\u3092\u4f7f\u7528\u3057\u305f\u304c\u3001ChatGPT\u304cTwitter\u306eAPI\u306e\u4ed5\u69d8\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f5c\u6210\u306b\u306f\u7279\u306b\u5f79\u7acb\u3064\u7b54\u3048\u304c\u5f97\u3089\u308c\u306a\u304b\u3063\u305f\u3002\u3057\u304b\u3057\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u516c\u958b\u3059\u308b\u305f\u3081\u306b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aac\u660e\u3084\u95a2\u6570\u306e\u8aac\u660e\u3092\u8ffd\u52a0\u3059\u308b\u4f5c\u696d\u306b\u306f\u975e\u5e38\u306b\u5f79\u7acb\u3063\u305f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u8003<\/h3>\n\n\n\n<p>Twitter\u516c\u5f0f Developper Platform<br><a href=\"https:\/\/developer.twitter.com\/en\/docs\/twitter-api\/tweets\/search\/api-reference\/get-tweets-search-recent\">https:\/\/developer.twitter.com\/en\/docs\/twitter-api\/tweets\/search\/api-reference\/get-tweets-search-recent<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"affiliate-box\"><div class=\"affiliate-containar\"><a href=\"https:\/\/amzn.to\/3D3Vjyv\" rel=\"nofollow\"><img decoding=\"async\" style=\"border: none;\" src=\"https:\/\/m.media-amazon.com\/images\/I\/51uEhL3YZDL._SX350_BO1,204,203,200_.jpg\" target=\"_blank\"><\/a><div class=\"affiliate-content\"><a href=\"https:\/\/amzn.to\/3D3Vjyv\" rel=\"nofollow\">\u9000\u5c48\u306a\u3053\u3068\u306fPython\u306b\u3084\u3089\u305b\u3088\u3046 \u7b2c2\u7248 \u2015\u30ce\u30f3\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u306b\u3082\u3067\u304d\u308b\u81ea\u52d5\u5316\u51e6\u7406\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/a><ul class=\"affiliate-button\"><li><a href=\"https:\/\/amzn.to\/3D3Vjyv\" rel=\"nofollow\">Amazon<\/a><\/li><\/ul><\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u3000\u524d\u56de\u306f\u3001OpenAI\u306eAPI\u3092\u4f7f\u7528\u3057\u3066\u3001\u8981\u7d04\u8a18\u4e8b\u3092\u751f\u6210\u3057\u3001WordPress\u306b\u81ea\u52d5\u6295\u7a3f\u3059\u308bPython\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u3002 \u4eca\u56de\u306f\u3001Twitter\u306eAPI\u3092\u4f7f\u7528\u3057\u3066\u3001\u7279\u5b9a\u306e\u30ef\u30fc\u30c9\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u691c\u7d22\u3057\u3001HTML [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1784,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1782","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/ubun2m.com\/wp-content\/uploads\/2023\/01\/Twitter-social-icons-square-blue.png","_links":{"self":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/posts\/1782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1782"}],"version-history":[{"count":0,"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/posts\/1782\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/media\/1784"}],"wp:attachment":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}