BingNewsSearchAPIを使って特定のニュース一覧を表示する

はじめに

rubyからBingNewsSearchAPIを使って特定のニュース一覧を表示する方法について

前提

・MicrosoftAzureアカウントにログインしている

リソースを作成する

まずAzureアカウントの検索窓でBing Search v7と検索してリソースを作成するページにいきます。

Image from Gyazo

Image from Gyazo

入力項目に任意の値をいれてリソースを作成します。

これでキーとエンドポイントが表示されるので保管しておいてください。

rubyで処理を追加する

require 'net/https'
require 'uri'
require 'json'

accessKey = "先程取得したキーを入力"

uri  = "https://api.bing.microsoft.com/"
path = "/v7.0/news/search"
count = "2"

term = "Microsoft"

uri = URI(uri + path + "?count=" + count + "&q=" + URI.escape(term))

request = Net::HTTP::Get.new(uri)
request['Ocp-Apim-Subscription-Key'] = accessKey

response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(request)
end

puts "\nJSON Response:\n\n"
puts JSON::pretty_generate(JSON(response.body))
require 'net/https'
require 'uri'
require 'json'

まずは上記のコードファイルをインポートします。

accessKey = "先程取得したキーを入力"

uri  = "https://api.bing.microsoft.com/"
path = "/v7.0/news/search"
count = "2"

term = "Microsoft"

accessKeyには先程取得したキーを代入します。

uriapiのエンドポイントを代入し、pathにはニュース検索のURLを入れます。

countは記事の取得数を設定します。デフォルトでは10件です。

termは検索する単語を入れます。

uri = URI(uri + path + "?count=" + count + "&q=" + URI.escape(term))

request = Net::HTTP::Get.new(uri)
request['Ocp-Apim-Subscription-Key'] = accessKey

response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(request)
end

先程の変数を利用してURLを作成し、apiをたたきます。

puts "\nJSON Response:\n\n"
puts JSON::pretty_generate(JSON(response.body))

帰ってきたJSONのレスポンスを解析して出力しています。

先程のファイルをrubyコマンドで実行します。

$ bundle exec ruby bing_news_search.rb

実際の実行結果は下記のとおりです。

JSON Response:

{
  "_type": "News",
  "readLink": "https://api.bing.microsoft.com/api/v7/news/search?q=Microsoft",
  "queryContext": {
    "originalQuery": "Microsoft",
    "adultIntent": false
  },
  "totalEstimatedMatches": 32,
  "sort": [
    {
      "name": "最も一致する結果",
      "id": "relevance",
      "isSelected": true,
      "url": "https://api.bing.microsoft.com/api/v7/news/search?q=Microsoft"
    },
    {
      "name": "最新",
      "id": "date",
      "isSelected": false,
      "url": "https://api.bing.microsoft.com/api/v7/news/search?q=Microsoft&sortby=date"
    }
  ],
  "value": [
    {
      "name": "AZPower 「Web アプリケーションの Microsoft Azure への最新化」分野で ...",
      "url": "https://japan.cnet.com/release/30569532/",
      "image": {
        "thumbnail": {
          "contentUrl": "https://www.bing.com/th?id=OVFT.9SmEaCzGrZPBD74eFfWwNi&pid=News",
          "width": 600,
          "height": 315
        }
      },
      "description": "「Microsoft Azure への Windows Server と SQL Server の移行」に続いて2つ目となる、Advanced Specializationの取得となります AZPower株式会社(本社:東京都千代田区/代表取締役社長 橋口 信平、以下AZPower)は、2021年7月6日、マイクロソフトがGoldコンピテンシーパートナー向けに高度な専門性を有する企業として「Adv",
      "about": [
        {
          "readLink": "https://api.bing.microsoft.com/api/v7/entities/cf3abf7d-e379-2693-f765-6da6b9fa9149",
          "name": "Windows Azure"
        }
      ],
      "provider": [
        {
          "_type": "Organization",
          "name": "CNET",
          "image": {
            "thumbnail": {
              "contentUrl": "https://www.bing.com/th?id=AR_813fbe5ed7179eb7f517fd9bab6522f9&pid=news"
            }
          }
        }
      ],
      "datePublished": "2021-07-15T00:52:00.0000000Z",
      "category": "ScienceAndTechnology"
    },
    {
      "name": "マイクロソフトがWindows 365を公開-コンピューティングの新しい ...",
      "url": "https://www.sanspo.com/geino/news/20210715/prl21071510170047-n1.html",
      "image": {
        "thumbnail": {
          "contentUrl": "https://www.bing.com/th?id=OVFT.s9nOmSHw4aHPf68X2QpXpy&pid=News",
          "width": 700,
          "height": 367
        }
      },
      "description": "マイクロソフト コーポレーション(Microsoft Corp.)は15日、Windows 10ないしはWindows 11(利用可能になった場合)を体験する新しい方法をあらゆる規模の企業に導入するクラウドサービスWindows 365を発表した。Windows 365は、オペレーティングシステム(OS)をマイクロソフトクラウドに取り込み、アプリ、データ、セッティングのすべてのWindows体験を",
      "about": [
        {
          "readLink": "https://api.bing.microsoft.com/api/v7/entities/16aeb6d9-9098-0a40-4970-8e46a4fcee12",
          "name": "Microsoft Windows"
        }
      ],
      "provider": [
        {
          "_type": "Organization",
          "name": "SANSPO"
        }
      ],
      "datePublished": "2021-07-15T01:17:00.0000000Z",
      "category": "ScienceAndTechnology"
    }
  ]
}

公式ドキュメント

docs.microsoft.com