Skip to content

Commit

Permalink
add catalog browsing
Browse files Browse the repository at this point in the history
  • Loading branch information
siuying committed Apr 22, 2010
1 parent 8ecf827 commit d670448
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions free.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require 'json'
require 'johnson'

HOMEPAGE = "http://comic.sky-fire.com/"
HOME_URL = "http://comic.sky-fire.com/"
CATALOG_URL = "http://comic.sky-fire.com/Catalog/"

helpers do
def find_comic_home(name)
Expand Down Expand Up @@ -59,14 +60,26 @@ def list_comic_episodes_by_thumbnail(thumb)
:url => "/#{comic_id}.json"
}
end

def parse_catalog_link(link)
page_index = link.match(/PageIndex=([0-9]+)/)[1] rescue nil
topic_index = link.match(/tid=([0-9]+)/)[1] rescue nil
{:page_index => page_index, :topic_index => topic_index }
end

def generate_local_catalog_link(options={})
page_index = options[:page_index] || "1" # page 1
topic_index = options[:topic_index] || "-1" # all topic
"/catalog.json?pid=#{page_index}&tid=#{topic_index}"
end
end

get '/' do
"No free lunch!"
end

get '/index.json' do
doc = Hpricot(open(HOMEPAGE).read)
doc = Hpricot(open(HOME_URL).read)
latest, anime, top = doc.search("table.gray_link1")

latest_list = latest.search("td a img").each.collect do |thumb|
Expand All @@ -80,6 +93,30 @@ def list_comic_episodes_by_thumbnail(thumb)
{:top => top_list, :latest => latest_list}.to_json
end

get "/catalog.json" do
page_index = params[:pid] || "1"
topic_index = params[:tid] || "-1"

doc = Hpricot(open(CATALOG_URL + "?PageIndex=#{page_index}&tid=#{topic_index}").read)
data = doc.search("ul.Comic_Pic_List").collect do |comic_block|
thumbnail, detail = comic_block.search("li")

thumbnail_url = thumbnail.search("img").attr("src")
name = detail.search(".F14PX").inner_text.strip
url = detail.search("a").attr("href")
comic_id = url.match(/\/HTML\/(.+)\//)[1] rescue nil

{:name => name, :comic_id => comic_id, :thumbnail => thumbnail_url, :url => "/#{comic_id}.json"}
end

current_page = doc.search(".pagebarCurrent").inner_text.to_i rescue 1
next_url = doc.search(".pagebarNext a").attr("href") rescue nil
next_param = parse_catalog_link(next_url)
next_url_local = generate_local_catalog_link(next_param)

{:list => data, :current_page => current_page, :next_url => next_url_local}.to_json
end

# use comic id to find episode list
get "/:comic.json" do
comic_id = params[:comic]
Expand All @@ -106,6 +143,7 @@ def list_comic_episodes_by_thumbnail(thumb)
{:sp => sp_list_links, :normal => normal_list_links}.to_json
end


# use comic id and episode id to find pages
get "/:comic/:episode.json" do
episode_id = params[:episode]
Expand Down

0 comments on commit d670448

Please sign in to comment.