first cut implementation at using public pixiv api

This commit is contained in:
r888888888
2015-05-11 15:19:05 -07:00
parent 721df95832
commit 17cc8735ad
2 changed files with 79 additions and 44 deletions

View File

@@ -48,8 +48,8 @@ module Sources
moniker = get_moniker_from_url
else
illust_id = illust_id_from_url(url)
get_metadata_from_spapi!(illust_id) do |metadata|
moniker = metadata[24]
get_metadata_from_papi!(illust_id) do |metadata|
moniker = metadata[:moniker]
end
end
@@ -81,7 +81,7 @@ module Sources
end
def rewrite_thumbnails(thumbnail_url, is_manga=nil)
# thumbnail_url = rewrite_new_medium_images(thumbnail_url)
thumbnail_url = rewrite_new_medium_images(thumbnail_url)
thumbnail_url = rewrite_medium_ugoiras(thumbnail_url)
thumbnail_url = rewrite_old_small_and_medium_images(thumbnail_url, is_manga)
return thumbnail_url
@@ -130,8 +130,8 @@ module Sources
# => http://i1.pixiv.net/img-original/img/2014/10/02/13/51/23/46304396_p1.
illust_id = illust_id_from_url(@url)
get_metadata_from_spapi!(illust_id) do |metadata|
file_ext = metadata[2]
get_metadata_from_papi!(illust_id) do |metadata|
file_ext = metadata[:file_ext]
thumbnail_url += file_ext
# => http://i1.pixiv.net/img-original/img/2014/10/02/13/51/23/46304396_p1.png
end
@@ -164,8 +164,8 @@ module Sources
if thumbnail_url =~ %r!/img/#{MONIKER}/\d+_[ms]\.#{EXT}!i
if is_manga.nil?
illust_id = illust_id_from_url(@url)
get_metadata_from_spapi!(illust_id) do |metadata|
page_count = metadata[19].to_i || 1
get_metadata_from_papi!(illust_id) do |metadata|
page_count = metadata[:page_count] || 1
is_manga = page_count > 1
end
end
@@ -314,53 +314,21 @@ module Sources
"http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{illust_id}"
end
def get_metadata_from_papi!(illust_id)
client = PixivApiClient.new
yield client.works(illust_id)
end
# Refer to http://danbooru.donmai.us/wiki_pages/58938 for documentation on the Pixiv API.
def get_metadata_from_spapi!(illust_id)
yield []
return
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
validate_spapi_metadata!(metadata)
yield metadata
end
end
def validate_spapi_metadata!(metadata)
if metadata.nil?
raise Sources::Error.new("Pixiv API returned empty response.")
elsif metadata.size != 31
raise Sources::Error.new("Pixiv API returned unexpected number of fields.")
end
illust_id = metadata[0]
file_ext = metadata[2]
page_count = metadata[19]
moniker = metadata[24]
mobile_profile_image = metadata[30]
if file_ext !~ /#{EXT}/i
raise Sources::Error.new("Pixiv API returned unexpected file extension '#{file_ext}' for pixiv ##{illust_id}.")
elsif moniker !~ /#{MONIKER}/i
raise Sources::Error.new("Pixiv API returned invalid artist moniker '#{moniker}' for pixiv ##{illust_id}.")
elsif page_count.to_s !~ /[0-9]*/i
raise Sources::Error.new("Pixiv API returned invalid page count '#{page_count}' for pixiv ##{illust_id}.")
end
if mobile_profile_image
# http://i1.pixiv.net/img01/profile/ccz67420/mobile/5042957_80.jpg
profile_regex = %r!i[12]\.pixiv\.net/img\d+/profile/#{MONIKER}/mobile/\d+_\d+\.jpg!i
mobile_moniker = mobile_profile_image.match(profile_regex)[1]
if mobile_moniker != moniker
raise Sources::Error.new("Pixiv API returned inconsistent artist moniker '#{moniker}' for pixiv ##{illust_id}.")
end
end
end
def illust_id_from_url(url)
# http://img18.pixiv.net/img/evazion/14901720.png
#