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 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 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 get_web_response(url, headers, xpath_string): web_response = requests.get(url, headers=headers) web_response.raise_for_status() parser = html.fromstring(web_response.text) results = parser.xpath(xpath_string) if not results: sh.print_then_sleep("Sorry, we didn't find any results for that.\n") return results
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 send_email(mail): try: # with smtplib.SMTP("smtp.gmail.com", 587) as server: # with smtplib.SMTP("smtp-mail.outlook.com", 587) as server: with smtplib.SMTP("smtp.office365.com", 587) as server: server.ehlo() server.starttls() email, password = get_email_data()[:2] server.login(email, password) server.send_message(mail) except smtplib.SMTPAuthenticationError as e: sh.print_then_sleep( f"There was an error in authentication. Double check your username and password.\n{e}" ) input("Press 'enter' to close the program.") except smtplib.SMTPException as e: sh.print_then_sleep(f"An SMTP error occurred.\n{e}") input("Press 'enter' to close the program.")
def get_user_search_selection(names): while True: search_choice = input( "Type in the number of the item you want to track, or else type 'q' to search for " "another item.\n") try: search_choice = int(search_choice) if not 0 < search_choice <= len(names): sh.print_then_sleep( f"That is not a valid input. Please ensure the chosen number is between 1 and " f"{len(names)}.\n") else: break except ValueError: if search_choice.lower() == "q": break sh.print_then_sleep( "That is not a valid input. Please type a number from the available choices, or else " "'q' to quit.\n") return search_choice
def get_user_ignore_selection(list_of_auctions): while True: search_choice = input( "Type in the number of the item you want to ignore while tracking, or type 't' to " "select a price to track this search key at, or else type 'q' to search for another " "item.\n") try: search_choice = int(search_choice) if not 0 < search_choice <= len(list_of_auctions): sh.print_then_sleep( f"That is not a valid input. Please ensure the chosen number is between 1 and " f"{len(list_of_auctions)}.\n") else: break except ValueError: if search_choice.lower() in ["q", "t"]: break sh.print_then_sleep( "That is not a valid input. Please type a number from the available choices, type 't' " "to track this search key, or else 'q' to quit.\n") return search_choice
def get_user_input_for_quality(): while True: sh.print_then_sleep( "What conditions of this item are you interested in tracking?\n" "From highest to lowest quality, possible conditions are:\n" "\tNew (n)\n" "\tLike New (l)\n" "\tVery Good (v)\n" "\tGood (g)\n" "\tAcceptable (a)\n" "\tAll (x)" "") response = input( "Please type the key letters for all conditions you are interested in tracking.\n" "For example, if you want to track only new, like new, and very good conditions, type " "'nlv'.\n").lower() if [i for i in set(response) if i in c.abbreviated_quality_dict]: return response sh.print_then_sleep( "There was an error in your input. You did not include any condition key letters.\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()