fix pixiv tests

This commit is contained in:
r888888888
2014-10-20 20:49:17 -07:00
parent 260de869b1
commit 8d4c9d7955
95 changed files with 146368 additions and 31275 deletions

View File

@@ -1,5 +1,5 @@
class PixivUgoiraConverter
def convert(source_path, output_path, preview_path, frame_data)
def self.convert(source_path, output_path, preview_path, frame_data)
folder = Zip::File.new(source_path)
write_webm(folder, output_path, frame_data)
write_preview(folder, preview_path)
@@ -7,7 +7,7 @@ class PixivUgoiraConverter
RemoteFileManager.new(preview_path).distribute
end
def write_webm(folder, write_path, frame_data)
def self.write_webm(folder, write_path, frame_data)
Dir.mktmpdir do |tmpdir|
FileUtils.mkdir_p("#{tmpdir}/images")
folder.each_with_index do |file, i|
@@ -43,7 +43,7 @@ class PixivUgoiraConverter
end
end
def write_preview(folder, path)
def self.write_preview(folder, path)
Dir.mktmpdir do |tmpdir|
file = folder.first
temp_path = File.join(tmpdir, file.name)

View File

@@ -0,0 +1,17 @@
class PixivUgiraProcessor
attr_accessor :post, :frame_data, :width, :height
def load_data(hash)
@frame_data = hash[:pixiv_ugoira_frame_data]
@width = hash[:pixiv_ugoira_width]
@height = hash[:pixiv_ugoira_height]
end
def process!(post)
save_pixiv_ugoira_frame_data(post)
end
def save_pixiv_ugoira_frame_data(post)
PixivUgoiraFrameData.create(:data => frame_data.to_json, :post_id => post.id)
end
end

View File

@@ -1,8 +1,12 @@
class PixivWebAgent
def self.phpsessid(agent)
agent.cookies.select do |cookie| cookie.name == "PHPSESSID" end.first.try(:value)
end
def self.build
mech = Mechanize.new
phpsessid = Cache.get("pixiv-phpsessid")
if phpsessid
cookie = Mechanize::Cookie.new("PHPSESSID", phpsessid)
cookie.domain = ".pixiv.net"

View File

@@ -0,0 +1,4 @@
module Sources
class Error < Exception
end
end

View File

@@ -1,8 +1,6 @@
# encoding: UTF-8
module Sources
class Error < Exception ; end
class Site
attr_reader :url, :strategy
delegate :get, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :page_count, :file_url, :ugoira_frame_data, :ugoira_width, :ugoira_height, :to => :strategy

View File

@@ -250,8 +250,7 @@ module Sources
# Refer to http://danbooru.donmai.us/wiki_pages/58938 for documentation on the Pixiv API.
def get_metadata_from_spapi!(illust_id)
phpsessid = agent.cookies.select do |cookie| cookie.name == "PHPSESSID" end.first.value
spapi_url = "http://spapi.pixiv.net/iphone/illust.php?illust_id=#{illust_id}&PHPSESSID=#{phpsessid}"
spapi_url = "http://spapi.pixiv.net/iphone/illust.php?illust_id=#{illust_id}&PHPSESSID=#{PixivWebAgent.phpsessid(agent)}"
agent.get(spapi_url) do |response|
metadata = CSV.parse(response.content.force_encoding("UTF-8")).first
@@ -298,31 +297,6 @@ module Sources
raise Sources::Error.new("Couldn't get illust ID from URL: #{url}")
end
end
def agent
@agent ||= begin
mech = Mechanize.new
phpsessid = Cache.get("pixiv-phpsessid")
if phpsessid
cookie = Mechanize::Cookie.new("PHPSESSID", phpsessid)
cookie.domain = ".pixiv.net"
cookie.path = "/"
mech.cookie_jar.add(cookie)
else
mech.get("http://www.pixiv.net") do |page|
page.form_with(:action => "/login.php") do |form|
form['pixiv_id'] = Danbooru.config.pixiv_login
form['pass'] = Danbooru.config.pixiv_password
end.click_button
end
phpsessid = mech.cookie_jar.cookies.select{|c| c.name == "PHPSESSID"}.first
Cache.put("pixiv-phpsessid", phpsessid.value, 1.month) if phpsessid
end
mech
end
end
end
end
end

View File

@@ -112,7 +112,7 @@ class Upload < ActiveRecord::Base
post.distribute_files
if post.save
CurrentUser.increment!(:post_upload_count)
ugoira_service.process(post)
ugoira_service.process(post) if is_ugoira?
update_attributes(:status => "completed", :post_id => post.id)
else
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))
@@ -138,7 +138,17 @@ class Upload < ActiveRecord::Base
update_attributes(:status => "error: #{x.class} - #{x.message}", :backtrace => x.backtrace.join("\n"))
ensure
delete_temp_file
if async_conversion?
# need to delay this because we have to process the file
# before deleting it
delay(:queue => Socket.gethostname).delete_temp_file(temp_file_path)
else
delete_temp_file
end
end
def async_conversion?
is_ugoira?
end
def ugoira_service
@@ -167,8 +177,8 @@ class Upload < ActiveRecord::Base
end
module FileMethods
def delete_temp_file
FileUtils.rm_f(temp_file_path)
def delete_temp_file(path = nil)
FileUtils.rm_f(path || temp_file_path)
end
def move_file