commit e321b96a1cb31aad74a56b3489e4006663268bde from: Sergey Bronnikov via: Sergey Bronnikov date: Wed May 15 08:55:28 2024 UTC md-generate.py: support youtube and gif screencasts commit - d1d5ed9055195de446f421d2011d1f8b95ef3b90 commit + e321b96a1cb31aad74a56b3489e4006663268bde blob - b614d516abeb1331560c63024c09735d93c1719a blob + c52cd8ef34da81be6f1a81b61fe0a1921da69619 --- md-generate.py +++ md-generate.py @@ -1,42 +1,59 @@ #!/usr/bin/env python -import yaml +import os import re from urllib.parse import urlparse +import yaml +ASCIINEMA_TEMPLATE = "[![asciicast](https://asciinema.org/a/{}.svg)](https://asciinema.org/a/{})" +YOUTUBE_TEMPLATE = "[![IMAGE ALT TEXT](http://img.youtube.com/vi/{}/0.jpg)](http://www.youtube.com/watch?v={} \"Screencast\")" +YOUTUBE_ID_RE = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?(?P[A-Za-z0-9\-=_]{11})' + +def youtube_video_id(youtube_url): + regex = re.compile(YOUTUBE_ID_RE) + match = regex.match(youtube_url) + return match.group('id') + stream = open("games.yaml", "r") ttygames = yaml.load(stream, Loader=yaml.FullLoader) for entry in ttygames: - name = str(entry['name']) - header = "### %s" % name.strip() + name = str(entry['name']).strip() + header = "### {}".format(name) screencast = "" if 'screencast' in entry and entry['screencast']: u = urlparse(entry['screencast']) if u.hostname == 'asciinema.org': - m = re.match( + asciinema_id_re = re.match( 'https://asciinema.org/a/([0-9A-z]+)', entry['screencast']) - if m: - id = m.group(1) - screencast = ( - "[![asciicast](https://asciinema.org/a/%s.svg)](https://asciinema.org/a/%s)" % (id, id)) + if asciinema_id_re: + asciinema_id = asciinema_id_re.group(1) + screencast = ASCIINEMA_TEMPLATE.format(asciinema_id, asciinema_id) + elif u.hostname == 'www.youtube.com': + youtube_id = youtube_video_id(entry['screencast']) + if youtube_id: + screencast = YOUTUBE_TEMPLATE.format(youtube_id, youtube_id) + elif u.path is not None and screencast is not None: + name, ext = os.path.splitext(u.path) + if ext == ".gif": + screencast = "![Alt Text]({})".format(entry['screencast']) else: - header = header + " [Screencast](%s)" % entry['screencast'] + header = "{} [Screencast]({})".format(header, entry['screencast']) print(header + "\n") if screencast: - print(screencast + "\n") + print("{}\n".format(screencast)) if 'info' in entry: - print("%s" % entry['info']) + print(entry['info']) if 'url' in entry: - print("\nWebsite: %s" % entry['url']) + print("\nWebsite: {}".format(entry['url'])) if 'wikipedia' in entry: - print("\nWikipedia: %s" % entry['wikipedia']) + print("\nWikipedia: {}".format(entry['wikipedia'])) if 'play' in entry: - print("\n**Play**: ```%s```" % entry['play']) + print("\n**Play**: `{}`".format(entry['play'])) print()