--Get a list of Qiita articles for a specific user with Ruby + Qiita API v2
require 'json'
require 'net/http'
require 'open-uri'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Access token
user = 'niwasawa' #Article creation user name
all_items = [] #Array to hold all articles
resource_url = 'https://qiita.com/api/v2/items' #WebAPI entry point
headers = {'Authorization' => "Bearer #{access_token}"} #Authentication header
page = 0 #Page nation page number
loop do
#Assemble the parameters
page += 1
params = {
'query' => "user:#{user}", #Article creation Search by user name
'per_page' => '100', #Up to 100 items can be acquired with one API call
'page' => page #Page nation page number(Start from 1)
}
#Get a list of articles
url = resource_url + '?' + URI.encode_www_form(params)
puts "Calling WebAPI: #{url}"
res = URI.open(url, headers)
items = JSON.parse(res.read)
#Exit the loop if you can't get any articles
break if items.empty?
#Add a list of articles to the array that contains all the articles
all_items.concat items
end
#Sort by article creation date
all_items.sort!{|a, b| Time.iso8601(a['created_at']) <=> Time.iso8601(b['created_at'])}
#Output the article creation date and time and title
all_items.each do |item|
puts "#{Time.iso8601(item['created_at']).strftime('%Y-%m-%d %H:%M:%S')} #{item['title']}"
end
An example of running sample code on macOS Catalina + Ruby 3.0.0.
Calling WebAPI: https://qiita.com/api/v2/items?query=user%3Aniwasawa&per_page=100&page=1
Calling WebAPI: https://qiita.com/api/v2/items?query=user%3Aniwasawa&per_page=100&page=2
Calling WebAPI: https://qiita.com/api/v2/items?query=user%3Aniwasawa&per_page=100&page=3
Calling WebAPI: https://qiita.com/api/v2/items?query=user%3Aniwasawa&per_page=100&page=4
Calling WebAPI: https://qiita.com/api/v2/items?query=user%3Aniwasawa&per_page=100&page=5
2017-01-10 12:31:51 Sample code to call WebAPI in Ruby
2017-01-16 12:49:08 Ruby + RESAS-Location information of tourism resources with API(longitude latitude)To get
2017-01-31 12:12:35 PHPUnit Beginning(Install & test suite build)
(Omission)
2020-12-12 16:18:40 Migrate from CentOS Linux 8 to CentOS Stream 8
2020-12-16 08:46:35 Parse integer values outside the range of numeric types described in JSON with JavaScript
2020-12-19 23:05:53 Check the table definition in Oracle Database 12c
-Qiita API v2 documentation -Qiita: Developer -Options available when searching -Qiita: Support -library open \ -uri \ (Ruby 3 \ .0 \ .0 Reference Manual ) -class Array \ (Ruby 3 \ .0 \ .0 Reference Manual ) -module JSON \ (Ruby 3 \ .0 \ .0 Reference Manual ) -class Time \ (Ruby 3 \ .0 \ .0 Reference Manual )
Recommended Posts