added tinami support
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -33,4 +33,5 @@ gem 'rmagick', :require => "RMagick"
|
|||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
gem 'pry'
|
gem 'pry'
|
||||||
|
gem 'awesome_print'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ GEM
|
|||||||
activesupport (3.1.0)
|
activesupport (3.1.0)
|
||||||
multi_json (~> 1.0)
|
multi_json (~> 1.0)
|
||||||
arel (2.2.1)
|
arel (2.2.1)
|
||||||
|
awesome_print (0.4.0)
|
||||||
bcrypt-ruby (3.0.1)
|
bcrypt-ruby (3.0.1)
|
||||||
builder (3.0.0)
|
builder (3.0.0)
|
||||||
chronic (0.6.4)
|
chronic (0.6.4)
|
||||||
@@ -172,6 +173,7 @@ PLATFORMS
|
|||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
awesome_print
|
||||||
delayed_job
|
delayed_job
|
||||||
factory_girl
|
factory_girl
|
||||||
ffaker!
|
ffaker!
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module Sources
|
module Sources
|
||||||
module Strategies
|
module Strategies
|
||||||
class Base
|
class Base
|
||||||
attr_reader :url, :agent
|
attr_reader :url
|
||||||
|
|
||||||
def self.url_match?(url)
|
def self.url_match?(url)
|
||||||
false
|
false
|
||||||
@@ -9,7 +9,6 @@ module Sources
|
|||||||
|
|
||||||
def initialize(url)
|
def initialize(url)
|
||||||
@url = url
|
@url = url
|
||||||
@agent = create_agent
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get
|
def get
|
||||||
@@ -49,7 +48,7 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def create_agent
|
def agent
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -81,18 +81,20 @@ module Sources
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_agent
|
def agent
|
||||||
mech = Mechanize.new
|
@agent ||= begin
|
||||||
|
mech = Mechanize.new
|
||||||
|
|
||||||
mech.get("http://www.pixiv.net") do |page|
|
mech.get("http://www.pixiv.net") do |page|
|
||||||
page.form_with(:action => "/login.php") do |form|
|
page.form_with(:action => "/login.php") do |form|
|
||||||
form['mode'] = "login"
|
form['mode'] = "login"
|
||||||
form['login_pixiv_id'] = "uroobnad"
|
form['login_pixiv_id'] = "uroobnad"
|
||||||
form['pass'] = "uroobnad556"
|
form['pass'] = "uroobnad556"
|
||||||
end.click_button
|
end.click_button
|
||||||
|
end
|
||||||
|
|
||||||
|
mech
|
||||||
end
|
end
|
||||||
|
|
||||||
mech
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,28 +1,87 @@
|
|||||||
module Sources
|
module Sources
|
||||||
module Strategies
|
module Strategies
|
||||||
class Tinami < Base
|
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
|
def site_name
|
||||||
"Tinami"
|
"Tinami"
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def profile_url
|
def normalized_url
|
||||||
url
|
url
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_url
|
def unique_id
|
||||||
url
|
profile_url =~ /\/profile\/(\d+)/
|
||||||
end
|
"tinami" + $1
|
||||||
|
|
||||||
def tags
|
|
||||||
[]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,6 +5,26 @@ require 'test_helper'
|
|||||||
module Sources
|
module Sources
|
||||||
class SiteTest < ActiveSupport::TestCase
|
class SiteTest < ActiveSupport::TestCase
|
||||||
context "The source site" do
|
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
|
context "for pixiv" do
|
||||||
setup do
|
setup do
|
||||||
@site = Sources::Site.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484")
|
@site = Sources::Site.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484")
|
||||||
|
|||||||
Reference in New Issue
Block a user