LA_*_*LA_ 2 python datetime youtube-api datetime-format
我正在 OSX 上运行 python 脚本,将视频文件 ( single_file
) 上传到 YouTube:
# define recording date as date of file modification
# https://developers.google.com/youtube/v3/docs/videos#resource
recordingDate = datetime.fromtimestamp(os.path.getctime(single_file)).isoformat("T")+"Z"
# define video title as file name
filename, file_extension = os.path.splitext(os.path.basename(single_file))
try:
initialize_upload(youtube, args, single_file, title, recordingDate)
except HttpError, e:
print " An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Run Code Online (Sandbox Code Playgroud)
在某些情况下,它运行良好,但在其他情况下,Google 返回以下错误 -
Invalid value for: Invalid format: \"2017-09-22T22:50:55Z\" is malformed at \"Z\"
Run Code Online (Sandbox Code Playgroud)
我应该如何修复它才能从文件中获取正确的日期?YouTube 期望该值采用 ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) 格式。
您在问题中共享的链接清楚地说明了格式
该值以 ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) 格式指定。
所以你的问题是,当微秒信息不可用时,isoformat 将没有微秒。下面的代码显示了差异
>>> current_date = datetime.now()
>>> current_date.isoformat()
'2018-05-20T10:18:26.785085'
>>> current_date.replace(microsecond=0).isoformat()
'2018-05-20T10:18:26'
Run Code Online (Sandbox Code Playgroud)
因此,对于它起作用的文件来说,microsecond
将会出现非零。所以解决办法很简单
recordingDate = datetime.fromtimestamp(os.path.getctime(single_file)).replace(microsecond=0).isoformat("T")+".0Z"
Run Code Online (Sandbox Code Playgroud)
这将确保微秒始终被截断并设置为.0
稍后的值
归档时间: |
|
查看次数: |
1463 次 |
最近记录: |