support for twitter downloads
This commit is contained in:
@@ -6,7 +6,7 @@ module Downloads
|
||||
end
|
||||
|
||||
def self.strategies
|
||||
[Pixiv, NicoSeiga, Twitpic, DeviantArt, Tumblr, Moebooru]
|
||||
[Pixiv, NicoSeiga, Twitpic, DeviantArt, Tumblr, Moebooru, Twitter]
|
||||
end
|
||||
|
||||
def rewrite(url, headers, data = {})
|
||||
|
||||
27
app/logical/downloads/rewrite_strategies/twitter.rb
Normal file
27
app/logical/downloads/rewrite_strategies/twitter.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Downloads
|
||||
module RewriteStrategies
|
||||
class Twitter < Base
|
||||
def rewrite(url, headers, data = {})
|
||||
if url =~ %r!^https?://(?:mobile\.)?twitter\.com!
|
||||
url, headers = rewrite_image_url(url, headers)
|
||||
end
|
||||
|
||||
return [url, headers, data]
|
||||
end
|
||||
|
||||
protected
|
||||
def rewrite_image_url(url, headers)
|
||||
# example: http://twitter.com/status
|
||||
url = url.sub(%r!^https?://twitter\.com!, "http://mobile.twitter.com")
|
||||
|
||||
if url =~ %r!^https?://mobile\.twitter\.com/\w+/status/\d+!
|
||||
source = ::Sources::Strategies::Twitter.new(url)
|
||||
source.get
|
||||
url = source.image_url
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -6,7 +6,7 @@ module Sources
|
||||
delegate :get, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :page_count, :file_url, :ugoira_frame_data, :to => :strategy
|
||||
|
||||
def self.strategies
|
||||
[Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt, Strategies::Nijie]
|
||||
[Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt, Strategies::Nijie, Strategies::Twitter]
|
||||
end
|
||||
|
||||
def initialize(url)
|
||||
|
||||
53
app/logical/sources/strategies/twitter.rb
Normal file
53
app/logical/sources/strategies/twitter.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
module Sources::Strategies
|
||||
class Twitter < Base
|
||||
def self.url_match?(url)
|
||||
url =~ %r!https?://mobile\.twitter\.com/\w+/status/\d+!
|
||||
end
|
||||
|
||||
def tags
|
||||
[]
|
||||
end
|
||||
|
||||
def site_name
|
||||
"Twitter"
|
||||
end
|
||||
|
||||
def get
|
||||
agent.get(url) do |page|
|
||||
@artist_name, @profile_url = get_profile_from_page(page)
|
||||
@image_url = get_image_url_from_page(page)
|
||||
end
|
||||
end
|
||||
|
||||
def get_profile_from_page(page)
|
||||
links = page.search("a.profile-link")
|
||||
if links.any?
|
||||
profile_url = "https://twitter.com" + links[0]["href"]
|
||||
artist_name = links[0].search("span")[0].text
|
||||
else
|
||||
profile_url = nil
|
||||
artist_name = nil
|
||||
end
|
||||
|
||||
return [artist_name, profile_url].compact
|
||||
end
|
||||
|
||||
def get_image_url_from_page(page)
|
||||
divs = page.search("div.media")
|
||||
|
||||
if divs.any?
|
||||
image_url = divs.search("img")[0]["src"] + ":large"
|
||||
else
|
||||
image_url = nil
|
||||
end
|
||||
|
||||
return image_url
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def agent
|
||||
@agent ||= Mechanize.new
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user