首页 > 解决方案 > 在 Cron 错误上运行 YouTube API 上传脚本

问题描述

在 Cron 上运行 YouTube API 上传脚本总是会出现此错误(取自 cron.log):

/home/ubuntu/scripts/resources/client_secret.json-youtube-v3-(['https://www.googleapis.com/auth/youtube.upload'],)
['https://www.googleapis.com/auth/youtube.upload']
Traceback (most recent call last):
  File "ytuploader.py", line 18, in <module>
    service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
  File "/home/ubuntu/scripts/server_scripts/zivcovek/Google.py", line 31, in Create_Service
    cred = flow.run_local_server()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/google_auth_oauthlib/flow.py", line 459, in run_local_server
    local_server = wsgiref.simple_server.make_server(
  File "/usr/lib/python3.8/wsgiref/simple_server.py", line 154, in make_server
    server = server_class((host, port), handler_class)
  File "/usr/lib/python3.8/socketserver.py", line 452, in __init__
    self.server_bind()
  File "/usr/lib/python3.8/wsgiref/simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)
  File "/usr/lib/python3.8/http/server.py", line 138, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.8/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

'ps -fA | grep python'输出:

root         432       1  0 17:54 ?        00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root         502       1  0 17:54 ?        00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu      1352    1351  0 18:28 ?        00:00:00 /bin/sh -c /usr/bin/python3 /home/ubuntu/scripts/server_scripts/zivcovek/yt2yt.py >> /home/ubuntu/scripts/server_scripts/zivcovek/logs/cron.log 2>&1
ubuntu      1353    1352  0 18:28 ?        00:00:00 /usr/bin/python3 /home/ubuntu/scripts/server_scripts/zivcovek/yt2yt.py
ubuntu      1634    1538  0 18:55 pts/0    00:00:00 grep --color=auto python

如果我用: 杀死 Python 服务kill -9 $(ps -A | grep python | awk '{print $1}') ,服务就会停止,但是一旦 Cron 运行脚本,我就会得到同样的错误。

我用于上传的代码如下:

    from Google import Create_Service
    from googleapiclient.http import MediaFileUpload
    
    CLIENT_SECRET_FILE = 'client_secret.json'
    API_NAME = 'youtube'
    API_VERSION = 'v3'
    SCOPES = ['https://www.googleapis.com/auth/youtube.upload']
    
    service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
    
    file = 'test.mp4'
    title = 'test'        
    video_description =  "test"
    
    request_body = {
        'snippet':{
            'categoryId': 19,
            'title': video_title,
            'description' : video_description,
        },
        'status': {
            'privacyStatus': 'unlisted',
        }
    }
    
    mediaFile = MediaFileUpload(file, chunksize=-1, resumable=True)
    
    response_upload = service.videos().insert(
        part = 'snippet, status',
        body = request_body,
        media_body = mediaFile
   ).execute()
print('Video Uploaded')

如我所见,上传代码中的“service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)”行与之前已经创建的服务 Cron 之间存在冲突。

有想法该怎么解决这个吗?也许在另一个端口上运行 Create_Service,但我该怎么做呢?

谢谢!

标签: pythonapicronerrno

解决方案


推荐阅读