Permalink
Browse files

reorganize and simplify app

  • Loading branch information...
1 parent d16a7d1 commit 452c592971f66f7d4a4e54fd8175990bffe61474 @mfilej committed Sep 17, 2011
View
@@ -1 +1,2 @@
-tmp/restart.txt
+tmp/*
+.sass-cache
View
@@ -2,7 +2,13 @@ source :rubygems
gem "sinatra"
gem "haml"
+gem "sass"
gem "bson_ext"
gem "mongo"
gem "nokogiri"
+group :test do
+ gem "rspec"
+ gem "rack-test"
+ gem "capybara"
+end
View
@@ -3,22 +3,58 @@ GEM
specs:
bson (1.3.1)
bson_ext (1.3.1)
+ capybara (1.1.1)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 2.0)
+ xpath (~> 0.1.4)
+ childprocess (0.2.2)
+ ffi (~> 1.0.6)
+ diff-lcs (1.1.2)
+ ffi (1.0.9)
haml (3.1.1)
+ json_pure (1.5.3)
+ mime-types (1.16)
mongo (1.3.1)
bson (>= 1.3.1)
nokogiri (1.4.4)
rack (1.2.2)
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ rubyzip (0.9.4)
+ sass (3.1.5)
+ selenium-webdriver (2.6.0)
+ childprocess (>= 0.2.1)
+ ffi (>= 1.0.7)
+ json_pure
+ rubyzip
sinatra (1.2.6)
rack (~> 1.1)
tilt (< 2.0, >= 1.2.2)
tilt (1.3)
+ xpath (0.1.4)
+ nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
bson_ext
+ capybara
haml
mongo
nokogiri
+ rack-test
+ rspec
+ sass
sinatra
View
@@ -0,0 +1,9 @@
+namespace :episodes do
+ desc "Clears and reuilds the episodes index from given file"
+ task :build do
+ $LOAD_PATH << File.expand_path("../lib", __FILE__)
+ require "zero"
+ Zero.db = "zerop_development"
+ Parsed.build_index(STDIN.read)
+ end
+end
View
65 app.rb
@@ -1,65 +0,0 @@
-require "bundler"
-Bundler.require
-
-$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
-require "zero"
-
-require "sinatra"
-require "haml"
-
-if ENV["MONGOHQ_URL"]
- uri = URI.parse(ENV["MONGOHQ_URL"])
- $mongo_connection = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
- db = uri.path[1..-1]
-else
- $mongo_connection = Mongo::Connection.new
- db = "zero_development"
-end
-
-Video.database = $mongo_connection.db(db)
-
-helpers do
- def video_url(video)
- url(%{/e/#{video._id}})
- end
-end
-
-get "/" do
- update_index
- @videos = Video.all
-
- headers "Cache-Control" => "public, max-age=900"
- haml :index
-end
-
-get "/e/:id" do |id|
- @video = Video.find(id)
-
- headers "Cache-Control" => "public",
- "Expires" => "Sun, 17-Jan-2038 19:14:07 GMT"
- haml :episode
-end
-
-get "/e/:id/v" do |id|
- url = Video.find(id).url
- video = Video.new(url)
- redirect to(video.video_url)
-end
-
-get "/feed.rss" do
- @videos = Video.all.reverse
- content_type :rss
- haml :feed, format: :xhtml
-end
-
-def update_index
- feed.videos.each do |key, attrs|
- next if Video[key]
-
- Video.save(key, attrs)
- end
-end
-
-def feed
- @feed ||= Feed.new("http://www.escapistmagazine.com/rss/videos/list/1.xml")
-end
View
@@ -1,3 +1,9 @@
-require "./app"
-run Sinatra::Application
+require "bundler/setup"
+
+$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
+require "zero"
+
+Zero.db = ENV["MONGOHQ_URL"] || "zerop_development"
+
+run Zero::App
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,34 @@
+require "forwardable"
+
+class Episode
+
+ class << self
+ extend Forwardable
+ def_delegators :collection, :save, :find, :find_one
+
+ def collection
+ @collection ||= Zero.db_connection["episodes"].tap do |c|
+ c.create_index([[:pubdate, -1]])
+ end
+ end
+
+ KEY = "_id"
+
+ def [](id)
+ find_one(KEY => id.to_i)
+ end
+
+ def []=(id, attrs)
+ save attrs.merge(KEY => id.to_i)
+ end
+
+ def newest_first
+ find.sort ['pubdate', -1]
+ end
+
+ def latest(num)
+ newest_first.limit(num)
+ end
+ end
+
+end
View
@@ -1,32 +0,0 @@
-require "date"
-
-class Feed
-
- def initialize(url)
- @url = url
- end
-
- def xml
- @xml ||= Nokogiri::XML(open @url)
- end
-
- def videos
- @videos ||= xml.search("item").each_with_object({}) do |item, hash|
- hash[key(item)] = params(item)
- end
- end
-
- def key(item)
- item.at("guid").inner_html
- end
-
- def params(item)
- {}.tap do |attrs|
- attrs[:url] = CGI.unescape item.at("link").inner_html
- attrs[:title] = item.at("title").inner_html
- attrs[:pubdate] = item.at("pubDate").inner_html
- end
- end
-
-end
-
View
@@ -0,0 +1,54 @@
+require "nokogiri"
+require "date"
+
+module Parsed
+ extend self
+
+ autoload :Feed, "parsed/feed"
+ autoload :Video, "parsed/video"
+
+ FEED_URL = "http://www.escapistmagazine.com/rss/videos/list/1.xml"
+
+ def update_index(feed)
+ Feed.new(feed).videos.each do |video|
+ id = episode_id(video.guid)
+ next if Episode[id]
+
+ Episode[id] = {
+ title: video.title,
+ pubdate: Time.parse(video.pubdate),
+ video_url: video.url,
+ page_url: video.link,
+ thumb_url: nil
+ }
+ end
+ end
+
+ def build_index(input)
+ Zero.db_connection.drop_collection("episodes")
+
+ input.each_line do |line|
+ next unless line =~ /^http/
+
+ video_url, page_url, thumb_url, year, month, day, title = line.split "\t"
+
+ id = episode_id(page_url)
+ pubdate = Time.mktime(year.to_i, month.to_i, day.to_i)
+
+ Episode[id] = {
+ title: title,
+ pubdate: pubdate,
+ video_url: video_url,
+ page_url: page_url,
+ thumb_url: thumb_url
+ }
+ end
+ end
+
+ private
+
+ def episode_id(url)
+ url.match(%r{/([^/]+)$})[1].to_i
+ end
+
+end
View
@@ -0,0 +1,15 @@
+require "date"
+
+module Parsed
+ class Feed
+
+ def initialize(feed)
+ @xml = Nokogiri::XML(feed)
+ end
+
+ def videos
+ @xml.search("item").map { |item| Video.new(item) }
+ end
+
+ end
+end
View
@@ -0,0 +1,68 @@
+require "cgi"
+require "json"
+
+module Parsed
+ class Video
+
+ attr_reader :item
+
+ def initialize(item)
+ @item = item
+ end
+
+ def guid
+ item.at("guid").inner_html
+ end
+
+ def pubdate
+ item.at("pubDate").inner_html
+ end
+
+ def title
+ item.at("title").inner_html
+ end
+
+ def link
+ CGI.unescape item.at("link").inner_html
+ end
+
+ def url
+ urls.video
+ end
+
+ def thumb
+ urls.thumb
+ end
+
+ def urls
+ @urls ||= URL.new(Nokogiri::HTML(open link))
+ end
+
+ class URL
+ def initialize(page)
+ @page = page
+ end
+
+ def config_url
+ @page.at("object#player_api").at("param[name='flashvars']")["value"][7..-1]
+ end
+
+ def config
+ JSON.parse open(config_url).read.tr!("'", '"')
+ end
+
+ def urls
+ config["playlist"].map { |p| p["url"] }
+ end
+
+ def video
+ urls.find { |u| u =~ /mp4$/ }
+ end
+
+ def thumb
+ urls.find { |u| u =~ /jpg$/ }
+ end
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 452c592

Please sign in to comment.