Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse the reddit API docs and return them as JSON
require 'nokogiri'
require 'json'
html = `curl 'https://www.reddit.com/dev/api/' \
-H 'authority: www.reddit.com' \
-H 'cache-control: max-age=0' \
-H 'upgrade-insecure-requests: 1' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' \
-H 'referer: https://www.google.co.jp/' \
-H 'accept-encoding: gzip, deflate, br' \
-H 'accept-language: en-US,en;q=0.9,ja;q=0.8' --compressed`
doc = Nokogiri::HTML(html)
data = doc.css('.endpoint').map { |e|
{ method: e.css('h3 .method').text,
oauth_scope: e.css('.api-badge.oauth-scope').text,
description: e.css('.info .md').text,
endpoint: -> {
e.css('h3 .placeholder').each do |p| p.replace("__#{p.text}__") end
e.css('h3').children.select(&:text?).map(&:text).join
}.(),
params: e.css('.parameters th').zip(e.css('.parameters td')).map { |param, desc|
{ param.text.to_sym => desc.text }
},
variants: -> {
e.css('.uri-variants .placeholder').each do |p| p.replace("__#{p.text}__") end
e.css('.uri-variants li').map(&:text)
}.()
}
}
File.open("reddit_api.json", 'w') { |f| f << data.to_json }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.