fork download
  1. require 'json'
  2. require 'open-uri'
  3.  
  4. def base_url
  5. "http://www.reddit.com/r/dailyprogrammer.json?limit=100000&after="
  6. end
  7.  
  8. def pull(before='')
  9. target_url = base_url + before.to_s
  10. puts "PULLING #{target_url}"
  11. results = open(target_url).readlines.join
  12.  
  13. posts = JSON.parse(results, symbolize_names: true)
  14.  
  15. # Discard occasional empty hash
  16. posts = posts.select { |p| p.is_a?(Hash) && !p.empty? }.first
  17. posts = posts.fetch(:data).fetch(:children)
  18.  
  19. posts = posts.map { |p| p[:data] }
  20. posts = posts.select { |p| p[:title].include?('#') }
  21. posts = posts.map { |p| Challenge.new(p) }.sort
  22. end
  23.  
  24. def dataset
  25. @dataset ||= get_all
  26. end
  27.  
  28. def get_all
  29. all_posts = []
  30. next_id = ''
  31.  
  32. while all_posts.empty? || (all_posts.map(&:challenge_id).min > 1)
  33. new_posts = pull(next_id)
  34. all_posts.concat(new_posts)
  35. all_posts.sort!.uniq!
  36. next_id = all_posts.first.reddit_id
  37. end
  38.  
  39. return all_posts.uniq.sort
  40. end
  41.  
  42. def main
  43. return dataset
  44. end
  45.  
  46.  
  47. class Challenge
  48. attr_accessor :raw
  49.  
  50. def initialize(hash={})
  51. @raw = hash
  52. self
  53. end
  54.  
  55. def difficulty
  56. return @difficulty unless @difficulty.nil?
  57. return @difficulty = 0 if self.title.downcase.include?('easy')
  58. return @difficulty = 1 if self.title.downcase.include?('intermediate')
  59. return @difficulty = 2
  60. end
  61.  
  62. def title
  63. @title ||= @raw[:title]
  64. end
  65.  
  66. def challenge_id
  67. begin
  68. @challenge_id ||= title.split('#')[1].split(' ').first.to_i
  69. rescue
  70. -1
  71. end
  72. end
  73.  
  74. def hash
  75. [self.challenge_id].hash
  76. end
  77.  
  78. def eql?(other)
  79. self.hash == other.hash
  80. end
  81.  
  82. def reddit_id
  83. @reddit_id ||= @raw[:name].to_s
  84. end
  85.  
  86. def url
  87. @url ||= @raw[:url]
  88. end
  89.  
  90. def to_s
  91. "[#{%w(Easy Intermediate Difficult)[self.difficulty]}] #{self.title} #{self.url}"
  92. end
  93.  
  94. def <=>(other)
  95. return -1 if self.challenge_id < other.challenge_id
  96. return 0 if self.challenge_id == other.challenge_id
  97. return 1
  98. end
  99. end
  100.  
  101. main
Runtime error #stdin #stdout 0.08s 9608KB
stdin
Standard input is empty
stdout
PULLING http://www.reddit.com/r/dailyprogrammer.json?limit=100000&after=