如何加快我的130秒YouTube-api Python示例?

发布于 2025-01-22 10:15:18 字数 3346 浏览 2 评论 0原文

此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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-29 10:15:18

此答案在此处适合一个不同的措辞问题:

它回答了如何强制IPV4 - 不是如何修复IPv6。

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()


#https://stackoverflow.com/a/50044152/536874
import socket
getaddrinfo_original = socket.getaddrinfo
def getaddrinfo_decorated(*args, **kwargs):
  # filter responses that are not ipv4
  return [response for response in getaddrinfo_original(*args, **kwargs) if response[0] == socket.AF_INET]
socket.getaddrinfo = getaddrinfo_decorated

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 14:06:27.311410 request.execute() start
response type <class 'dict'>
2022-04-20 14:06:27.503453
 request.execute() end
{'kind': 'youtube#channelListResponse', 'etag': '_EY8e-LjjUX00BenLPIGIts8iS0', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'QmXjB1Hz-b5QX1ahMELzX1BTl6I', 'id': 'UC-lHJZR3Gqxm24_Vd_AJ5Yw', 'statistics': {'viewCount': '28263689871', 'subscriberCount': '111000000', 'hiddenSubscriberCount': False, 'videoCount': '4472'}}]}

这是200ms的调用。
一个更好的答案将包括如何查找和修复我网络上IPv6的问题。

This answer to a differently worded question fits here:

It answers how to force ipv4 - not how to fix ipv6.

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()


#https://stackoverflow.com/a/50044152/536874
import socket
getaddrinfo_original = socket.getaddrinfo
def getaddrinfo_decorated(*args, **kwargs):
  # filter responses that are not ipv4
  return [response for response in getaddrinfo_original(*args, **kwargs) if response[0] == socket.AF_INET]
socket.getaddrinfo = getaddrinfo_decorated

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)

The updated code now outputs:

youtube type <class 'googleapiclient.discovery.Resource'>
request type <class 'googleapiclient.http.HttpRequest'>

2022-04-20 14:06:27.311410 request.execute() start
response type <class 'dict'>
2022-04-20 14:06:27.503453
 request.execute() end
{'kind': 'youtube#channelListResponse', 'etag': '_EY8e-LjjUX00BenLPIGIts8iS0', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'QmXjB1Hz-b5QX1ahMELzX1BTl6I', 'id': 'UC-lHJZR3Gqxm24_Vd_AJ5Yw', 'statistics': {'viewCount': '28263689871', 'subscriberCount': '111000000', 'hiddenSubscriberCount': False, 'videoCount': '4472'}}]}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文