<s> import time , urllib2 , textwrap , re , HTMLParser , tempfile <EOL> from PIL import Image , ImageDraw , ImageFont <EOL> from argparse import ArgumentParser <EOL> from snapchat_bots import SnapchatBot , Snap <EOL> from snapchat_bots . utils import resize_image <EOL> h = HTMLParser . HTMLParser ( ) <EOL> def get_article_info ( url ) : <EOL> content = urllib2 . urlopen ( url ) . read ( ) <EOL> description = re . search ( "<STR_LIT>" , content ) . group ( <NUM_LIT:1> ) <EOL> img_src = re . search ( "<STR_LIT>" , content ) . group ( <NUM_LIT:1> ) <EOL> img = download_image ( img_src ) <EOL> return h . unescape ( description ) , img <EOL> def download_image ( src ) : <EOL> tmp = tempfile . NamedTemporaryFile ( suffix = "<STR_LIT>" ) <EOL> tmp . write ( urllib2 . urlopen ( src ) . read ( ) ) <EOL> img = Image . open ( tmp . name ) <EOL> return img <EOL> def get_last_breaking_news_url ( ) : <EOL> content = urllib2 . urlopen ( "<STR_LIT>" ) . read ( ) <EOL> try : <EOL> return re . search ( '<STR_LIT>' , content ) . group ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> def create_breaking_news_image_from_info ( info ) : <EOL> title , header_img = info <EOL> para = textwrap . wrap ( title , width = <NUM_LIT:30> ) <EOL> im = Image . new ( '<STR_LIT>' , ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> draw = ImageDraw . Draw ( im ) <EOL> font = ImageFont . truetype ( '<STR_LIT>' , <NUM_LIT> ) <EOL> current_h , pad = <NUM_LIT:100> , <NUM_LIT:10> <EOL> for line in para : <EOL> w , h = draw . textsize ( line , font = font ) <EOL> draw . text ( ( ( <NUM_LIT> - w ) / <NUM_LIT:2> , current_h ) , line , font = font ) <EOL> current_h += h + pad <EOL> current_h += <NUM_LIT> <EOL> im . paste ( header_img , ( <NUM_LIT:0> , current_h ) ) <EOL> return im <EOL> class ReporterBot ( SnapchatBot ) : <EOL> def initialize ( self ) : <EOL> self . last_tweet_url = None <EOL> def on_friend_add ( self , friend ) : <EOL> self . add_friend ( friend ) <EOL> def on_friend_delete ( self , friend ) : <EOL> self . delete_friend ( friend ) <EOL> def run ( self ) : <EOL> while True : <EOL> self . log ( "<STR_LIT>" ) <EOL> last_tweet_url = get_last_breaking_news_url ( ) <EOL> if self . last_tweet_url is None : <EOL> self . last_tweet_url = last_tweet_url <EOL> elif self . last_tweet_url != last_tweet_url : <EOL> info = get_article_info ( last_tweet_url ) <EOL> self . log ( "<STR_LIT>" % info [ <NUM_LIT:0> ] ) <EOL> img = create_breaking_news_image_from_info ( info ) <EOL> snap = Snap . from_image ( img , duration = <NUM_LIT:10> ) <EOL> self . send_snap ( self . get_friends ( ) , snap ) <EOL> self . last_tweet_url = last_tweet_url <EOL> else : <EOL> self . log ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = ArgumentParser ( "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , required = True , type = str , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , required = True , type = str , help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> bot = ReporterBot ( args . username , args . password ) <EOL> bot . run ( ) </s>
