Skip to content

Commit

Permalink
New parse_qs() to process seed_illust_ids[] liked params
Browse files Browse the repository at this point in the history
  • Loading branch information
upbit committed Mar 21, 2017
1 parent 23bd13f commit b271278
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
13 changes: 10 additions & 3 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ def appapi_recommend(aapi):
json_result = aapi.illust_recommended(**next_qs)
# print(json_result)
illust = json_result.illusts[0]
print(">>> %s, origin url: %s" % (illust.title, illust.image_urls['large']))
print(" > %s, origin url: %s" % (illust.title, illust.image_urls['large']))

json_result = aapi.illust_related(59580629)
print(json_result)
illust = json_result.illusts[0]
print(">>> %s, origin url: %s" % (illust.title, illust.image_urls['large']))

# get next page
next_qs = aapi.parse_qs(json_result.next_url)
json_result = aapi.illust_related(**next_qs)
# print(json_result)
illust = json_result.illusts[0]
print(" > %s, origin url: %s" % (illust.title, illust.image_urls['large']))

def appapi_users(aapi):
json_result = aapi.user_detail(275527)
print(json_result)
Expand Down Expand Up @@ -317,8 +324,8 @@ def main():
appapi_ranking(aapi)

# auth test
aapi.login(_USERNAME, _PASSWORD)
appapi_auth_api(aapi)
# aapi.login(_USERNAME, _PASSWORD)
# appapi_auth_api(aapi)

if __name__ == '__main__':
main()
33 changes: 25 additions & 8 deletions pixivpy3/aapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import re
import shutil
import json
import requests
Expand Down Expand Up @@ -47,13 +48,29 @@ def format_bool(self, bool_value):
def parse_qs(self, next_url):
if not next_url: return None
if sys.version_info >= (3, 0):
from urllib.parse import urlparse, parse_qs, unquote
unquote_url = unquote(next_url)
from urllib.parse import urlparse, unquote
safe_unquote = lambda s: unquote(s)
else:
from urlparse import urlparse, parse_qs, unquote
unquote_url = unquote(next_url.encode('utf8')).decode('utf8')
query = urlparse(unquote_url).query
return dict([(k,v[0]) for k,v in parse_qs(query).items()])
from urlparse import urlparse, unquote
safe_unquote = lambda s: unquote(s.encode('utf8')).decode('utf8')

result_qs = {}
query = urlparse(next_url).query
for kv in query.split('&'):
# split than unquote() to k,v strings
k, v = map(safe_unquote, kv.split('='))

# merge seed_illust_ids[] liked PHP params to array
matched = re.match('(?P<key>[\w]*)\[(?P<idx>[\w]*)\]', k)
if matched:
mk = matched.group('key')
marray = result_qs.get(mk, [])
# keep the origin sequence, just ignore group('idx')
result_qs[mk] = marray + [v]
else:
result_qs[k] = v

return result_qs

# 用户详情 (无需登录)
def user_detail(self, user_id, filter='for_ios', req_auth=False):
Expand Down Expand Up @@ -136,9 +153,9 @@ def illust_related(self, illust_id, filter='for_ios', seed_illust_ids=None, req_
'filter': filter,
}
if type(seed_illust_ids) == str:
params['seed_illust_ids'] = seed_illust_ids
params['seed_illust_ids[]'] = [seed_illust_ids]
if type(seed_illust_ids) == list:
params['seed_illust_ids'] = ",".join([ str(iid) for iid in seed_illust_ids ])
params['seed_illust_ids[]'] = seed_illust_ids
r = self.no_auth_requests_call('GET', url, params=params, req_auth=req_auth)
return self.parse_result(r)

Expand Down

0 comments on commit b271278

Please sign in to comment.