args.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import argparse
  2. from urllib3.util import parse_url
  3. desc = "Get video from youtube, extract audio and save as mp3 with tags and \
  4. cover. Then it sends mp3 to telegram channel with descrption of tags"
  5. class ValidateUrl(argparse.Action):
  6. def __call__(self, parser, namespace, values, option_string=None):
  7. if not self.__validate(values):
  8. parser.error(f"Invalid URL. Please provide a link to youtube \
  9. video.\nGot: {values}")
  10. setattr(namespace, self.dest, values)
  11. def __validate(self, url) -> bool:
  12. try:
  13. result = parse_url(url)
  14. return all([result.scheme, result.netloc])
  15. except Exception:
  16. return False
  17. class ArgParser:
  18. def __init__(self) -> None:
  19. self.parser = argparse.ArgumentParser(description=desc)
  20. self.parser.add_argument(
  21. "-l", "--link", default=None,
  22. help="Insert youtube video URL",
  23. action=ValidateUrl,
  24. type=str
  25. )
  26. self.parser.add_argument(
  27. "-s", "--shift", default=0,
  28. help="Insert number to shift list of videos for x positions",
  29. type=int
  30. )
  31. self.parser.add_argument(
  32. "-d", "--download", default=False,
  33. help="Only downloading and tagging mp3",
  34. action="store_true"
  35. )
  36. self.args = self.parser.parse_args()