概要
自分の記事全てを取得し、元のテキスト本文にテキストを追加して編集リクエストを続けていく。
ちなみに2000記事ほどを連続で編集しようとすると、途中で制限されたのか、Fobbiden扱いになった。
コード
require 'net/https'
require 'uri'
require 'json'
uri = URI.parse("https://qiita.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request_header = {'Content-Type' =>'application/json', "Authorization" => "Bearer #{ENV['TOKEN']}"}
round = 0
# 100はQiita APIのページめくりのMAX
(1..100).each do |i|
get_url = "https://qiita.com/api/v2/users/#{ENV['USER_ID']}/items?page=#{i}&per_page=100"
get_request = Net::HTTP::Get.new(get_url, request_header)
get_response = http.request(get_request)
items = JSON.parse(get_response.response.body)
items.each do |item|
patch_url = "https://qiita.com/api/v2/items/#{item['id']}"
patch_request = Net::HTTP::Patch.new(patch_url, request_header)
round += 1
puts "#{round} #{item['url']}"
# すでにリンクが付いている記事はスキップ
if item['body'] =~ /\n
EOM
patch_request.body = { 'body' => edit_body, 'title' => item['title'] }.to_json
patch_response = http.request(patch_request)
puts patch_response.code
end
end
# PATCH /api/v2/items/:item_id
# https://qiita.com/api/v2/docs#patch-apiv2itemsitem_id
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。