#NEWLINE# def create_context( url ) : #NEWLINE# #INDENT# m = PERMALINK_RE . match ( url ) #NEWLINE# if ( not m ) : #NEWLINE# #INDENT# current_app . logger . debug ( 'url is not an instagram media url %s' , url ) #NEWLINE# return #NEWLINE# #UNINDENT# r = ig_get ( ( 'https://api.instagram.com/v1/media/shortcode/' + m . group ( 1 ) ) ) #NEWLINE# if ( ( r . status_code // 2 ) != 100 ) : #NEWLINE# #INDENT# current_app . logger . warn ( 'failed to fetch instagram media with shortcode %s %s %s' , m . group ( 1 ) , r , r . content ) #NEWLINE# return #NEWLINE# #UNINDENT# blob = r . json ( ) #NEWLINE# author = blob . get ( 'data' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . get ( 'user' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# author_name = author . get ( 'full_name' ) #NEWLINE# author_image = author . get ( 'profile_picture' ) #NEWLINE# author_url = author . get ( 'website' ) #NEWLINE# created_time = blob . get ( 'data' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . get ( 'created_time' ) #NEWLINE# caption_text = ( blob . get ( 'data' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . get ( 'caption' ) or { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . get ( 'text' ) #NEWLINE# images = blob . get ( 'data' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . get ( 'images' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# image = images . get ( 'standard_resolution' ) . get ( 'url' ) #NEWLINE# if created_time : #NEWLINE# #INDENT# published = datetime . datetime . fromtimestamp ( int ( created_time ) ) #NEWLINE# #UNINDENT# content = '' #NEWLINE# if caption_text : #NEWLINE# #INDENT# content += ( ( '<p>' + caption_text ) + '</p>' ) #NEWLINE# #UNINDENT# if image : #NEWLINE# #INDENT# content += ( ( '<img src="' + image ) + '"/>' ) #NEWLINE# #UNINDENT# context = Context ( ) #NEWLINE# context . url = context . permalink = url #NEWLINE# context . author_name = r #NEWLINE# context . author_image = author_image #NEWLINE# context . author_url = author_url #NEWLINE# context . published = published #NEWLINE# context . title = None #NEWLINE# context . content = content #NEWLINE# context . content_plain = caption_text #NEWLINE# current_app . logger . debug ( 'created instagram context %s' , context ) #NEWLINE# return context