grgr.me

latest commits to https://grgr.me/
Log | Files | Refs

youtube-embed.py (1073B)


      1 #!/usr/bin/env python3
      2 
      3 from bs4 import BeautifulSoup
      4 import sys
      5 
      6 raw = sys.stdin.read()
      7 raw = BeautifulSoup(raw, "html.parser")
      8 
      9 lists = raw.find_all('li')
     10 for li in lists:
     11     anchor = li.find('a')
     12     if not anchor:
     13         continue
     14     elif anchor.parent.name == "p" or len(anchor.find_parents("ul")) > 1:
     15         continue
     16     elif "youtube.com" in anchor['href']:
     17         leftSide = True if len(anchor.get_text()) < 32 else False
     18         vid = anchor['href'].split('=')[1]
     19         embed_markup = (' <span class="nowrap">[<button class="embed"'
     20                         f'onclick="toggleYT(\'{vid}\')">'
     21                         f'<img class="embed{" left" if leftSide else ""}" '
     22                         'alt="" src='
     23                         f'"https://img.youtube.com/vi/{vid}/mqdefault.jpg">'
     24                         'toggle embed</button>]</span>'
     25                         f'<div class="embed" id="{vid}"></div>'
     26                         )
     27         embed_markup = BeautifulSoup(embed_markup, 'html.parser')
     28         li.insert(1, embed_markup)
     29 
     30 sys.stdout.write(str(raw))