Created
October 12, 2013 21:12
-
-
Save irmiller22/6954982 to your computer and use it in GitHub Desktop.
reddit_scrape
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' | |
| require 'pry' | |
| @html_reddit = " " | |
| def get_children_data_scrape | |
| r_hash = JSON.parse(RestClient.get('http://www.reddit.com/.json')) | |
| r_hash["data"]["children"].each do |post| | |
| unless post["data"]["over_18"] == true | |
| @html_reddit << reddit_html_template(post) | |
| end | |
| end | |
| end | |
| def reddit_html_template(post_attribute) | |
| data = post_attribute["data"] | |
| html_string = "<li> | |
| <a href=\"http://reddit.com#{data["permalink"]}\"> | |
| <h1>#{data["title"]}</h1> | |
| <img src=\"#{data["thumbnail"]}\" /> | |
| <h4>Upvotes:</p> | |
| <p>#{data["ups"]}</h4> | |
| <p>Downvotes:</p> | |
| <h4>#{data["downs"]}</h4> | |
| </a> | |
| </li> | |
| " | |
| end | |
| def new_html_file | |
| reddit_html_file = File.new("reddit.html", "w+") | |
| if reddit_html_file | |
| reddit_html_file.syswrite(@html_reddit) | |
| else | |
| puts "Unable to open file!" | |
| end | |
| reddit_html_file.close | |
| end | |
| binding.pry | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment