#NEWLINE# @ gen . coroutine #NEWLINE# def _RepairMissingPosts( self , activities , existing_posts ) : #NEWLINE# #INDENT# "Remove references to posts in activities if the posts don't exist." #NEWLINE# @ gen . coroutine #NEWLINE# def _RebuildActivity( activity , act_args ) : #NEWLINE# #INDENT# rebuilt_episodes = [ ] #NEWLINE# for ep_dict in act_args [ 'episodes' ] : #NEWLINE# #INDENT# if ( ep_dict [ 'episode_id' ] in existing_posts ) : #NEWLINE# #INDENT# new_ep_dict = { #NEWLINE# #INDENT# 'episode_id' : ep_dict [ 'episode_id' ] , #NEWLINE# #UNINDENT# } #NEWLINE# for photo_id in list ( ep_dict [ 'photo_ids' ] ) : #NEWLINE# #INDENT# if ( photo_id in existing_posts [ ep_dict [ 'episode_id' ] ] ) : #NEWLINE# #INDENT# new_ep_dict . setdefault ( 'photo_ids' , [ ] ) . append ( photo_id ) #NEWLINE# #UNINDENT# #UNINDENT# rebuilt_episodes . append ( new_ep_dict ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logging . warning ( '  unable to rebuild activity: %s; missing all posts for episode: %s' , activity . activity_id , ep_dict [ 'episode_id' ] ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# act_args [ 'episodes' ] = rebuilt_episodes #NEWLINE# update_activity = Activity . CreateFromKeywords ( viewpoint_id = activity . viewpoint_id , activity_id = activity . activity_id , name = activity . name , json = json . dumps ( act_args ) ) #NEWLINE# ( yield gen . Task ( update_activity . Update , self . _client ) ) #NEWLINE# logging . warning ( '  updated activity: %s' , activity . activity_id ) #NEWLINE# #UNINDENT# def _IsEpisodeMissingAnyPosts( ep_dict ) : #NEWLINE# #INDENT# for photo_id in ep_dict [ 'photo_ids' ] : #NEWLINE# #INDENT# if ( photo_id not in existing_posts [ ep_dict [ 'episode_id' ] ] ) : #NEWLINE# #INDENT# return True #NEWLINE# #UNINDENT# #UNINDENT# return False #NEWLINE# #UNINDENT# for activity in activities : #NEWLINE# #INDENT# if ( activity . name in [ 'share_new' , 'share_existing' ] ) : #NEWLINE# #INDENT# act_args = json . loads ( ep_dict . json ) #NEWLINE# for ep_dict in act_args [ 'episodes' ] : #NEWLINE# #INDENT# if ( ( ep_dict [ 'episode_id' ] not in existing_posts ) or _IsEpisodeMissingAnyPosts ( ep_dict ) ) : #NEWLINE# #INDENT# ( yield _RebuildActivity ( activity , act_args ) ) #NEWLINE# break