Skip to content

Commit

Permalink
Merge pull request #114 from upbit/add.bapi.dnsbackup
Browse files Browse the repository at this point in the history
Add backup dns for require_appapi_hosts()
  • Loading branch information
upbit authored Jan 29, 2020
2 parents e718dfe + d8e1e2f commit 9c6bce7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
9 changes: 5 additions & 4 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ def appapi_illust(aapi):
json_result = aapi.illust_comments(59580629)
print(json_result)

json_result = aapi.ugoira_metadata(51815717)
print(json_result)
metadata = json_result.ugoira_metadata
print(">>> frames=%d %s" % (len(metadata.frames), metadata.zip_urls.medium))
# (2020/01/28) Comment because 51815717 is deleted
# json_result = aapi.ugoira_metadata(51815717)
# print(json_result)
# metadata = json_result.ugoira_metadata
# print(">>> frames=%d %s" % (len(metadata.frames), metadata.zip_urls.medium))


def appapi_recommend(aapi):
Expand Down
24 changes: 20 additions & 4 deletions pixivpy3/bapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ def __init__(self, **requests_kwargs):
session.mount('https://', host_header_ssl.HostHeaderSSLAdapter())
self.requests = session

def require_appapi_hosts(self, hostname="app-api.pixiv.net"):
def require_appapi_hosts(self, hostname="app-api.pixiv.net", timeout=3):
"""
通过1.0.0.1请求真实的ip地址
通过cloudflare的 DNS over HTTPS 请求真实的ip地址
"""
url = "https://1.0.0.1/dns-query?ct=application/dns-json&name=%s&type=A&do=false&cd=false" % hostname
response = requests.get(url)
url = "https://1.0.0.1/dns-query" # 先使用1.0.0.1的地址
params = {
'ct': 'application/dns-json',
'name': hostname,
'type': 'A',
'do': 'false',
'cd': 'false',
}

try:
response = requests.get(url, params=params, timeout=timeout)
except Exception:
# 根据 #111 的反馈,部分地区无法访问1.0.0.1,此时尝试域名解析
url = "https://cloudflare-dns.com/dns-query"
response = requests.get(url, params=params, timeout=timeout)

# 返回第一个解析到的IP
self.hosts = "https://" + response.json()['Answer'][0]['data']
return self.hosts

0 comments on commit 9c6bce7

Please sign in to comment.