sea*_*ean 9 python google-api youtube-api argparse
我想我不理解python的argparse基本知识.
我正在尝试将Google YouTube API用于python脚本,但我不了解如何在不使用命令行的情况下将值传递给脚本.
例如,这里是对API的例子.github和其他地方的示例将此示例显示为从命令行调用,在调用脚本时从该命令行传递argparse值.
我不想使用命令行.我正在构建一个使用装饰器获取用户登录凭据的应用程序,当该用户想要上传到他们的YouTube帐户时,他们会提交一个表单,然后调用此脚本并将argparse值传递给它.
如何从另一个python脚本将值传递给argparser(请参阅下面的YouTube上传API脚本中的部分代码)?
if __name__ == '__main__':
argparser.add_argument("--file", required=True, help="Video file to upload")
argparser.add_argument("--title", help="Video title", default="Test Title")
argparser.add_argument("--description", help="Video description",
default="Test Description")
argparser.add_argument("--category", default="22",
help="Numeric video category. " +
"See https://developers.google.com/youtube/v3/docs/videoCategories/list")
argparser.add_argument("--keywords", help="Video keywords, comma separated",
default="")
argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
args = argparser.parse_args()
if not os.path.exists(args.file):
exit("Please specify a valid file using the --file= parameter.")
youtube = get_authenticated_service(args)
try:
initialize_upload(youtube, args)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Run Code Online (Sandbox Code Playgroud)
编辑:每个请求,这里是我使用标准方法初始化字典或使用argparse创建字典的400错误的回溯.我以为我是因为形成错误的参数而得到这个,但也许不是:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\...\testapp\oauth2client\appengine.py", line 796, in setup_oauth
resp = method(request_handler, *args, **kwargs)
File "C:\Users\...\testapp\testapp.py", line 116, in get
resumable_upload(insert_request)
File "C:\Users\...\testapp\testapp.py", line 183, in resumable_upload
status, response = insert_request.next_chunk()
File "C:\Users\...\testapp\oauth2client\util.py", line 129, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\...\testapp\apiclient\http.py", line 874, in next_chunk
return self._process_response(resp, content)
File "C:\Users\...\testapp\apiclient\http.py", line 901, in _process_response
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/youtube/v3/videos?alt=json&part=status%2Csnippet&uploadType=resumable returned "Bad Request">
Run Code Online (Sandbox Code Playgroud)
无论它是否是最好的方法,你真的要弄清楚.但是使用没有命令行的argparse很容易.我一直这样做,因为我有可以从命令行运行的批处理.或者也可以通过其他代码调用 - 这对于单元测试非常有用,如上所述.例如,argparse特别擅长默认参数.
从您的样本开始.
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument("--file", required=True, help="Video file to upload")
argparser.add_argument("--title", help="Video title", default="Test Title")
argparser.add_argument("--description", help="Video description",
default="Test Description")
argparser.add_argument("--category", default="22",
help="Numeric video category. " +
"See https://developers.google.com/youtube/v3/docs/videoCategories/list")
argparser.add_argument("--keywords", help="Video keywords, comma separated",
default="")
VALID_PRIVACY_STATUSES = ("private","public")
argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
#pass in any positional or required variables.. as strings in a list
#which corresponds to sys.argv[1:]. Not a string => arcane errors.
args = argparser.parse_args(["--file", "myfile.avi"])
#you can populate other optional parameters, not just positionals/required
#args = argparser.parse_args(["--file", "myfile.avi", "--title", "my title"])
print vars(args)
#modify them as you see fit, but no more validation is taking place
#so best to use parse_args.
args.privacyStatus = "some status not in choices - already parsed"
args.category = 42
print vars(args)
#proceed as before, the system doesn't care if it came from the command line or not
# youtube = get_authenticated_service(args)
Run Code Online (Sandbox Code Playgroud)
输出:
{'category': '22', 'description': 'Test Description', 'title': 'Test Title', 'privacyStatus': 'private', 'file': 'myfile.avi', 'keywords': ''}
{'category': 42, 'description': 'Test Description', 'title': 'Test Title', 'privacyStatus': 'some status not in choices - already parsed', 'file': 'myfile.avi', 'keywords': ''}
Run Code Online (Sandbox Code Playgroud)
parse_args用自己的字符串列表进行调用是一种常见的argparse测试方法。如果您不提供parse_args此列表,它将使用sys.argv[1:]-即shell给出的字符串。 sys.argv[0]是地带名称。
args = argparser.parse_args(['--foo','foovalue','barvalue'])
Run Code Online (Sandbox Code Playgroud)
构造args对象也很容易。
args = argparse.Namespace(foo='foovalue', bar='barvalue')
Run Code Online (Sandbox Code Playgroud)
实际上,如果您args从parse_args呼叫中打印,它应该看起来像这样。如文档中所述,a Namespace是一个简单的对象,并且值是属性。因此,构造自己的namespace类很容易。所args需要的只是当用作以下内容时可以返回适当的值:
x = args.foo
b = args.bar
Run Code Online (Sandbox Code Playgroud)
另外,如在文档中所述,vars(args)将此名称空间转换为字典。有些代码喜欢使用字典,但是显然这些youtub函数需要一个Namespace(或等效的)。
get_authenticated_service(args)
initialize_upload(youtube, args)
Run Code Online (Sandbox Code Playgroud)
https://docs.python.org/3/library/argparse.html#beyond-sys-argv
https://docs.python.org/3/library/argparse.html#the-namespace-object
https://developers.google.com/youtube/v3/guides/uploading_a_video?hl=id-ID
有get_authenticated_service和initialize_upload代码
def initialize_upload(youtube, options):
tags = None
if options.keywords:
tags = options.keywords.split(",")
body=dict(
snippet=dict(
title=options.title,
description=options.description,
tags=tags,
categoryId=options.category
),
status=dict(
privacyStatus=options.privacyStatus
)
)
....
Run Code Online (Sandbox Code Playgroud)
args解析器中的from是options,它用作options.category,options.title等。您可以替换具有相同行为和必要属性的任何其他对象。
| 归档时间: |
|
| 查看次数: |
5251 次 |
| 最近记录: |