Last active
December 24, 2015 12:59
-
-
Save ivanbrennan/6801116 to your computer and use it in GitHub Desktop.
Safe for work Reddit
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 'json' | |
| require 'rest_client' | |
| reddit_hash = JSON.parse(RestClient.get('http://reddit.com/.json')) | |
| def indent(string, num=1) | |
| string.each_line. | |
| map{|line| "#{" " * num}#{line}"}.join | |
| end | |
| def open_tag(string) | |
| "<#{string}>\n" | |
| end | |
| def close_tag(string) | |
| "</#{string}>\n" | |
| end | |
| def build_html(stories) | |
| open_tag("html") + | |
| indent(build_head + build_body(stories)) + | |
| close_tag("html") | |
| end | |
| def build_head | |
| open_tag("head") + close_tag("head") | |
| end | |
| def build_body(stories) | |
| open_tag("body") + | |
| indent(build_ul(stories)) + | |
| close_tag("body") | |
| end | |
| def build_ul(stories) | |
| open_tag("ul") + | |
| indent(stories.collect{|story| build_li(story)}.join) + | |
| close_tag("ul") | |
| end | |
| def build_li(story) | |
| open_tag("li") + | |
| indent(build_li_link(story)) + | |
| close_tag("li") | |
| end | |
| def build_li_link(story) | |
| url = story["data"]["url"] | |
| open_tag("a href=#{url}") + | |
| indent(build_li_attrs(story),2) + | |
| close_tag("a") | |
| end | |
| def build_li_attrs(story) | |
| title = story["data"]["title"] | |
| thumbnail = story["data"]["thumbnail"] | |
| ups = story["data"]["ups"] | |
| downs = story["data"]["downs"] | |
| open_tag("h1").chomp + title + close_tag("h1") + | |
| open_tag("img src=#{thumbnail} /") + | |
| open_tag("h4").chomp + "Upvotes" + close_tag("h4") + | |
| open_tag("p").chomp + ups.to_s + close_tag("p") + | |
| open_tag("h4").chomp + "Downvotes" + close_tag("h4") + | |
| open_tag("p").chomp + downs.to_s + close_tag("p") | |
| end | |
| reddit_stories = reddit_hash["data"]["children"] | |
| filtered_reddit_stories = reddit_stories. | |
| reject{|story| story["data"]["over_18"] == true} | |
| html = build_html(filtered_reddit_stories) | |
| html_file = File.open("sfw-reddit.html","w"){|file| file.write(html)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment