Skip to content

Commit 2708250

Browse files
author
Peter Cooper
committed
Added main project
1 parent 20dfcc5 commit 2708250

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+980
-0
lines changed

app.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
get '/' do
2+
# Get JSON filenames
3+
#@dates = Dir[File.dirname(__FILE__) + '/public/data/*.json'].sort.reverse.first(30).reverse.map { |a| a[/\d{4}-\d{2}-\d{2}(?=\.)/] }.compact
4+
@dates = Dir[File.dirname(__FILE__) + '/public/data/*.json'].sort.reverse.first(70).reverse.map { |a| a[/\d{4}-\d{2}-\d{2}-\d*(?=\.)/] }.compact
5+
erb :index
6+
end

config.ru

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'rubygems'
2+
require 'sinatra'
3+
require 'app'
4+
5+
set :environment, :development
6+
set :root, File.dirname(__FILE__)
7+
8+
run Sinatra::Application

crawl.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'rubygems'
2+
require 'json'
3+
4+
# Get the current time in GMT and build some filenames
5+
time = Time.now.gmtime
6+
filename = File.dirname(__FILE__) + "/public/data/"
7+
now_filename = filename + "now.json"
8+
filename += "#{time.year}-#{time.month}-#{time.mday}"
9+
daily_filename = filename + ".json"
10+
hourly_filename = filename + "-#{sprintf("%.2d", Time.now.hour)}.json"
11+
12+
require 'ruby-hackernews'
13+
14+
# Fetch items from front page of Hacker News
15+
items = Entry.all
16+
17+
# Turn the items into a nicer array of hashes
18+
items.map! do |item|
19+
{ :title => item.link.title,
20+
:site => item.link.site,
21+
:href => item.link.href,
22+
:number => item.number,
23+
:score => item.voting.score,
24+
:user => item.user.name,
25+
:hours_ago => ((Time.now.to_i - item.time.to_i) / 3600).to_i,
26+
:comments => item.instance_variable_get(:@comments_info).count,
27+
:item_id => item.instance_variable_get(:@comments_info).page[/\d+/] } # eugh!
28+
end
29+
30+
# Save the items in JSON format for later recall
31+
puts "Writing #{now_filename}"
32+
File.open(now_filename, "w") do |f|
33+
f.puts items.to_json
34+
end
35+
36+
unless File.exist?(daily_filename)
37+
puts "Writing #{daily_filename}"
38+
File.open(daily_filename, "w") do |f|
39+
f.puts items.to_json
40+
end
41+
end
42+
43+
unless File.exist?(hourly_filename)
44+
puts "Writing #{hourly_filename}"
45+
File.open(hourly_filename, "w") do |f|
46+
f.puts items.to_json
47+
end
48+
end

public/.DS_Store

6 KB
Binary file not shown.

public/._.DS_Store

82 Bytes
Binary file not shown.

public/css/._ui.slider.extras.css

237 Bytes
Binary file not shown.
237 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)