I think the most readable code to me would be pattern matching, i.e. using regex, and ensuring a docstring. Dealing with splits and indices is always a bother to read (it would then be better if you did tuple unpacking into named variables), and you don't need any real flexibility here anyway so it would be a very simple operation. You don't really need anything else than:
import re def convert_to_watch_url(url: str) -> str: """Converts youtube embed-url to watch-url >>> convert_to_watch_url("https://www.youtube.com/embed/FjHGZj2IjBk?controls=2&fs=0&rel=0&modestbranding=1&showinfo=0&autohide=1&iv_load_policy=3&cc_load_policy=0&autoplay=0") 'https://www.youtube.com/watch?v=FjHGZj2IjBk' """ pattern = r"^.*/embed/([a-zA-Z0-9]*)\?controls.*$" repl = r"https://www.youtube.com/watch?v=\1" return re.sub(pattern, repl, url)
Then I'd probably turn it into a script that accepts arguments:
#!/usr/bin/env python3 """Small script to convert YouTube embed URLs to watch URLs. Can be used from a shell like $ ./converter.py -h $ python3 converter.py "https://www.youtube.com/embed/FjHGZj2IjBk?controls=2&fs=0&rel=0&modestbranding=1&showinfo=0&autohide=1&iv_load_policy=3&cc_load_policy=0&autoplay=0" $ cat urls.txt > xargs python3 converter.py or from Python >>> from converter import convert_to_watch_url """ import argparse import re def convert_to_watch_url(url: str) -> str: """Converts youtube embed-url to watch-url >>> convert_to_watch_url("https://www.youtube.com/embed/FjHGZj2IjBk?controls=2&fs=0&rel=0&modestbranding=1&showinfo=0&autohide=1&iv_load_policy=3&cc_load_policy=0&autoplay=0") 'https://www.youtube.com/watch?v=FjHGZj2IjBk' """ pattern = r"^.*/embed/([a-zA-Z0-9]*)\?controls.*$" repl = r"https://www.youtube.com/watch?v=\1" return re.sub(pattern, repl, url) def main(): parser = argparse.ArgumentParser("Small script to convert URLs") parser.add_argument("URL", dest="urls", nargs="*", help="URL(s) to convert") args = parser.parse_args() for url in args.urls: print(convert_to_watch_url(url)) if __name__ == "__main__": main()
and if you want to keep your tests (but my example is also already testable with doctest
), I'd use pytest and would have the test separate:
import pytest @pytest.parametrize("embed, watch", [ ("https://www.youtube.com/embed/NL5ZuWmrisA?controls=2&fs=0&rel=0&modestbranding=1&showinfo=0&autohide=1&iv_load_policy=3&cc_load_policy=0&autoplay=0", "https://www.youtube.com/watch?v=NL5ZuWmrisA") ("https://www.youtube.com/embed/gnZImHvA0ME?controls=2&fs=0&rel=0&modestbranding=1&showinfo=0&autohide=1&iv_load_policy=3&cc_load_policy=0&autoplay=0", "https://www.youtube.com/watch?v=gnZImHvA0ME") ("https://www.youtube.com/embed/FjHGZj2IjBk?controls=2&fs=0&rel=0&modestbranding=1&showinfo=0&autohide=1&iv_load_policy=3&cc_load_policy=0&autoplay=0", "https://www.youtube.com/watch?v=FjHGZj2IjBk") ]) def test_convert_watch_url_on_correct_urls(): assert convert_to_watch_url(embed) == watch
(which either needs to import the function or be in the same file).