def delete_items(): close_window(confirmation_window, reddit_state, 'confirmation_window_open') count = 1 # refetch item_array since we sent the generator all the way to the # end of the items when we showed which would be deleted if comment_bool: item_array = reddit_state['user'].comments.new(limit=None) else: item_array = reddit_state['user'].submissions.new(limit=None) for item in item_array: if comment_bool: item_string = 'Comment' item_snippet = helpers.format_snippet(item.body, 50) else: item_string = 'Submission' item_snippet = helpers.format_snippet(item.title, 50) time_created = arrow.get(item.created_utc) if time_created > reddit_state['time_to_save']: currently_deleting_text.set( f'{item_string} `{item_snippet}` more recent than cutoff, skipping.' ) elif item.score > reddit_state['max_score']: currently_deleting_text.set( f'{item_string} `{item_snippet}` is higher than max score, skipping.' ) elif item.gilded and reddit_state['gilded_skip']: currently_deleting_text.set( f'{item_string} `{item_snippet}` is gilded, skipping.') elif reddit_state[f'whitelisted_{identifying_text}'][item.id]: currently_deleting_text.set( f'{item_string} `{item_snippet}` is whitelisted, skipping.`' ) else: # Need the try/except here as it will crash on # link submissions otherwise try: item.edit(EDIT_OVERWRITE) except: pass item.delete() currently_deleting_text.set( f'Deleting {item_string} `{item_snippet}`') num_deleted_items_text.set( f'{str(count)}/{str(total_items)} items processed.') deletion_progress_bar['value'] = round((count / total_items) * 100, 1) root.update() count += 1
def delete_twitter_tweets(root, currently_deleting_text, deletion_progress_bar, num_deleted_items_text, twitter_state): """ Deletes user's tweets according to user configurations. :param root: the reference to the actual tkinter GUI window :param currently_deleting_text: Describes the item that is currently being deleted. :param deletion_progress_bar: updates as the items are looped through :param num_deleted_items_text: updates as X out of Y comments are looped through :param twitter_state: dictionary holding twitter settings :return: none """ global twitter_api user_tweets = gather_items(twitter_api.user_timeline) total_tweets = len(user_tweets) num_deleted_items_text.set(f'0/{str(total_tweets)} items processed so far') count = 1 for tweet in user_tweets: tweet_snippet = helpers.format_snippet(tweet.text, 50) time_created = arrow.Arrow.fromdatetime(tweet.created_at) if time_created > twitter_state['time_to_save']: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` is more recent than cutoff, skipping.') elif tweet.favorite_count >= twitter_state['max_favorites']: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` has more favorites than max favorites, skipping.') elif tweet.retweet_count >= twitter_state['max_retweets'] and not tweet.retweeted: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` has more retweets than max retweets, skipping.') elif tweet.id in twitter_state['whitelisted_tweets'] and twitter_state['whitelisted_tweets'][tweet.id]: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` is whitelisted, skipping.') else: if twitter_state['test_run'] == 0: currently_deleting_text.set( f'Deleting tweet: `{tweet_snippet}`') twitter_api.destroy_status(tweet.id) else: currently_deleting_text.set( f'-TEST RUN- Would delete tweet: `{tweet_snippet}`') num_deleted_items_text.set( f'{str(count)}/{str(total_tweets)} items processed.') deletion_progress_bar['value'] = round((count / total_tweets) * 100, 1) root.update() count += 1
def delete_tweets(): close_window(confirmation_window, twitter_state, 'confirmation_window_open') count = 1 for tweet in user_tweets: tweet_snippet = helpers.format_snippet(tweet.text, 50) time_created = arrow.Arrow.fromdatetime(tweet.created_at) if time_created > twitter_state['time_to_save']: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` is more recent than cutoff, skipping.' ) elif tweet.favorite_count >= twitter_state['max_favorites']: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` has more favorites than max favorites, skipping.' ) elif tweet.retweet_count >= twitter_state[ 'max_retweets'] and not tweet.retweeted: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` has more retweets than max retweets, skipping.' ) elif tweet.id in twitter_state[ 'whitelisted_tweets'] and twitter_state[ 'whitelisted_tweets'][tweet.id]: currently_deleting_text.set( f'Tweet: `{tweet_snippet}` is whitelisted, skipping.') else: currently_deleting_text.set( f'Deleting tweet: `{tweet_snippet}`') twitter_api.destroy_status(tweet.id) num_deleted_items_text.set( f'{str(count)}/{str(total_tweets)} items processed.') deletion_progress_bar['value'] = round( (count / total_tweets) * 100, 1) root.update() count += 1
def delete_favorites(): close_window(confirmation_window, twitter_state, 'confirmation_window_open') count = 1 for favorite in user_favorites: favorite_snippet = helpers.format_snippet(favorite.text, 50) currently_deleting_text.set( f'Deleting favorite: `{favorite_snippet}`') time_created = arrow.Arrow.fromdatetime(favorite.created_at) if time_created > twitter_state['time_to_save']: currently_deleting_text.set( f'Favorite: `{favorite_snippet}` is more recent than cutoff, skipping.' ) elif favorite.id in twitter_state[ 'whitelisted_favorites'] and twitter_state[ 'whitelisted_favorites'][favorite.id]: currently_deleting_text.set( f'Favorite: `{favorite_snippet}` is whitelisted, skipping.' ) else: currently_deleting_text.set( f'Deleting favorite: `{favorite_snippet}`') twitter_api.destroy_favorite(favorite.id) num_deleted_items_text.set( f'{str(count)}/{str(total_favorites)} items processed.') deletion_progress_bar['value'] = round( (count / total_favorites) * 100, 1) root.update() count += 1
def set_reddit_whitelist(root, comment_bool, reddit_state): """ Creates a window to let users select which comments or posts to whitelist :param root: the reference to the actual tkinter GUI window :param comment_bool: true for comments, false for posts :param reddit_state: dictionary holding reddit settings :return: none """ # TODO: update this to get whether checkbox is selected or unselected instead of blindly flipping from true to false def flip_whitelist_dict(id, identifying_text): whitelist_dict = reddit_state[f'whitelisted_{identifying_text}'] whitelist_dict[id] = not whitelist_dict[id] reddit_state[f'whitelisted_{identifying_text}'] = whitelist_dict reddit_state.sync if reddit_state['whitelist_window_open'] == 1: return if comment_bool: identifying_text = 'comments' item_array = reddit_state['user'].comments.new(limit=None) else: identifying_text = 'posts' item_array = reddit_state['user'].submissions.new(limit=None) whitelist_window = tk.Toplevel(root) reddit_state['whitelist_window_open'] = 1 reddit_state.sync whitelist_window.protocol( 'WM_DELETE_WINDOW', lambda: close_window( whitelist_window, reddit_state, 'whitelist_window_open')) frame = build_window(root, whitelist_window, f'Pick {identifying_text} to save') counter = 3 for item in item_array: if (item.id not in reddit_state[f'whitelisted_{identifying_text}']): # I wish I could tell you why I need to copy the dictionary of whitelisted items, and then modify it, and then # reassign it back to the persistant shelf. I don't know why this is needed, but it works. whitelist_dict = reddit_state[f'whitelisted_{identifying_text}'] whitelist_dict[item.id] = False reddit_state[f'whitelisted_{identifying_text}'] = whitelist_dict reddit_state.sync whitelist_checkbutton = tk.Checkbutton( frame, command=lambda id=item.id: flip_whitelist_dict( id, identifying_text)) if (reddit_state[f'whitelisted_{identifying_text}'][item.id]): whitelist_checkbutton.select() else: whitelist_checkbutton.deselect() whitelist_checkbutton.grid(row=counter, column=0) tk.Label(frame, text=helpers.format_snippet( item.body if comment_bool else item.title, 100)).grid(row=counter, column=1) ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=counter + 1, columnspan=2, sticky=(tk.E, tk.W), pady=5) counter = counter + 2
def delete_reddit_items(root, comment_bool, currently_deleting_text, deletion_progress_bar, num_deleted_items_text, reddit_state, scheduled_bool): """ Deletes the items according to user configurations. :param root: the reference to the actual tkinter GUI window :param comment_bool: true if deleting comments, false if deleting submissions :param currently_deleting_text: Describes the item that is currently being deleted. :param deletion_progress_bar: updates as the items are looped through :param num_deleted_items_text: updates as X out of Y comments are looped through :param reddit_state: dictionary holding reddit settings :param scheduled_bool: True if a scheduled run, False if triggered manually :return: none """ if reddit_state['confirmation_window_open'] == 1 and not scheduled_bool: return confirmation_window = tk.Toplevel(root) reddit_state['confirmation_window_open'] = 1 reddit_state.sync confirmation_window.protocol( 'WM_DELETE_WINDOW', lambda: close_window( confirmation_window, reddit_state, 'confirmation_window_open')) frame = build_window( root, confirmation_window, f"The following {'comments' if comment_bool else 'posts'} will be deleted" ) if comment_bool: total_items = sum( 1 for _ in reddit_state['user'].comments.new(limit=None)) item_array = reddit_state['user'].comments.new(limit=None) identifying_text = 'comments' else: total_items = sum( 1 for _ in reddit_state['user'].submissions.new(limit=None)) item_array = reddit_state['user'].submissions.new(limit=None) identifying_text = 'posts' num_deleted_items_text.set(f'0/{str(total_items)} items processed so far') button_frame = tk.Frame(frame) button_frame.grid(row=1, column=0, sticky='w') def delete_items(): close_window(confirmation_window, reddit_state, 'confirmation_window_open') count = 1 # refetch item_array since we sent the generator all the way to the # end of the items when we showed which would be deleted if comment_bool: item_array = reddit_state['user'].comments.new(limit=None) else: item_array = reddit_state['user'].submissions.new(limit=None) for item in item_array: if comment_bool: item_string = 'Comment' item_snippet = helpers.format_snippet(item.body, 50) else: item_string = 'Submission' item_snippet = helpers.format_snippet(item.title, 50) time_created = arrow.get(item.created_utc) if time_created > reddit_state['time_to_save']: currently_deleting_text.set( f'{item_string} `{item_snippet}` more recent than cutoff, skipping.' ) elif item.score > reddit_state['max_score']: currently_deleting_text.set( f'{item_string} `{item_snippet}` is higher than max score, skipping.' ) elif item.gilded and reddit_state['gilded_skip']: currently_deleting_text.set( f'{item_string} `{item_snippet}` is gilded, skipping.') elif reddit_state[f'whitelisted_{identifying_text}'][item.id]: currently_deleting_text.set( f'{item_string} `{item_snippet}` is whitelisted, skipping.`' ) else: # Need the try/except here as it will crash on # link submissions otherwise try: item.edit(EDIT_OVERWRITE) except: pass item.delete() currently_deleting_text.set( f'Deleting {item_string} `{item_snippet}`') num_deleted_items_text.set( f'{str(count)}/{str(total_items)} items processed.') deletion_progress_bar['value'] = round((count / total_items) * 100, 1) root.update() count += 1 proceed_button = tk.Button(button_frame, text='Proceed', command=lambda: delete_items()) cancel_button = tk.Button( button_frame, text='Cancel', command=lambda: close_window(confirmation_window, reddit_state, 'confirmation_window_open')) proceed_button.grid(row=1, column=0, sticky='nsew') cancel_button.grid(row=1, column=1, sticky='nsew') counter = 3 for item in item_array: time_created = arrow.get(item.created_utc) if time_created > reddit_state['time_to_save']: pass elif item.score > reddit_state['max_score']: pass elif item.gilded and reddit_state['gilded_skip']: pass elif reddit_state[f'whitelisted_{identifying_text}'][item.id]: pass else: tk.Label(frame, text=helpers.format_snippet( item.body if comment_bool else item.title, 100)).grid(row=counter, column=0) ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=counter + 1, columnspan=2, sticky='ew', pady=5) counter = counter + 2
def set_twitter_whitelist(root, tweet_bool, twitter_state): """ Creates a window to let users select which tweets or favorites to whitelist :param root: the reference to the actual tkinter GUI window :param tweet_bool: true for tweets, false for favorites :param twitter_state: dictionary holding twitter settings :return: none """ global twitter_api # TODO: update this to get whether checkbox is selected or unselected instead of blindly flipping from true to false def flip_whitelist_dict(id, identifying_text): whitelist_dict = twitter_state[f'whitelisted_{identifying_text}'] whitelist_dict[id] = not whitelist_dict[id] twitter_state[f'whitelisted_{identifying_text}'] = whitelist_dict twitter_state.sync def onFrameConfigure(canvas): '''Reset the scroll region to encompass the inner frame''' canvas.configure(scrollregion=canvas.bbox("all")) def closeWindow(whitelist_window): twitter_state['whitelist_window_open'] = 0 whitelist_window.destroy() if tweet_bool: identifying_text = 'tweets' item_array = gather_items(twitter_api.user_timeline) else: identifying_text = 'favorites' item_array = gather_items(twitter_api.favorites) whitelist_window = tk.Toplevel(root) twitter_state['whitelist_window_open'] = 1 whitelist_window.protocol( 'WM_DELETE_WINDOW', lambda: closeWindow(whitelist_window)) canvas = tk.Canvas(whitelist_window, width=750, height=1000) frame = tk.Frame(canvas) scrollbar = tk.Scrollbar(whitelist_window, command=canvas.yview) canvas.configure(yscrollcommand=scrollbar.set) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) canvas.create_window((4, 4), window=frame, anchor="nw") whitelist_title_label = tk.Label( frame, text=f'Pick {identifying_text} to save', font=('arial', 30)) frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas)) whitelist_title_label.grid( row=0, column=0, columnspan=2, sticky=(tk.N, tk.E, tk.W, tk.S)) ttk.Separator(frame, orient=tk.HORIZONTAL).grid( row=1, columnspan=2, sticky=(tk.E, tk.W), pady=5) counter = 2 for item in item_array: if (item.id not in twitter_state[f'whitelisted_{identifying_text}']): # I wish I could tell you why I need to copy the dictionary of whitelisted items, and then modify it, and then # reassign it back to the persistant shelf. I don't know why this is needed, but it works. whitelist_dict = twitter_state[f'whitelisted_{identifying_text}'] whitelist_dict[item.id] = False twitter_state[f'whitelisted_{identifying_text}'] = whitelist_dict twitter_state.sync whitelist_checkbutton = tk.Checkbutton(frame, command=lambda id=item.id: flip_whitelist_dict(id, identifying_text)) if (twitter_state[f'whitelisted_{identifying_text}'][item.id]): whitelist_checkbutton.select() else: whitelist_checkbutton.deselect() whitelist_checkbutton.grid(row=counter, column=0) tk.Label(frame, text=helpers.format_snippet(item.text, 100)).grid(row=counter, column=1) ttk.Separator(frame, orient=tk.HORIZONTAL).grid( row=counter+1, columnspan=2, sticky=(tk.E, tk.W), pady=5) counter = counter + 2
def delete_reddit_items(root, comment_bool, currently_deleting_text, deletion_progress_bar, num_deleted_items_text, reddit_state): """ Deletes the items according to user configurations. :param root: the reference to the actual tkinter GUI window :param comment_bool: true if deleting comments, false if deleting submissions :param currently_deleting_text: Describes the item that is currently being deleted. :param deletion_progress_bar: updates as the items are looped through :param num_deleted_items_text: updates as X out of Y comments are looped through :param reddit_state: dictionary holding reddit settings :return: none """ if comment_bool: total_items = sum( 1 for _ in reddit_state['user'].comments.new(limit=None)) item_array = reddit_state['user'].comments.new(limit=None) else: total_items = sum( 1 for _ in reddit_state['user'].submissions.new(limit=None)) item_array = reddit_state['user'].submissions.new(limit=None) num_deleted_items_text.set(f'0/{str(total_items)} items processed so far') count = 1 for item in item_array: if comment_bool: identifying_text = 'comments' item_string = 'Comment' item_snippet = helpers.format_snippet(item.body, 50) else: identifying_text = 'posts' item_string = 'Submission' item_snippet = helpers.format_snippet(item.title, 50) time_created = arrow.get(item.created_utc) if time_created > reddit_state['time_to_save']: currently_deleting_text.set( f'{item_string} `{item_snippet}` more recent than cutoff, skipping.') elif item.score > reddit_state['max_score']: currently_deleting_text.set( f'{item_string} `{item_snippet}` is higher than max score, skipping.') elif item.gilded and reddit_state['gilded_skip']: currently_deleting_text.set( f'{item_string} `{item_snippet}` is gilded, skipping.') elif reddit_state[f'whitelisted_{identifying_text}'][item.id]: currently_deleting_text.set( f'{item_string} `{item_snippet}` is whitelisted, skipping.`' ) else: if reddit_state['test_run'] == 0: # Need the try/except here as it will crash on # link submissions otherwise try: item.edit(EDIT_OVERWRITE) except: pass item.delete() currently_deleting_text.set( f'Deleting {item_string} `{item_snippet}`') else: currently_deleting_text.set( f'TEST RUN: Would delete {item_string} `{item_snippet}`') num_deleted_items_text.set( f'{str(count)}/{str(total_items)} items processed.') deletion_progress_bar['value'] = round( (count / total_items) * 100, 1) root.update() count += 1
def delete_twitter_favorites(root, currently_deleting_text, deletion_progress_bar, num_deleted_items_text, twitter_state, scheduled_bool): """ Deletes users's favorites according to user configurations. :param root: the reference to the actual tkinter GUI window :param currently_deleting_text: Describes the item that is currently being deleted. :param deletion_progress_bar: updates as the items are looped through :param num_deleted_items_text: updates as X out of Y comments are looped through :param twitter_state: dictionary holding twitter settings :param scheduled_bool: True if a scheduled run, False if triggered manually :return: none """ if twitter_state['confirmation_window_open'] == 1 and not scheduled_bool: return global twitter_api confirmation_window = tk.Toplevel(root) twitter_state['confirmation_window_open'] = 1 twitter_state.sync confirmation_window.protocol( 'WM_DELETE_WINDOW', lambda: close_window( confirmation_window, twitter_state, 'confirmation_window_open')) frame = build_window(root, confirmation_window, f"The following favorites will be removed") user_favorites = gather_items(twitter_api.favorites) total_favorites = len(user_favorites) num_deleted_items_text.set( f'0/{str(total_favorites)} items processed so far') button_frame = tk.Frame(frame) button_frame.grid(row=1, column=0, sticky='w') def delete_favorites(): close_window(confirmation_window, twitter_state, 'confirmation_window_open') count = 1 for favorite in user_favorites: favorite_snippet = helpers.format_snippet(favorite.text, 50) currently_deleting_text.set( f'Deleting favorite: `{favorite_snippet}`') time_created = arrow.Arrow.fromdatetime(favorite.created_at) if time_created > twitter_state['time_to_save']: currently_deleting_text.set( f'Favorite: `{favorite_snippet}` is more recent than cutoff, skipping.' ) elif favorite.id in twitter_state[ 'whitelisted_favorites'] and twitter_state[ 'whitelisted_favorites'][favorite.id]: currently_deleting_text.set( f'Favorite: `{favorite_snippet}` is whitelisted, skipping.' ) else: currently_deleting_text.set( f'Deleting favorite: `{favorite_snippet}`') twitter_api.destroy_favorite(favorite.id) num_deleted_items_text.set( f'{str(count)}/{str(total_favorites)} items processed.') deletion_progress_bar['value'] = round( (count / total_favorites) * 100, 1) root.update() count += 1 proceed_button = tk.Button(button_frame, text='Proceed', command=lambda: delete_favorites()) cancel_button = tk.Button( button_frame, text='Cancel', command=lambda: close_window(confirmation_window, twitter_state, 'confirmation_window_open')) proceed_button.grid(row=1, column=0, sticky='nsew') cancel_button.grid(row=1, column=1, sticky='nsew') counter = 3 for favorite in user_favorites: time_created = arrow.Arrow.fromdatetime(favorite.created_at) if time_created > twitter_state['time_to_save']: pass elif favorite.id in twitter_state[ 'whitelisted_favorites'] and twitter_state[ 'whitelisted_favorites'][favorite.id]: pass else: tk.Label(frame, text=helpers.format_snippet(favorite.text, 100)).grid(row=counter, column=0) ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=counter + 1, columnspan=2, sticky='ew', pady=5) counter = counter + 2