From dc31e74809f0c43bebe1e5674c08c3de3ae375b7 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 26 Sep 2011 17:30:35 -0400 Subject: [PATCH] added tinami support --- Gemfile | 1 + Gemfile.lock | 2 + app/logical/sources/strategies/base.rb | 5 +- app/logical/sources/strategies/pixiv.rb | 22 ++++--- app/logical/sources/strategies/tinami.rb | 79 +++++++++++++++++++++--- test/unit/sources/site_test.rb | 20 ++++++ 6 files changed, 106 insertions(+), 23 deletions(-) diff --git a/Gemfile b/Gemfile index 91d473567..c4b5d9aaa 100644 --- a/Gemfile +++ b/Gemfile @@ -33,4 +33,5 @@ gem 'rmagick', :require => "RMagick" group :development do gem 'pry' + gem 'awesome_print' end diff --git a/Gemfile.lock b/Gemfile.lock index 657ce30ab..1a4360389 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,7 @@ GEM activesupport (3.1.0) multi_json (~> 1.0) arel (2.2.1) + awesome_print (0.4.0) bcrypt-ruby (3.0.1) builder (3.0.0) chronic (0.6.4) @@ -172,6 +173,7 @@ PLATFORMS ruby DEPENDENCIES + awesome_print delayed_job factory_girl ffaker! diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index 16ae39768..f9d7e6f21 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -1,7 +1,7 @@ module Sources module Strategies class Base - attr_reader :url, :agent + attr_reader :url def self.url_match?(url) false @@ -9,7 +9,6 @@ module Sources def initialize(url) @url = url - @agent = create_agent end def get @@ -49,7 +48,7 @@ module Sources end protected - def create_agent + def agent raise NotImplementedError end end diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 72149b786..78aa51b27 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -81,18 +81,20 @@ module Sources end end - def create_agent - mech = Mechanize.new + def agent + @agent ||= begin + mech = Mechanize.new - mech.get("http://www.pixiv.net") do |page| - page.form_with(:action => "/login.php") do |form| - form['mode'] = "login" - form['login_pixiv_id'] = "uroobnad" - form['pass'] = "uroobnad556" - end.click_button + mech.get("http://www.pixiv.net") do |page| + page.form_with(:action => "/login.php") do |form| + form['mode'] = "login" + form['login_pixiv_id'] = "uroobnad" + form['pass'] = "uroobnad556" + end.click_button + end + + mech end - - mech end end end diff --git a/app/logical/sources/strategies/tinami.rb b/app/logical/sources/strategies/tinami.rb index 53b010975..90dc86056 100644 --- a/app/logical/sources/strategies/tinami.rb +++ b/app/logical/sources/strategies/tinami.rb @@ -1,28 +1,87 @@ module Sources module Strategies class Tinami < Base + attr_reader :artist_name, :profile_url, :image_url, :tags + + def self.url_match?(url) + url =~ /^https?:\/\/(?:\w+\.)?tinami\.com/ + end + def site_name "Tinami" end - def artist_name - "?" + def get + url = URI.parse(normalized_url).request_uri + agent.get(url) do |page| + @artist_name, @profile_url = get_profile_from_page(page) + @image_url = get_image_url_from_page(page) + @tags = get_tags_from_page(page) + end end - def profile_url + def normalized_url url end - def image_url - url - end - - def tags - [] + def unique_id + profile_url =~ /\/profile\/(\d+)/ + "tinami" + $1 end protected - def create_agent + def get_profile_from_page(page) + links = page.search("div.prof a") + + if links.any? + profile_url = "http://www.tinami.com" + links[0]["href"] + else + profile_url = nil + end + + links = page.search("div.prof p a strong") + + if links.any? + artist_name = links[0].text + else + artist_name = nil + end + + return [artist_name, profile_url].compact + end + + def get_image_url_from_page(page) + img = page.search("img.captify[rel=caption]").first + if img + img.attr("src") + else + nil + end + end + + def get_tags_from_page(page) + links = page.search("div.tag a") + + links.map do |node| + [node.text, "http://www.tinami.com" + node.attr("href")] + end + end + + def agent + @agent ||= begin + mech = Mechanize.new + + mech.get("http://www.tinami.com/login") do |page| + page.form_with do |form| + form["action_login"] = "true" + form['username'] = "r888888888@gmail.com" + form['password'] = "uroobnad556" + form["rem"] = "1" + end.click_button + end + + mech + end end end end diff --git a/test/unit/sources/site_test.rb b/test/unit/sources/site_test.rb index 92adb043b..f64b8d3d1 100644 --- a/test/unit/sources/site_test.rb +++ b/test/unit/sources/site_test.rb @@ -5,6 +5,26 @@ require 'test_helper' module Sources class SiteTest < ActiveSupport::TestCase context "The source site" do + context "for tinami" do + setup do + @site = Sources::Site.new("http://www.tinami.com/view/308311") + @site.get + end + + should "get a single post" do + assert_equal("http://www.tinami.com/creator/profile/29399", @site.profile_url) + assert_match(/ROM/, @site.artist_name) + assert_equal("http://img.tinami.com/illust2/img/336/4e80b9773c084.png", @site.image_url) + assert(@site.tags.size > 0) + end + + should "convert a page into a json representation" do + assert_nothing_raised do + @site.to_json + end + end + end + context "for pixiv" do setup do @site = Sources::Site.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484")