0

My code searches youtube video link for every line in file using 'for loop', and youtube api.

File line looks like this 'video_id name'. I am passing this video_id value to my function with 'argparser function' that fails. After first iteration code fails when trying to launch this args = argparser.parse_args()

Is there a way to maybe clear argparser? I googled for that, but it seems like there is no solution for my particular constantly changing value of argparser argument. This link Disable/Remove argument in argparse suggests creating parent parser, but I don't understand how it could help my problem.

def ytSearchLaunch(video_id):
  argparser.add_argument("--q", help="Search term", default=video_id)
  argparser.add_argument("--max-results", help="Max results", default=25)
  args = argparser.parse_args()
  youtube_search(args)

This is whole code.

def youtube_search(options):
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)
  search_response = youtube.search().list(
    q=options.q,
    type="video",
    part="id,snippet",
    maxResults=options.max_results
  ).execute()

def ytSearchLaunch(video_id):
  argparser.add_argument("--q", help="Search term", default=video_id)
  argparser.add_argument("--max-results", help="Max results", default=25)
  args = argparser.parse_args()
  youtube_search(args)

def checkDateFunction():
    fname = 'file'
    f = open(fname,'r')
    for l in f:
        video_id = l.split()[0]
        ytSearchLaunch(video_id)
Community
  • 1
  • 1
Pavel Vanchugov
  • 383
  • 1
  • 3
  • 16
  • I am using it in the example code from youtube api page https://developers.google.com/youtube/v3/docs/search/list it's importing from oauth2client `from oauth2client.tools import argparser` link for actual code if it's hard to find on youtube api page https://github.com/youtube/api-samples/blob/master/python/geolocation_search.py – Pavel Vanchugov Mar 06 '17 at 23:18

1 Answers1

2

Your ytSearchLaunch function is taking an argparser object that was created by other code, adding a couple of arguments to it, and then parsing the command line values (which you can also see in sys.argv). It is then passing the resulting args namespace object to youtube_search.

It would help us if you showed how argparser was created, or at least show the help or usage that it produces. It would also be nice to see the args object after parsing.

If I read this right you want to parse the inputs multiple times, each time with a different -q default value. But you can't add the same argument to argparser several times. You can do it once, but on the repeat calls you'll have to modify an existing argument.

Here's a possible fix:

def ytSearchLaunch(video_id, added_actions=[]):
  if len(added_actions)==0: # first time
      a1 = argparser.add_argument("--q", help="Search term", default=video_id)
      a2 = argparser.add_argument("--max-results", help="Max results", default=25)
      added_actions.append(a1)
      added_actions.append(a2)
  else:         # repeats
      # change defaults to the -q argument without adding a new one
      added_actions[0].default = video_id
  args = argparser.parse_args()
  youtube_search(args)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • That's 100% works. Thank you so much! I just started to scratch my head on how to show the help/usage. – Pavel Vanchugov Mar 06 '17 at 23:48
  • 1
    `-h` is the usual `help` commandline. But you can also use `argparser.print_help()` or `argparser.print_usage` to show those for debugging purposes. On occasion I also print `argparser._actions` to see all the Actions have been defined. – hpaulj Mar 06 '17 at 23:58