Add get_metadata_from_spapi!

This commit is contained in:
evazion
2014-10-03 20:53:58 -05:00
parent 7f98b370ec
commit f889dbf10f

View File

@@ -1,5 +1,7 @@
# encoding: UTF-8
require 'csv'
module Sources
module Strategies
class Pixiv < Base
@@ -123,6 +125,57 @@ module Sources
end
end
# 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}"
agent.get(spapi_url) do |response|
metadata = CSV.parse(response.content.force_encoding("UTF-8")).first
if metadata.nil?
raise "Couldn't get Pixiv API metadata from #{spapi_url}."
else
yield metadata
end
end
end
def illust_id_from_url(url)
# http://img18.pixiv.net/img/evazion/14901720.png
#
# http://i2.pixiv.net/img18/img/evazion/14901720.png
# http://i2.pixiv.net/img18/img/evazion/14901720_m.png
# http://i2.pixiv.net/img18/img/evazion/14901720_s.png
# http://i1.pixiv.net/img07/img/pasirism/18557054_p1.png
# http://i1.pixiv.net/img07/img/pasirism/18557054_big_p1.png
#
# http://i1.pixiv.net/img-inf/img/2011/05/01/23/28/04/18557054_64x64.jpg
# http://i1.pixiv.net/img-inf/img/2011/05/01/23/28/04/18557054_s.png
#
# http://i1.pixiv.net/c/600x600/img-master/img/2014/10/02/13/51/23/46304396_p0_master1200.jpg
# http://i1.pixiv.net/img-original/img/2014/10/02/13/51/23/46304396_p0.png
#
# http://i1.pixiv.net/img-zip-ugoira/img/2014/10/03/17/29/16/46323924_ugoira1920x1080.zip
if url =~ %r!/(\d+)(?:_\w+)?\.(?:jpg|jpeg|png|gif|zip)!i
$1
# http://www.pixiv.net/member_illust.php?mode=medium&illust_id=18557054
# http://www.pixiv.net/member_illust.php?mode=big&illust_id=18557054
# http://www.pixiv.net/member_illust.php?mode=manga&illust_id=18557054
# http://www.pixiv.net/member_illust.php?mode=manga_big&illust_id=18557054&page=1
elsif url =~ /illust_id=(\d+)/i
$1
# http://www.pixiv.net/i/18557054
elsif url =~ %r!pixiv\.net/i/(\d+)!i
$1
else
raise "Couldn't get illust ID from URL: #{url}"
end
end
def agent
@agent ||= begin
mech = Mechanize.new