Beispiel #1
0
    def _form_plate_lookup_response_parts(
            self,
            borough_data: List[Tuple[str, int]],
            frequency: int,
            fine_data: FineData,
            plate: str,
            plate_types: List[str],
            state: str,
            unique_identifier: str,
            username: str,
            violations: List[Tuple[str, int]],
            year_data: List[Tuple[str, int]],
            camera_streak_data: Optional[CameraStreakData] = None,
            previous_lookup: Optional[PlateLookup] = None):

        # response_chunks holds tweet-length-sized parts of the response
        # to be tweeted out or appended into a single direct message.
        response_chunks: List[str] = []
        violations_string: str = ""

        # Get total violations
        total_violations: int = sum([s['count'] for s in violations])
        LOG.debug(f'total_violations: {total_violations}')

        # Append to initially blank string to build tweet.
        violations_string += L10N.LOOKUP_SUMMARY_STRING.format(
            L10N.VEHICLE_HASHTAG.format(state, plate),
            L10N.get_plate_types_string(plate_types), frequency,
            L10N.pluralize(int(frequency)))

        # If this vehicle has been queried before...
        if previous_lookup:
            previous_num_violations: int = previous_lookup.num_tickets

            violations_string += self._create_repeat_lookup_string(
                new_violations=(total_violations - previous_num_violations),
                plate=plate,
                previous_lookup=previous_lookup,
                state=state,
                username=username)

        response_chunks += self._handle_response_part_formation(
            collection=violations,
            continued_format_string=L10N.LOOKUP_TICKETS_STRING_CONTD.format(
                L10N.VEHICLE_HASHTAG.format(state, plate)),
            count='count',
            cur_string=violations_string,
            description='title',
            default_description='No Year Available',
            prefix_format_string=L10N.LOOKUP_TICKETS_STRING.format(
                total_violations),
            result_format_string=L10N.LOOKUP_RESULTS_DETAIL_STRING,
            username=username)

        if year_data:
            response_chunks += self._handle_response_part_formation(
                collection=year_data,
                continued_format_string=L10N.LOOKUP_YEAR_STRING_CONTD.format(
                    L10N.VEHICLE_HASHTAG.format(state, plate)),
                count='count',
                description='title',
                default_description='No Year Available',
                prefix_format_string=L10N.LOOKUP_YEAR_STRING.format(
                    L10N.VEHICLE_HASHTAG.format(state, plate)),
                result_format_string=L10N.LOOKUP_RESULTS_DETAIL_STRING,
                username=username)

        if borough_data:
            response_chunks += self._handle_response_part_formation(
                collection=borough_data,
                continued_format_string=L10N.LOOKUP_BOROUGH_STRING_CONTD.
                format(L10N.VEHICLE_HASHTAG.format(state, plate)),
                count='count',
                description='title',
                default_description='No Borough Available',
                prefix_format_string=L10N.LOOKUP_BOROUGH_STRING.format(
                    L10N.VEHICLE_HASHTAG.format(state, plate)),
                result_format_string=L10N.LOOKUP_RESULTS_DETAIL_STRING,
                username=username)

        if fine_data and fine_data.fines_assessed():

            cur_string = f"Known fines for {L10N.VEHICLE_HASHTAG.format(state, plate)}:\n\n"

            max_count_length = len('${:,.2f}'.format(fine_data.max_amount()))
            spaces_needed = (max_count_length * 2) + 1

            for fine_type, amount in fine_data:

                currency_string = '${:,.2f}'.format(amount)
                count_length = len(str(currency_string))

                # e.g., if spaces_needed is 5, and count_length is 2, we need
                # to pad to 3.
                left_justify_amount = spaces_needed - count_length

                # formulate next string part
                next_part = (f"{currency_string.ljust(left_justify_amount)}| "
                             f"{fine_type.replace('_', ' ').title()}\n")

                # determine current string length if necessary
                potential_response_length = len(username + ' ' + cur_string +
                                                next_part)

                # If username, space, violation string so far and new part are less or
                # equal than 280 characters, append to existing tweet string.
                if (potential_response_length <=
                        twitter_constants.MAX_TWITTER_STATUS_LENGTH):
                    cur_string += next_part
                else:
                    response_chunks.append(cur_string)

                    cur_string = "Known fines for #{}_{}, cont'd:\n\n"
                    cur_string += next_part

            # add to container
            response_chunks.append(cur_string)

        if camera_streak_data:

            if camera_streak_data.max_streak and camera_streak_data.max_streak >= 5:

                # formulate streak string
                streak_string = (
                    f"Under @bradlander's proposed legislation, "
                    f"this vehicle could have been booted or impounded "
                    f"due to its {camera_streak_data.max_streak} camera violations "
                    f"(>= 5/year) from {camera_streak_data.min_streak_date}"
                    f" to {camera_streak_data.max_streak_date}.\n")

                # add to container
                response_chunks.append(streak_string)

        unique_link: str = self._get_website_plate_lookup_link(
            unique_identifier)

        website_link_string = f'View more details at {unique_link}.'
        response_chunks.append(website_link_string)

        # Send it back!
        return response_chunks
    def _form_plate_lookup_response_parts(
            self,
            borough_data: list[Tuple[str, int]],
            frequency: int,
            fine_data: FineData,
            lookup_source: str,
            plate: str,
            plate_types: list[str],
            state: str,
            unique_identifier: str,
            username: str,
            violations: list[Tuple[str, int]],
            year_data: list[Tuple[str, int]],
            camera_streak_data: dict[str, CameraStreakData] = None,
            previous_lookup: Optional[PlateLookup] = None):

        # response_chunks holds tweet-length-sized parts of the response
        # to be tweeted out or appended into a single direct message.
        response_chunks: list[str] = []
        violations_string: str = ""

        # Get total violations
        total_violations: int = sum([s['count'] for s in violations])
        LOG.debug(f'total_violations: {total_violations}')

        now_in_eastern_time = self.utc.localize(datetime.now())
        time_prefix = now_in_eastern_time.strftime(
            'As of %I:%M:%S %p on %B %-d, %Y:\n\n')

        # Append username to blank string to start to build tweet.
        violations_string += (f'@{username} {time_prefix}' if lookup_source
                              == lookup_sources.LookupSource.STATUS.value else
                              '')

        # Append summary string.
        violations_string += L10N.LOOKUP_SUMMARY_STRING.format(
            L10N.VEHICLE_HASHTAG.format(state, plate),
            L10N.get_plate_types_string(plate_types), frequency,
            L10N.pluralize(int(frequency)))

        # If this vehicle has been queried before...
        if previous_lookup:
            previous_num_violations: int = previous_lookup.num_tickets

            violations_string += self._create_repeat_lookup_string(
                new_violations=(total_violations - previous_num_violations),
                plate=plate,
                previous_lookup=previous_lookup,
                state=state,
                username=username)

        response_chunks.append(violations_string)

        username_prefix = (
            f'@{twitter_constants.HMDNY_TWITTER_HANDLE} {time_prefix}' if
            lookup_source == lookup_sources.LookupSource.STATUS.value else '')

        response_chunks += self._handle_response_part_formation(
            collection=violations,
            continued_format_string=L10N.LOOKUP_TICKETS_STRING_CONTD.format(
                L10N.VEHICLE_HASHTAG.format(state, plate)),
            count='count',
            description='title',
            default_description='No Year Available',
            prefix_format_string=L10N.LOOKUP_TICKETS_STRING.format(
                total_violations),
            result_format_string=L10N.LOOKUP_RESULTS_DETAIL_STRING,
            username_prefix=username_prefix)

        if year_data:
            response_chunks += self._handle_response_part_formation(
                collection=year_data,
                continued_format_string=L10N.LOOKUP_YEAR_STRING_CONTD.format(
                    L10N.VEHICLE_HASHTAG.format(state, plate)),
                count='count',
                description='title',
                default_description='No Year Available',
                prefix_format_string=L10N.LOOKUP_YEAR_STRING.format(
                    L10N.VEHICLE_HASHTAG.format(state, plate)),
                result_format_string=L10N.LOOKUP_RESULTS_DETAIL_STRING,
                username_prefix=username_prefix)

        if borough_data:
            response_chunks += self._handle_response_part_formation(
                collection=borough_data,
                continued_format_string=L10N.LOOKUP_BOROUGH_STRING_CONTD.
                format(L10N.VEHICLE_HASHTAG.format(state, plate)),
                count='count',
                description='title',
                default_description='No Borough Available',
                prefix_format_string=L10N.LOOKUP_BOROUGH_STRING.format(
                    L10N.VEHICLE_HASHTAG.format(state, plate)),
                result_format_string=L10N.LOOKUP_RESULTS_DETAIL_STRING,
                username_prefix=username_prefix)

        if fine_data and fine_data.fines_assessed():

            cur_string = (
                f'{username_prefix}'
                f'Known fines for {L10N.VEHICLE_HASHTAG.format(state, plate)}:\n\n'
            )

            max_count_length = len('${:,.2f}'.format(fine_data.max_amount()))
            spaces_needed = (max_count_length * 2) + 1

            for fine_type, amount in fine_data:

                currency_string = '${:,.2f}'.format(amount)
                count_length = len(str(currency_string))

                # e.g., if spaces_needed is 5, and count_length is 2, we need
                # to pad to 3.
                left_justify_amount = spaces_needed - count_length

                # formulate next string part
                next_part = (f"{currency_string.ljust(left_justify_amount)}| "
                             f"{fine_type.replace('_', ' ').title()}\n")

                # determine current string length if necessary
                potential_response_length = len(cur_string + next_part)

                # If violation string so far and new part are less or
                # equal than 280 characters, append to existing tweet string.
                if (potential_response_length <=
                        twitter_constants.MAX_TWITTER_STATUS_LENGTH):
                    cur_string += next_part
                else:
                    response_chunks.append(cur_string)

                    cur_string = "Known fines for #{}_{}, cont'd:\n\n"
                    cur_string += next_part

            # add to container
            response_chunks.append(cur_string)

        for camera_violation_type, threshold in self.CAMERA_THRESHOLDS.items():
            violation_type_data = camera_streak_data[camera_violation_type]

            if violation_type_data:

                if (camera_violation_type == 'Failure to Stop at Red Light'
                        and violation_type_data.max_streak >= threshold):

                    # add to container
                    response_chunks.append(
                        L10N.
                        DANGEROUS_VEHICLE_ABATEMENT_ACT_REPEAT_OFFENDER_STRING.
                        format(username_prefix, violation_type_data.max_streak,
                               'red light', threshold,
                               violation_type_data.min_streak_date,
                               violation_type_data.max_streak_date))

                elif (camera_violation_type
                      == 'School Zone Speed Camera Violation'
                      and violation_type_data.max_streak >= threshold):

                    # add to container
                    response_chunks.append(
                        L10N.
                        DANGEROUS_VEHICLE_ABATEMENT_ACT_REPEAT_OFFENDER_STRING.
                        format(username_prefix, violation_type_data.max_streak,
                               'school zone speed', threshold,
                               violation_type_data.min_streak_date,
                               violation_type_data.max_streak_date))

        unique_link: str = self._get_website_plate_lookup_link(
            unique_identifier)

        website_link_string = f'View more details at {unique_link}.'
        response_chunks.append(website_link_string)

        # Send it back!
        return response_chunks