Last active
October 14, 2018 14:18
-
-
Save yanshiyason/e4b4827a80b01a6d83547249ce7e2015 to your computer and use it in GitHub Desktop.
Parse the reddit API docs and return them as JSON
This file contains 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 '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