fix pixiv tests
This commit is contained in:
@@ -5,10 +5,102 @@ class PixivApiClient
|
||||
|
||||
class Error < Exception ; end
|
||||
|
||||
def works(illust_id)
|
||||
# Sample response:
|
||||
# {"status":"success","response":[{"id":49270482,"title":"ツイログ","caption":null,"tags":["神崎蘭子","双葉杏","アイドルマスターシンデレラガールズ","Star!!","アイマス5000users入り"],"tools":["CLIP STUDIO PAINT"],"image_urls":{"large":"http://i3.pixiv.net/img-original/img/2015/03/14/17/53/32/49270482_p0.jpg"},"width":1200,"height":951,"stats":{"scored_count":8247,"score":81697,"views_count":191630,"favorited_count":{"public":7804,"private":745},"commented_count":182},"publicity":0,"age_limit":"all-age","created_time":"2015-03-14 17:53:32","reuploaded_time":"2015-03-14 17:53:32","user":{"id":341433,"account":"nardack","name":"Nardack","is_following":false,"is_follower":false,"is_friend":false,"is_premium":null,"profile_image_urls":{"px_50x50":"http://i1.pixiv.net/img19/profile/nardack/846482_s.jpg"},"stats":null,"profile":null},"is_manga":true,"is_liked":false,"favorite_id":0,"page_count":2,"book_style":"none","type":"illustration","metadata":{"pages":[{"image_urls":{"large":"http://i3.pixiv.net/img-original/img/2015/03/14/17/53/32/49270482_p0.jpg","medium":"http://i3.pixiv.net/c/1200x1200/img-master/img/2015/03/14/17/53/32/49270482_p0_master1200.jpg"}},{"image_urls":{"large":"http://i3.pixiv.net/img-original/img/2015/03/14/17/53/32/49270482_p1.jpg","medium":"http://i3.pixiv.net/c/1200x1200/img-master/img/2015/03/14/17/53/32/49270482_p1_master1200.jpg"}}]},"content_type":null}],"count":1}
|
||||
class WorksResponse
|
||||
attr_reader :json, :pages, :moniker, :page_count
|
||||
|
||||
def initialize(json)
|
||||
# Sample response:
|
||||
# {
|
||||
# "status": "success",
|
||||
# "response": [
|
||||
# {
|
||||
# "id": 49270482,
|
||||
# "title": "ツイログ",
|
||||
# "caption": null,
|
||||
# "tags": [
|
||||
# "神崎蘭子",
|
||||
# "双葉杏",
|
||||
# "アイドルマスターシンデレラガールズ",
|
||||
# "Star!!",
|
||||
# "アイマス5000users入り"
|
||||
# ],
|
||||
# "tools": [
|
||||
# "CLIP STUDIO PAINT"
|
||||
# ],
|
||||
# "image_urls": {
|
||||
# "large": "http://i3.pixiv.net/img-original/img/2015/03/14/17/53/32/49270482_p0.jpg"
|
||||
# },
|
||||
# "width": 1200,
|
||||
# "height": 951,
|
||||
# "stats": {
|
||||
# "scored_count": 8247,
|
||||
# "score": 81697,
|
||||
# "views_count": 191630,
|
||||
# "favorited_count": {
|
||||
# "public": 7804,
|
||||
# "private": 745
|
||||
# },
|
||||
# "commented_count": 182
|
||||
# },
|
||||
# "publicity": 0,
|
||||
# "age_limit": "all-age",
|
||||
# "created_time": "2015-03-14 17:53:32",
|
||||
# "reuploaded_time": "2015-03-14 17:53:32",
|
||||
# "user": {
|
||||
# "id": 341433,
|
||||
# "account": "nardack",
|
||||
# "name": "Nardack",
|
||||
# "is_following": false,
|
||||
# "is_follower": false,
|
||||
# "is_friend": false,
|
||||
# "is_premium": null,
|
||||
# "profile_image_urls": {
|
||||
# "px_50x50": "http://i1.pixiv.net/img19/profile/nardack/846482_s.jpg"
|
||||
# },
|
||||
# "stats": null,
|
||||
# "profile": null
|
||||
# },
|
||||
# "is_manga": true,
|
||||
# "is_liked": false,
|
||||
# "favorite_id": 0,
|
||||
# "page_count": 2,
|
||||
# "book_style": "none",
|
||||
# "type": "illustration",
|
||||
# "metadata": {
|
||||
# "pages": [
|
||||
# {
|
||||
# "image_urls": {
|
||||
# "large": "http://i3.pixiv.net/img-original/img/2015/03/14/17/53/32/49270482_p0.jpg",
|
||||
# "medium": "http://i3.pixiv.net/c/1200x1200/img-master/img/2015/03/14/17/53/32/49270482_p0_master1200.jpg"
|
||||
# }
|
||||
# },
|
||||
# {
|
||||
# "image_urls": {
|
||||
# "large": "http://i3.pixiv.net/img-original/img/2015/03/14/17/53/32/49270482_p1.jpg",
|
||||
# "medium": "http://i3.pixiv.net/c/1200x1200/img-master/img/2015/03/14/17/53/32/49270482_p1_master1200.jpg"
|
||||
# }
|
||||
# }
|
||||
# ]
|
||||
# },
|
||||
# "content_type": null
|
||||
# }
|
||||
# ],
|
||||
# "count": 1
|
||||
# }
|
||||
|
||||
@json = json
|
||||
@moniker = json["user"]["account"]
|
||||
@page_count = json["page_count"].to_i
|
||||
|
||||
if page_count > 1
|
||||
@pages = json["metadata"]["pages"].map {|x| x["image_urls"]["large"]}
|
||||
else
|
||||
@pages = [json["image_urls"]["large"]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def works(illust_id)
|
||||
headers = {
|
||||
"Referer" => "http://www.pixiv.net",
|
||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}",
|
||||
@@ -26,23 +118,24 @@ class PixivApiClient
|
||||
Net::HTTP.start(url.host, url.port, :use_ssl => true) do |http|
|
||||
resp = http.request_get(url.request_uri, headers)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
json = JSON.parse(resp.body)
|
||||
json = parse_api_json(resp.body)
|
||||
else
|
||||
raise Error.new("Pixiv API call failed (status=#{resp.code} body=#{resp.body})")
|
||||
end
|
||||
end
|
||||
|
||||
map(json)
|
||||
WorksResponse.new(json["response"][0])
|
||||
end
|
||||
|
||||
private
|
||||
def parse_api_json(body)
|
||||
json = JSON.parse(body)
|
||||
|
||||
def map(raw_json)
|
||||
{
|
||||
:moniker => raw_json["response"][0]["user"]["account"],
|
||||
:file_ext => File.extname(raw_json["response"][0]["image_urls"]["large"]),
|
||||
:page_count => raw_json["response"][0]["page_count"]
|
||||
}
|
||||
if json["status"] != "success"
|
||||
raise Error.new("Pixiv API call failed (status=#{json['status']} body=#{body})")
|
||||
end
|
||||
|
||||
json
|
||||
end
|
||||
|
||||
def access_token
|
||||
|
||||
@@ -48,9 +48,8 @@ module Sources
|
||||
moniker = get_moniker_from_url
|
||||
else
|
||||
illust_id = illust_id_from_url(url)
|
||||
get_metadata_from_papi!(illust_id) do |metadata|
|
||||
moniker = metadata[:moniker]
|
||||
end
|
||||
metadata = get_metadata_from_papi(illust_id)
|
||||
moniker = metadata.moniker
|
||||
end
|
||||
|
||||
"http://img.pixiv.net/img/#{moniker}/"
|
||||
@@ -122,19 +121,11 @@ module Sources
|
||||
# => http://i1.pixiv.net/img-original/img/2014/10/02/13/51/23/46304396_p1.png
|
||||
def rewrite_new_medium_images(thumbnail_url)
|
||||
if thumbnail_url =~ %r!/c/\d+x\d+/img-master/img/#{TIMESTAMP}/\d+_p\d+_\w+\.jpg!i
|
||||
thumbnail_url = thumbnail_url.sub(%r!/c/\d+x\d+/img-master/!i, '/img-original/')
|
||||
# => http://i1.pixiv.net/img-original/img/2014/10/02/13/51/23/46304396_p1_master1200.jpg
|
||||
|
||||
page = manga_page_from_url(@url)
|
||||
thumbnail_url = thumbnail_url.sub(%r!_p(\d+)_\w+\.jpg$!i, "_p#{page}")
|
||||
# => http://i1.pixiv.net/img-original/img/2014/10/02/13/51/23/46304396_p1
|
||||
|
||||
page = manga_page_from_url(@url).to_i
|
||||
illust_id = illust_id_from_url(@url)
|
||||
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
|
||||
|
||||
metadata = get_metadata_from_papi(illust_id)
|
||||
thumbnail_url = metadata.pages[page]
|
||||
end
|
||||
|
||||
thumbnail_url
|
||||
@@ -164,10 +155,9 @@ 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_papi!(illust_id) do |metadata|
|
||||
page_count = metadata[:page_count] || 1
|
||||
is_manga = page_count > 1
|
||||
end
|
||||
metadata = get_metadata_from_papi(illust_id)
|
||||
page_count = metadata.page_count
|
||||
is_manga = page_count > 1
|
||||
end
|
||||
|
||||
if is_manga
|
||||
@@ -314,19 +304,8 @@ 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)
|
||||
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
|
||||
yield metadata
|
||||
end
|
||||
def get_metadata_from_papi(illust_id)
|
||||
@metadata ||= PixivApiClient.new.works(illust_id)
|
||||
end
|
||||
|
||||
def illust_id_from_url(url)
|
||||
|
||||
@@ -13,7 +13,10 @@ class ArtistUrl < ActiveRecord::Base
|
||||
url = url.gsub(/^http:\/\/blog\d+\.fc2/, "http://blog.fc2")
|
||||
url = url.gsub(/^http:\/\/blog-imgs-\d+\.fc2/, "http://blog.fc2")
|
||||
url = url.gsub(/^http:\/\/blog-imgs-\d+-\w+\.fc2/, "http://blog.fc2")
|
||||
url = Sources::Site.new(url).normalize_for_artist_finder!
|
||||
begin
|
||||
url = Sources::Site.new(url).normalize_for_artist_finder!
|
||||
rescue PixivApiClient::Error
|
||||
end
|
||||
url = url.gsub(/\/+\Z/, "")
|
||||
url + "/"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user