def get_email_data(): while True: try: with open("email-data.txt", "r") as f: data = f.readlines() from_email = data[0].strip() password = data[1].strip() to_email = data[2].strip() return from_email, password, to_email except FileNotFoundError: sh.print_then_sleep( f"Could not find 'email-data.txt' containing your username and password." ) confirmation = sh.get_confirmation( "Would you like to create this file?") if not confirmation: input("Press 'enter' to close the program.") sys.exit() create_email_data_file() except IndexError: sh.print_then_sleep( f"The file 'email-data.txt' did not contain all of the required lines." ) confirmation = sh.get_confirmation( "Would you like to try creating this file again?") if not confirmation: input("Press 'enter' to close the program.") sys.exit() create_email_data_file()
def update_file_if_already_tracked(key, new_price, file): with open(file, "r") as f: file_lines = f.readlines() for line_num, line_string in enumerate(file_lines): current_line = line_string.strip() if current_line == key: try: val = file_lines[line_num + 1].strip() if val in [ str(round(new_price, 2)), str(round(new_price, 2)) + "0" ]: sh.print_then_sleep( f"You are already tracking {key} at a maximum price value of ${val}.\n" ) elif sh.get_confirmation( f"You are currently tracking {key} for a maximum price value of ${val}.\n" f"Would you like to update this maximum price value to ${new_price:.2f}?\n" ): file_lines[line_num + 1] = f"{new_price:.2f}\n" with open(file, "w") as f: f.writelines(file_lines) return True except IndexError: return False return False
def update_file_if_already_tracked(name, link, new_price, file): with open(file, "r") as f: file_lines = f.readlines() for line_num, line_string in enumerate(file_lines): current_line = line_string.strip() if sh.is_link(line_string.strip()) and get_offer_listing_url( current_line) == get_offer_listing_url(link): try: val = file_lines[line_num + 1].strip() previous_conditions = convert_link_to_conditions(current_line) new_conditions = convert_link_to_conditions(link) if val in [str(round(new_price, 2)), str(round(new_price, 2)) + "0"] and \ previous_conditions == new_conditions: sh.print_then_sleep( f"You are already tracking {name} at a maximum price value of ${val}.\n" ) elif sh.get_confirmation( f"You are currently tracking {name} for a maximum price value of ${val}, " f"at these conditions: {', '.join(previous_conditions) or 'all'}.\n" f"Would you like to update this maximum price value to ${new_price:.2f} " f"at these conditions: {', '.join(new_conditions) or 'all'}?\n" ): file_lines[line_num] = f"{link}\n" file_lines[line_num + 1] = f"{new_price:.2f}\n" with open(file, "w") as f: f.writelines(file_lines) return True except IndexError: return False return False
def get_notification_price(key): while True: try: notification_price = float( input( f'What is the maximum price you want to track for "{key}"?\n' )) if (sh.get_confirmation( f'Shall we send you notifications when items found under the search key "{key}" ' f'are available for less than ${notification_price:.2f}?\n' f"(Input 'yes' or 'no')\n")): return notification_price else: return "" except ValueError: sh.print_then_sleep( "Please type your answer in the form of a number. Do not include any currency symbols " "or abbreviations.\n")
def get_total_price_list(base_cost_list, shipping_cost_list): total_price_list = [] for base, shipping in zip(base_cost_list, shipping_cost_list): if isinstance(shipping, float): total_price_list.append(base + shipping) else: total_price_list.append(base) return total_price_list if __name__ == "__main__": try: while True: main(c.main_headers) continuing = sh.get_confirmation( "Would you like to track another search key?") if not continuing: break except requests.exceptions.HTTPError as e: sh.print_then_sleep( f"There was an error in getting the response from the server.\n{e}" ) input("Press 'enter' to close the program.") except requests.exceptions.RequestException as e: sh.print_then_sleep( f"There was a general error in processing the request.\n{e}") input("Press 'enter' to close the program.") except Exception as e: print(e) input()
def convert_link_to_conditions(link): conditions = set() split_link = link.split("/") if len(split_link) < 6: return {"x"} condition_information = split_link[-1] condition_information = re.split('[?&]', condition_information) for element in condition_information[1:]: if element[2:-5] in c.full_quality_dict: conditions.add(c.full_quality_dict[element[2:-5]]) return conditions if __name__ == "__main__": try: while True: main(c.main_headers) continuing = sh.get_confirmation( "Would you like to search for another item to track?") if not continuing: break except requests.exceptions.HTTPError as e: sh.print_then_sleep( f"There was an error in getting the response from the server.\n{e}" ) input("Press 'enter' to close the program.") except requests.exceptions.RequestException as e: sh.print_then_sleep( f"There was a general error in processing the request.\n{e}") input("Press 'enter' to close the program.")