如何加快我的130秒YouTube-api Python示例?
此Python片段:
from datetime import datetime
from googleapiclient.discovery import build
import googleapiclient
import socket
with open("secret_youtube_api_key", "r") as api_key_file:
api_key = api_key_file.read()
socket.setdefaulttimeout(30000)
youtube : googleapiclient.discovery.Resource = build('youtube', 'v3', developerKey=api_key)
print("youtube type " + str(type(youtube)))
request : googleapiclient.http.HttpRequest = youtube.channels().list(
part='statistics',
forUsername='PewDiePie'
)
print("request type " + str(type(request)))
print("\n" + str(datetime.now()) + " request.execute() start")
response = request.execute()
print("response type " + str(type(response)))
print(str(datetime.now()) + "\n request.execute() end")
print(response)
给出以下输出:
youtube type <class 'googleapiclient.discovery.Resource'>
request type <class 'googleapiclient.http.HttpRequest'>
2022-04-20 03:22:35.541216 request.execute() start
response type <class 'dict'>
2022-04-20 03:24:46.061762
request.execute() end
{'kind': 'youtube#channelListResponse', 'etag': '01QNwqKcy80m97clcBBjfozMTV0', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'O5elmeewmtTIKBg8LuPaid7kDFs', 'id': 'UC-lHJZR3Gqxm24_Vd_AJ5Yw', 'statistics': {'viewCount': '28262284610', 'subscriberCount': '111000000', 'hiddenSubscriberCount': False, 'videoCount': '4472'}}]}
实际呼叫执行的时间戳定期为Quiite,相距130秒。我使用WGET的控制命令提示
wget --timeout=300000 "https://www.googleapis.com/youtube/v3/channels?key=$APP_KEY&forUsername=PewDiePie&part=statistics"
--2022-04-20 03:24:44-- https://www.googleapis.com/youtube/v3/channels?key=REDACTED&forUsername=PewDiePie&part=statistics
Resolving www.googleapis.com (www.googleapis.com)... 2a00:1450:4001:810::200a, 2a00:1450:4001:811::200a, 2a00:1450:4001:80e::200a, ...
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:810::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:811::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:80e::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:80f::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|142.250.185.170|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: ‘channels?key=REDACTED&forUsername=PewDiePie&part=statistics’
channels?key=REDACTED [ <=> ] 476 --.-KB/s in 0s
2022-04-20 03:33:28 (18,7 MB/s) - ‘channels?key=REDACTED&forUsername=PewDiePie&part=statistics’ saved [476]
这似乎是一个常见的问题,没有明确的解决方案。 有相关问题( link )的答案不足。
看起来IPv6通常只是失败。 我的尽头可能是什么问题。 我该如何解决IPv6问题? 鉴于我的结局可能是不可用的 - 我需要如何修改我的Python代码以排除IPv6?
在WGET上强迫IPv4立即返回:wget -4 -4-pimeout = 300000“ https://www.googleapis.com/youtube/youtube/v3/channels?key=jure=qunport_keykey&amp_key.代码>
This python snippet:
from datetime import datetime
from googleapiclient.discovery import build
import googleapiclient
import socket
with open("secret_youtube_api_key", "r") as api_key_file:
api_key = api_key_file.read()
socket.setdefaulttimeout(30000)
youtube : googleapiclient.discovery.Resource = build('youtube', 'v3', developerKey=api_key)
print("youtube type " + str(type(youtube)))
request : googleapiclient.http.HttpRequest = youtube.channels().list(
part='statistics',
forUsername='PewDiePie'
)
print("request type " + str(type(request)))
print("\n" + str(datetime.now()) + " request.execute() start")
response = request.execute()
print("response type " + str(type(response)))
print(str(datetime.now()) + "\n request.execute() end")
print(response)
gives the following output:
youtube type <class 'googleapiclient.discovery.Resource'>
request type <class 'googleapiclient.http.HttpRequest'>
2022-04-20 03:22:35.541216 request.execute() start
response type <class 'dict'>
2022-04-20 03:24:46.061762
request.execute() end
{'kind': 'youtube#channelListResponse', 'etag': '01QNwqKcy80m97clcBBjfozMTV0', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'O5elmeewmtTIKBg8LuPaid7kDFs', 'id': 'UC-lHJZR3Gqxm24_Vd_AJ5Yw', 'statistics': {'viewCount': '28262284610', 'subscriberCount': '111000000', 'hiddenSubscriberCount': False, 'videoCount': '4472'}}]}
The timestamp for the actual call execution are quiite regularly 130 seconds apart. My control command using wget gives a hint
wget --timeout=300000 "https://www.googleapis.com/youtube/v3/channels?key=$APP_KEY&forUsername=PewDiePie&part=statistics"
--2022-04-20 03:24:44-- https://www.googleapis.com/youtube/v3/channels?key=REDACTED&forUsername=PewDiePie&part=statistics
Resolving www.googleapis.com (www.googleapis.com)... 2a00:1450:4001:810::200a, 2a00:1450:4001:811::200a, 2a00:1450:4001:80e::200a, ...
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:810::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:811::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:80e::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|2a00:1450:4001:80f::200a|:443... failed: Connection timed out.
Connecting to www.googleapis.com (www.googleapis.com)|142.250.185.170|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: ‘channels?key=REDACTED&forUsername=PewDiePie&part=statistics’
channels?key=REDACTED [ <=> ] 476 --.-KB/s in 0s
2022-04-20 03:33:28 (18,7 MB/s) - ‘channels?key=REDACTED&forUsername=PewDiePie&part=statistics’ saved [476]
This seems to be a common problem without a clear solution.
There are related questions (link) that fall short in their answer.
It looks like IPv6 just generally fails.
What could be wrong on my end.
How would I fix IPV6 issues on my end?
Given that my end may be unfixable - how would I need to modify my python code to exclude IPV6?
forcing ipv4 on wget returns instantly: wget -4 --timeout=300000 "https://www.googleapis.com/youtube/v3/channels?key=$APP_KEY&forUsername=PewDiePie&part=statistics"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此答案在此处适合一个不同的措辞问题:
它回答了如何强制IPV4 - 不是如何修复IPv6。
现在更新的代码输出:
这是200ms的调用。
一个更好的答案将包括如何查找和修复我网络上IPv6的问题。
This answer to a differently worded question fits here:
It answers how to force ipv4 - not how to fix ipv6.
The updated code now outputs:
Which is a 200ms call like it should be.
A better answer would include how to find and fix whatever is wrong with ipv6 on my network.