|
| 1 | +from datetime import datetime |
| 2 | +from ..ticketscraping.models.pick import Pick |
| 3 | + |
| 4 | +def price_formatter(price): |
| 5 | + price_str = '' |
| 6 | + if type(price) is float or int: |
| 7 | + price_str = "$" + "{:.2f}".format(price) |
| 8 | + elif type(price) is str: |
| 9 | + price_str = price |
| 10 | + return price_str |
| 11 | + |
| 12 | +def decimal_to_percent(num: float): |
| 13 | + return "{:.2f}".format(num*100) + "%" |
| 14 | + |
| 15 | +def format_date(date: datetime): |
| 16 | + return date.isoformat() |
| 17 | + |
| 18 | +def default_formatter(s: str): |
| 19 | + return s |
| 20 | + |
| 21 | +def format_seat_columns(cols): |
| 22 | + if type(cols) is str: |
| 23 | + return cols |
| 24 | + elif type(cols) is list: |
| 25 | + return "(" + ",".join(cols) + ")" |
| 26 | + return '-' |
| 27 | + |
| 28 | +def apply_format(s, formatter)->str: |
| 29 | + return formatter(s) |
| 30 | + |
| 31 | +def apply(values: list, formatters: list, delimiter="\t"): |
| 32 | + if len(values) != len(formatters): |
| 33 | + raise Exception('values and formatters must have the same length') |
| 34 | + s = [] |
| 35 | + for i in range(len(values)): |
| 36 | + s.append(apply_format(values[i], formatters[i])) |
| 37 | + return delimiter.join(s) |
| 38 | + |
| 39 | +def format_full_seat(seat: dict, delimiter="\t"): |
| 40 | + price = seat.get("price", "n/a") |
| 41 | + section = seat.get("section", "n/a") |
| 42 | + row = seat.get("row", "n/a") |
| 43 | + seat_columns = seat.get("seat_columns", "n/a") |
| 44 | + last_modified = seat.get("last_modified", "n/a") |
| 45 | + return apply( |
| 46 | + [price, section, row, seat_columns, last_modified], |
| 47 | + [price_formatter, default_formatter, default_formatter, |
| 48 | + format_seat_columns, format_date], |
| 49 | + delimiter) |
| 50 | + |
| 51 | +def format_price_only_seat(seat: dict, delimiter="\t"): |
| 52 | + price = seat.get("price", "n/a") |
| 53 | + last_modified = seat.get("last_modified", "n/a") |
| 54 | + return apply([price, last_modified], [price_formatter, format_date], delimiter) |
| 55 | + |
| 56 | +def format_seat(seat: dict, price_only=False, delimiter="\t"): |
| 57 | + if price_only: |
| 58 | + return format_price_only_seat(seat, delimiter) |
| 59 | + else: |
| 60 | + return format_full_seat(seat, delimiter) |
| 61 | + |
| 62 | +def format_seats(seats: list, price_only=False, delimiter="\t"): |
| 63 | + return "\n".join([format_seat(seat, price_only, delimiter) for seat in seats]) |
| 64 | + |
| 65 | + |
| 66 | +def format_entire_mail(pick: Pick, target_price: int, percentile: float, rank: int, num_total: int, top_history_seats: list, same_seats: list): |
| 67 | + """ |
| 68 | + structure of message: |
| 69 | + 1. greetings |
| 70 | + 2. attributes of new seats |
| 71 | + 3. top 3 comparable history seats |
| 72 | + 4. exact same seats if possible |
| 73 | + 5. signature |
| 74 | + """ |
| 75 | + p1 = ( |
| 76 | + f"Hi!" |
| 77 | + ) |
| 78 | + p2 = ( |
| 79 | + f"Congratulations! Ticket tracker reminds you that your ticket subscription request with target price {price_formatter(target_price)} " |
| 80 | + f"found better budget seats (price, section, row, seats) at ({format_full_seat(vars(pick), delimiter=', ')}). " |
| 81 | + f"{decimal_to_percent(percentile)} of all comparable seats in the history are better than the newly found seats, that is, " |
| 82 | + f"they rank no.{rank} out of {num_total} comparable seats in the history." |
| 83 | + ) |
| 84 | + p3 = ( |
| 85 | + f"You can compare to history seats that are better than the newly found seats:" |
| 86 | + f"{chr(10)}" |
| 87 | + f"{format_seats(top_history_seats, price_only=False)}" |
| 88 | + ) if len(top_history_seats) > 0 else "" |
| 89 | + p4 = ( |
| 90 | + f"The newly found seats have history prices:" |
| 91 | + f"{chr(10)}" |
| 92 | + f"{format_seats(same_seats, price_only=True)}" |
| 93 | + ) if len(same_seats) > 0 else "" |
| 94 | + p5 = ( |
| 95 | + f"Bests," |
| 96 | + f"{chr(10)}" |
| 97 | + f"Ticketmaster Ticket Tracker" |
| 98 | + ) |
| 99 | + paras = list(filter(lambda p: len(p) > 0, [p1, p2, p3, p4, p5])) |
| 100 | + return "\n\n".join(paras) |
0 commit comments