<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import re <EOL> import os <EOL> import urllib <EOL> from MarkdownPP . Module import Module <EOL> from MarkdownPP . Transform import Transform <EOL> youtube_url_re = re . compile ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> glowfoto_server_re = re . compile ( "<STR_LIT>" ) <EOL> glowfoto_image_re = re . compile ( "<STR_LIT>" ) <EOL> codere = re . compile ( "<STR_LIT>" ) <EOL> fencedcodere = re . compile ( "<STR_LIT>" ) <EOL> play_button_url = '<STR_LIT>' <EOL> class YoutubeEmbed ( Module ) : <EOL> """<STR_LIT>""" <EOL> def transform ( self , data ) : <EOL> transforms = [ ] <EOL> in_fenced_code_block = False <EOL> linenum = <NUM_LIT:0> <EOL> for line in data : <EOL> if fencedcodere . search ( line ) : <EOL> if in_fenced_code_block : <EOL> in_fenced_code_block = False <EOL> else : <EOL> in_fenced_code_block = True <EOL> if not in_fenced_code_block and not codere . search ( line ) : <EOL> match = youtube_url_re . search ( line ) <EOL> if match : <EOL> url = match . group ( <NUM_LIT:1> ) <EOL> image_url = '<STR_LIT>' % url <EOL> video_url = '<STR_LIT>' % url <EOL> processed_image_dir = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> processed_image_path = os . path . join ( processed_image_dir , <EOL> '<STR_LIT>' % url ) <EOL> if not os . path . isfile ( processed_image_path ) : <EOL> try : <EOL> import Image <EOL> from tempfile import NamedTemporaryFile <EOL> if not os . path . exists ( processed_image_dir ) : <EOL> os . makedirs ( processed_image_dir ) <EOL> screenshot_img = NamedTemporaryFile ( suffix = "<STR_LIT>" ) <EOL> button_img = NamedTemporaryFile ( suffix = "<STR_LIT>" ) <EOL> urllib . urlretrieve ( image_url , screenshot_img . name ) <EOL> urllib . urlretrieve ( play_button_url , <EOL> button_img . name ) <EOL> background = Image . open ( screenshot_img . name ) <EOL> foreground = Image . open ( button_img . name ) <EOL> background . paste ( foreground , ( <NUM_LIT> , <NUM_LIT> ) , foreground ) <EOL> background . save ( processed_image_path ) <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> image_link = ( '<STR_LIT>' % <EOL> ( processed_image_path , video_url ) ) <EOL> transforms . append ( Transform ( linenum , "<STR_LIT>" , image_link ) ) <EOL> linenum += <NUM_LIT:1> <EOL> return transforms </s>
