Created
November 30, 2016 16:04
-
-
Save MaxPleaner/7944a0f6c7423bdbe9e91762f8521fad to your computer and use it in GitHub Desktop.
get all stack overflow answers as plaintext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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