Get a list of Qiita articles for a specific user with Ruby + Qiita API v2

Overview

--Get a list of Qiita articles for a specific user with Ruby + Qiita API v2

Sample code

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

Execution example

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

Reference material

-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

Get a list of Qiita articles for a specific user with Ruby + Qiita API v2
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
Get a list of MBean information for Java applications
Get a list of classes in a Guava specific package
A list of rawValues for UITextContentType.
Extract a part of a string with Ruby
How to get the ID of a user authenticated with Firebase in Swift
Procedure for operating google spreadsheet with API (ruby)
[Ruby] Get Qiita trend articles by web scraping
Try to get redmine API key with ruby
[Spring Boot] Get user information with Rest API (beginner)
Output a list of cron settings for all users.
Extract a specific element from the list of objects
Get S3 object size with AWS SDK for Ruby
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post
Notify Slack of AWS bills daily with Lambda for Ruby
I searched for a web framework with Gem in Ruby
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
I tried to create an API to get data from a spreadsheet in Ruby (with service account)