get_yt_id.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import re
  4. import sys
  5. from pathlib import Path
  6. from urllib import request
  7. RE_HREF = re.compile(b'.*href="([^"]+)".*')
  8. RE_LINK = re.compile(b'(<link[^>]+rel="canonical"[^>]+)')
  9. def main(url: str) -> int:
  10. print('downloading...', file=sys.stderr)
  11. with request.urlopen(url) as response:
  12. print('parsing...', file=sys.stderr)
  13. try:
  14. if link_match := next(RE_LINK.finditer(response.read())):
  15. if link := link_match.group(1):
  16. if href_match := RE_HREF.match(link):
  17. print(Path(href_match.group(1).decode()).name)
  18. return 0
  19. except StopIteration:
  20. pass
  21. print('failed to extract id', file=sys.stderr)
  22. return 1
  23. if __name__ == '__main__':
  24. if len(sys.argv) != 2 or sys.argv[1] in ('-h', '--help'):
  25. print(
  26. 'usage: python3 %s url\n' % sys.argv[0],
  27. 'get channel id from channel url',
  28. 'for example:',
  29. ' * https://www.youtube.com/c/ChannelName',
  30. ' * https://www.youtube.com/user/UserName',
  31. sep='\n', file=sys.stderr
  32. )
  33. else:
  34. sys.exit(main(sys.argv[1]))