Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Created November 30, 2016 16:04
Show Gist options
  • Save MaxPleaner/7944a0f6c7423bdbe9e91762f8521fad to your computer and use it in GitHub Desktop.
Save MaxPleaner/7944a0f6c7423bdbe9e91762f8521fad to your computer and use it in GitHub Desktop.
get all stack overflow answers as plaintext
require 'dotenv'
require 'pry'
require 'ruby-stackoverflow'
require 'rest_client'
Dotenv.load
RubyStackoverflow.configure do|config|
config.client_key = ENV["SO_PRIVATE"]
end
response = RubyStackoverflow.users_with_answers(
[ENV["USER_ID"]], page: 5, pagesize: 100
)
answer_ids = response.data.flat_map(&:answers)
.select(&:is_accepted)
.map(&:answer_id)
puts "answer ids: #{answer_ids.length}"
answer_ids.each_with_index do |id, idx|
sleep 2
puts "#{idx}"
url = "https://api.stackexchange.com/2.1/answers/#{id}?site=stackoverflow&filter=withbody"
response = JSON.parse RestClient.get url
File.open("files/#{id}.md", 'w') { |f|
txt = response['items'][0]['body']
f.write txt
puts txt
}
end
concatenated = Dir.glob("./files/*.md").map do |path|
File.read path
end.join("<br><hr><br>")
File.open("concatenated.md", 'w') { |f| f.write concatenated }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment