Skip to content

Commit

Permalink
Add proxy hosts for AppAPI, use pixivlite hosts behind the Wall
Browse files Browse the repository at this point in the history
  • Loading branch information
upbit committed Apr 27, 2019
1 parent 8f53762 commit 841d959
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
42 changes: 42 additions & 0 deletions example_api_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
if sys.version_info >= (3, 0):
import imp
imp.reload(sys)
else:
reload(sys)
sys.setdefaultencoding('utf8')
sys.dont_write_bytecode = True

from pixivpy3 import *

# change _USERNAME,_PASSWORD first!
_USERNAME = "userbay"
_PASSWORD = "userpay"


def proxy_get_tokens():
aapi = AppPixivAPI(proxies={'https': 'http://127.0.0.1:1087'}, verify=False)
return aapi.login(_USERNAME, _PASSWORD)


def main():
# use proxy get tokens
tokens = proxy_get_tokens()

aapi = AppPixivAPI() # no proxy needed
aapi.set_auth(tokens.response.access_token)

# set API proxy to pixivlite.com
aapi.set_api_proxy("http://app-api.pixivlite.com")

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


if __name__ == '__main__':
main()
49 changes: 27 additions & 22 deletions pixivpy3/aapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class AppPixivAPI(BasePixivAPI):
def __init__(self, **requests_kwargs):
"""initialize requests kwargs if need be"""
super(AppPixivAPI, self).__init__(**requests_kwargs)
self.hosts = "https://app-api.pixiv.net"

def set_api_proxy(self, proxy_hosts="http://app-api.pixivlite.com"):
"""Set proxy hosts: eg pixivlite.com"""
self.hosts = proxy_hosts

# Check auth and set BearerToken to headers
def no_auth_requests_call(self, method, url, headers={}, params=None, data=None, req_auth=True):
Expand Down Expand Up @@ -76,7 +81,7 @@ def parse_qs(self, next_url):

# 用户详情
def user_detail(self, user_id, filter='for_ios', req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/detail'
url = '%s/v1/user/detail' % self.hosts
params = {
'user_id': user_id,
'filter': filter,
Expand All @@ -87,7 +92,7 @@ def user_detail(self, user_id, filter='for_ios', req_auth=True):
# 用户作品列表
# type: [illust, manga]
def user_illusts(self, user_id, type='illust', filter='for_ios', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/illusts'
url = '%s/v1/user/illusts' % self.hosts
params = {
'user_id': user_id,
'filter': filter,
Expand All @@ -102,7 +107,7 @@ def user_illusts(self, user_id, type='illust', filter='for_ios', offset=None, re
# 用户收藏作品列表
# tag: 从 user_bookmark_tags_illust 获取的收藏标签
def user_bookmarks_illust(self, user_id, restrict='public', filter='for_ios', max_bookmark_id=None, tag=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/bookmarks/illust'
url = '%s/v1/user/bookmarks/illust' % self.hosts
params = {
'user_id': user_id,
'restrict': restrict,
Expand All @@ -118,7 +123,7 @@ def user_bookmarks_illust(self, user_id, restrict='public', filter='for_ios', ma
# 关注用户的新作
# restrict: [public, private]
def illust_follow(self, restrict='public', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v2/illust/follow'
url = '%s/v2/illust/follow' % self.hosts
params = {
'restrict': restrict,
}
Expand All @@ -129,7 +134,7 @@ def illust_follow(self, restrict='public', offset=None, req_auth=True):

# 作品详情 (类似PAPI.works(),iOS中未使用)
def illust_detail(self, illust_id, req_auth=True):
url = 'https://app-api.pixiv.net/v1/illust/detail'
url = '%s/v1/illust/detail' % self.hosts
params = {
'illust_id': illust_id,
}
Expand All @@ -138,7 +143,7 @@ def illust_detail(self, illust_id, req_auth=True):

# 作品评论
def illust_comments(self, illust_id, offset=None, include_total_comments=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/illust/comments'
url = '%s/v1/illust/comments' % self.hosts
params = {
'illust_id': illust_id,
}
Expand All @@ -151,7 +156,7 @@ def illust_comments(self, illust_id, offset=None, include_total_comments=None, r

# 相关作品列表
def illust_related(self, illust_id, filter='for_ios', seed_illust_ids=None, req_auth=True):
url = 'https://app-api.pixiv.net/v2/illust/related'
url = '%s/v2/illust/related' % self.hosts
params = {
'illust_id': illust_id,
'filter': filter,
Expand All @@ -170,9 +175,9 @@ def illust_recommended(self, content_type='illust', include_ranking_label=True,
offset=None, include_ranking_illusts=None, bookmark_illust_ids=None,
include_privacy_policy=None, req_auth=True):
if (req_auth):
url = 'https://app-api.pixiv.net/v1/illust/recommended'
url = '%s/v1/illust/recommended' % self.hosts
else:
url = 'https://app-api.pixiv.net/v1/illust/recommended-nologin'
url = '%s/v1/illust/recommended-nologin' % self.hosts
params = {
'content_type': content_type,
'include_ranking_label': self.format_bool(include_ranking_label),
Expand Down Expand Up @@ -205,7 +210,7 @@ def illust_recommended(self, content_type='illust', include_ranking_label=True,
# mode (Past): [day, week, month, day_male, day_female, week_original, week_rookie,
# day_r18, day_male_r18, day_female_r18, week_r18, week_r18g]
def illust_ranking(self, mode='day', filter='for_ios', date=None, offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/illust/ranking'
url = '%s/v1/illust/ranking' % self.hosts
params = {
'mode': mode,
'filter': filter,
Expand All @@ -219,7 +224,7 @@ def illust_ranking(self, mode='day', filter='for_ios', date=None, offset=None, r

# 趋势标签 (Search - tags)
def trending_tags_illust(self, filter='for_ios', req_auth=True):
url = 'https://app-api.pixiv.net/v1/trending-tags/illust'
url = '%s/v1/trending-tags/illust' % self.hosts
params = {
'filter': filter,
}
Expand All @@ -235,7 +240,7 @@ def trending_tags_illust(self, filter='for_ios', req_auth=True):
# duration: [within_last_day, within_last_week, within_last_month]
def search_illust(self, word, search_target='partial_match_for_tags', sort='date_desc', duration=None,
filter='for_ios', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/search/illust'
url = '%s/v1/search/illust' % self.hosts
params = {
'word': word,
'search_target': search_target,
Expand All @@ -251,7 +256,7 @@ def search_illust(self, word, search_target='partial_match_for_tags', sort='date

# 作品收藏详情
def illust_bookmark_detail(self, illust_id, req_auth=True):
url = 'https://app-api.pixiv.net/v2/illust/bookmark/detail'
url = '%s/v2/illust/bookmark/detail' % self.hosts
params = {
'illust_id': illust_id,
}
Expand All @@ -260,7 +265,7 @@ def illust_bookmark_detail(self, illust_id, req_auth=True):

# 新增收藏
def illust_bookmark_add(self, illust_id, restrict='public', tags=None, req_auth=True):
url = 'https://app-api.pixiv.net/v2/illust/bookmark/add'
url = '%s/v2/illust/bookmark/add' % self.hosts
data = {
'illust_id': illust_id,
'restrict': restrict,
Expand All @@ -276,7 +281,7 @@ def illust_bookmark_add(self, illust_id, restrict='public', tags=None, req_auth=

# 删除收藏
def illust_bookmark_delete(self, illust_id, req_auth=True):
url = 'https://app-api.pixiv.net/v1/illust/bookmark/delete'
url = '%s/v1/illust/bookmark/delete' % self.hosts
data = {
'illust_id': illust_id,
}
Expand All @@ -285,7 +290,7 @@ def illust_bookmark_delete(self, illust_id, req_auth=True):

# 用户收藏标签列表
def user_bookmark_tags_illust(self, restrict='public', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/bookmark-tags/illust'
url = '%s/v1/user/bookmark-tags/illust' % self.hosts
params = {
'restrict': restrict,
}
Expand All @@ -296,7 +301,7 @@ def user_bookmark_tags_illust(self, restrict='public', offset=None, req_auth=Tru

# Following用户列表
def user_following(self, user_id, restrict='public', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/following'
url = '%s/v1/user/following' % self.hosts
params = {
'user_id': user_id,
'restrict': restrict,
Expand All @@ -309,7 +314,7 @@ def user_following(self, user_id, restrict='public', offset=None, req_auth=True)

# Followers用户列表
def user_follower(self, user_id, filter='for_ios', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/follower'
url = '%s/v1/user/follower' % self.hosts
params = {
'user_id': user_id,
'filter': filter,
Expand All @@ -322,7 +327,7 @@ def user_follower(self, user_id, filter='for_ios', offset=None, req_auth=True):

# 好P友
def user_mypixiv(self, user_id, offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v1/user/mypixiv'
url = '%s/v1/user/mypixiv' % self.hosts
params = {
'user_id': user_id,
}
Expand All @@ -334,7 +339,7 @@ def user_mypixiv(self, user_id, offset=None, req_auth=True):

# 黑名单用户
def user_list(self, user_id, filter='for_ios', offset=None, req_auth=True):
url = 'https://app-api.pixiv.net/v2/user/list'
url = '%s/v2/user/list' % self.hosts
params = {
'user_id': user_id,
'filter': filter,
Expand All @@ -347,7 +352,7 @@ def user_list(self, user_id, filter='for_ios', offset=None, req_auth=True):

# 获取ugoira信息
def ugoira_metadata(self, illust_id, req_auth=True):
url = 'https://app-api.pixiv.net/v1/ugoira/metadata'
url = '%s/v1/ugoira/metadata' % self.hosts
params = {
'illust_id': illust_id,
}
Expand All @@ -368,4 +373,4 @@ def showcase_article(self, showcase_id):
}

r = self.no_auth_requests_call('GET', url, headers=headers, params=params, req_auth=False)
return self.parse_result(r)
return self.parse_result(r)

0 comments on commit 841d959

Please sign in to comment.