def get_new_profile_data(profile_data: dict, attributes: dict, new_profile_data: dict, genders: list) -> None: """ The user is asked for the new values for the personal data of the profile, and the values are stored in the new_profile_data dictionary Arguments: profile_data (dict) : All personal account data attributes (dict) : Stores all available fields that can be changed new_profile_data (dict) : Empty dictionary that will store the new values genders (list) : List of available genres """ for key, attribute in attributes.items(): print_account_warnings(attribute) change_attribute = input_user_chat( f"Do you want to change {key}? yes/no: ") if user_answer_is_yes(change_attribute): new_value = input_user_chat(f"Enter the new value for {key}: ") secure = input_user_chat(f"\nAre you sure to change {key} " f"to '{new_value}'? yes/no: ") if user_answer_is_yes(secure): if attribute == 'is_private': new_value = get_new_account_privacy(new_value) elif attribute == 'gender': new_value = get_new_user_gender(new_value, genders) new_profile_data[attribute] = new_value else: print_write_chatbot(f"No changes have been made to the {key}") else: new_profile_data[attribute] = profile_data[attribute]
def connection_instagram_api() -> object: """ The user is asked if he has an instagram account to connect, if he does not have, the Crux account will be used Returns: object (instagram.Client()) - instagram.Client object """ response = input_user_chat("Would you like to connect to Instagram? (yes/no): ") credentials = {} if user_answer_is_yes(response.lower()): username = input_user_chat("Please enter your username: "******"Please enter your password: "******"By not using the instagram tool with your personal page," " we will provide the service with our Instagram account crux.bot", color = 'blue', attrs_color = ['bold']) instagram_api = instagram.connection_instagram(user_credentials = credentials) return instagram_api
def run_bot(bot: ChatBot) -> None: """ The user interacts with the bot. The bot can answer the user or execute the functions requested by the user Arguments: bot (ChatBot) """ print_welcome_message() running = True is_taken_name = False read = False while not read: print_write_chatbot("PLEASE READ ALL THE MESSAGE", color = 'blue', attrs_color = ['bold', 'underline', 'blink']) is_read = input_user_chat("Did you read all the message? (yes/no) ", first_time = True) read = user_answer_is_yes(is_read) while running: try: if not is_taken_name: ask_name() is_taken_name = True facebook_api = connection_facebook_api() instagram_api = connection_instagram_api() user_input = input_user_chat("\nYou: ") bot_response = str(bot.get_response(user_input)) if "_" in bot_response: exec(bot_response) else: print_write_chatbot(bot_response) keep_running = input_user_chat("Do you want to continue chatting with me?? (yes/no): ") if not user_answer_is_yes(keep_running): running = False except (KeyboardInterrupt, EOFError, SystemExit): running = False else: print_write_chatbot("It's the end", color = 'blue', attrs_color = ['bold']) animation("May the Force be with you\n") print_write_chatbot("May the Force be with you", print_text = False)
def want_unlike_target(target_type: str) -> bool: """ The user is asked if he wants to leave a dislike in a comment or post Arguments: target_type (str) : String that can be post or comment Returns: bool - If the user wants to leave a dislike in the target """ do_unlike = input_user_chat( f"The {target_type} is already" f" liked by you, you want to unliked? (yes/no): ".lower()) return user_answer_is_yes(do_unlike)
def connection_facebook_api() -> object: """ The user is asked if he has an facebook page to connect, if he does not have, the Crux account will be used Returns: object (facebook.GraphAPI()) - facebook.GraphAPI object """ response = input_user_chat("Would you like to connect to Facebook? (yes/no): ") credentials = {} if user_answer_is_yes(response.lower()): page_token = input_user_chat("Please enter your page access token: ") credentials = {'token': page_token} else: print_write_chatbot("By not using the facebook tool with your personal page, " "we will provide the service with our" "Facebook page Crux.cruz", color = 'blue', attrs_color = ['bold']) facebook_api = facebook.connection_api(user_credentials = credentials) return facebook_api
def delete_comment(api: Client, feed: dict) -> None: """ Delete a comment chosen by the current user Arguments: api (Client) : Object instagram Client feed (dict) : the user feed """ print_write_chatbot("You cannot edit a comment, only delete it\n", color='blue', attrs_color=['bold', 'underline']) secure_delete = input_user_chat( "Do you want to delete a comment from a post? (yes/no):") if user_answer_is_yes(secure_delete): text = "Which comment would you delete?" comment_data = prepare_comment(api=api, feed=feed['items'], text=text) delete(api, target_id=comment_data['comment_id'], target_type='comment', parent_id=comment_data['post_id'])
def edit_post(api: Client, feed: dict, post_id: str, number_post: int) -> None: """ Edit a post chosen by the current user Arguments: api (Client) : Object instagram Client feed (dict) : the user feed post_id (str) : Id of the post number_post (int) : Position of the post in the items feed """ print_write_chatbot("You can only edit the caption!\n", color='blue', attrs_color=['bold', 'blink']) old_caption = feed['items'][number_post]['caption']['text'] new_caption = input_user_chat("Enter the new caption: ") secure = input_user_chat( f"\nAre you sure to change '{old_caption}' to '{new_caption}'? (yes/no): " ) if user_answer_is_yes(secure): result = api.edit_media(media_id=post_id, caption=new_caption) if result['status'] == 'ok': print_write_chatbot("It has been edited successfully!\n", color='green', attrs_color=['bold']) else: print_write_chatbot( message= "An error occurred while changing it, please try again later\n", color='red', attrs_color=['bold']) else: print_write_chatbot("The post has not been modified\n", color='blue', attrs_color=['bold'])
def edit_actions(api: Client, edit_type: str, target_type: str = 'post') -> None: """ Depending on the type of target and type of like, a post or a comment will be edit or delete Arguments: api (Client) : Object instagram Client edit_type (str) : Can be delete or edit target_type (str) : Can be post or comment """ feed = api.self_feed() is_feed_empty = feed['items'][0] if is_feed_empty: show_user_feed(api, feed['items'], True) if target_type == 'post': text = f"Which post would you {edit_type}?" post_id, number_post = get_post_id(feed, text=text) if edit_type == 'edit': edit_post(api, feed=feed, post_id=post_id, number_post=number_post) else: secure = input_user_chat( f"Are you sure to {edit_type} the post? (yes/no): \n") if user_answer_is_yes(secure): delete(api, target_id=str(post_id), target_type='post') else: if edit_type == 'delete': delete_comment(api, feed) else: print_write_chatbot("Your feed is empty", color='red', attrs_color=['bold'])
def post_comment(api: Client) -> None: """ Post a comment on a post from a user chosen by the current user Arguments: api (Client) : object instagram Client Returns: None """ username = get_username( api, "Who do you want to find to post a comment on his post?") can_get_feed, own_feed = is_following_user(api=api, username=username, client_username=api.username) if can_get_feed: feed, is_feed_empty = get_user_feed(api, username, own_feed=own_feed) if not is_feed_empty: comment_block = True text = "Which post would you like to comment on?" post_id, number_post = get_post_id(feed, text) want_put_comment = True while comment_block and want_put_comment: if 'comments_disabled' not in feed['items'][number_post]: comment_block = False else: print_write_chatbot( message= "The post has the comments blocked, please choose another post\n", color='red', attrs_color=['bold']) another_try = input_user_chat( "Do you want to try another comment? (yes/no) ") if user_answer_is_yes(another_try): post_id, number_post = get_post_id( feed, text), get_post_number(text=text, max_cant_posts=len( feed['items'])) else: want_put_comment = False if want_put_comment: message = input_user_chat("Message: ") result = api.post_comment(media_id=post_id, comment_text=message) try: if result['status'] == 'ok': print_write_chatbot( message="The comment has been posted correctly!\n", color='green', attrs_color=['bold']) except Exception as error: write_log(STATUS_FILE, str(error), 'Exception') print_write_chatbot(f"There was an error: {error}", color='red', attrs_color=['bold']) else: print_write_chatbot( message="It's okay if you couldn't leave a comment," " there are many posts in the sea, go get them tiger!\n", color='blue', attrs_color=['bold', 'underline']) else: print_write_chatbot(message="You cant get the feed", color='red', attrs_color=['bold'])