Skip to content

base_search

Recreation.gov Web Searching Utilities

BaseCampingSearch #

Bases: ABC

Camping Search Object

Source code in camply/search/base_search.py
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
class BaseCampingSearch(ABC):
    """
    Camping Search Object
    """

    campgrounds: List[CampgroundFacility] = []
    list_campsites_supported: bool = True
    notifier: Optional[MultiNotifierProvider] = None

    def __init__(
        self,
        search_window: Union[SearchWindow, List[SearchWindow]],
        weekends_only: bool = False,
        nights: int = 1,
        offline_search: bool = False,
        offline_search_path: Optional[str] = None,
        days_of_the_week: Optional[Sequence[int]] = None,
        **kwargs,
    ) -> None:
        """
        Initialize with Search Parameters

        Parameters
        ----------
        search_window: Union[SearchWindow, List[SearchWindow]]
            Search Window tuple containing start date and End Date
        weekends_only: bool
            Whether to only search for Camping availabilities on the weekends (Friday /
            Saturday nights)
        nights: int
            minimum number of consecutive nights to search per campsite,defaults to 1
        offline_search: bool
            When set to True, the campsite search will both save the results of the
            campsites it's found, but also load those campsites before beginning a
            search for other campsites.
        offline_search_path: Optional[str]
            When offline search is set to True, this is the name of the file to be saved/loaded.
            When not specified, the filename will default to `camply_campsites.json`
        days_of_the_week: Optional[Sequence[int]]
            Days of the week (by weekday integer) to search for.
        """
        self._verbose = kwargs.get("verbose", True)
        self.campsite_finder: ProviderType = self.provider_class()
        self.search_window: List[SearchWindow] = make_list(search_window)
        self.days_of_the_week = set(
            days_of_the_week if days_of_the_week is not None else ()
        )
        self.weekends_only: bool = weekends_only
        if self.weekends_only is True:
            self.days_of_the_week.add(4)
            self.days_of_the_week.add(5)
        if len(self.days_of_the_week) == 0:
            self.days_of_the_week = {0, 1, 2, 3, 4, 5, 6}
        self._original_search_days: List[datetime] = self._get_search_days()
        self._original_search_months: List[
            datetime
        ] = self.campsite_finder.get_search_months(self._original_search_days)
        self.nights = self._validate_consecutive_nights(nights=nights)
        if offline_search_path is not None:
            self.offline_search = True
        else:
            self.offline_search = offline_search
        self.offline_search_path = self._set_offline_search_path(
            file_path=offline_search_path
        )
        self.campsites_found: Set[AvailableCampsite] = set()
        self.loaded_campsites: Set[AvailableCampsite] = set()
        if self.offline_search_path.suffixes[-1] == ".json":
            self.offline_mode: str = "json"
        elif self.offline_search_path.suffixes[-1] in [".pkl", ".pickle"]:
            self.offline_mode: str = "pickle"
        else:
            raise CamplyError(
                "You must provide a `.json` or a `.pickle` / `.pkl` file name for offline searches"
            )
        if self.offline_search is True:
            logger.info(
                "Campsite search is configured to save offline: %s",
                self.offline_search_path,
            )
            self.campsites_found: Set[
                AvailableCampsite
            ] = self.load_campsites_from_file()
            self.loaded_campsites: Set[AvailableCampsite] = self.campsites_found.copy()

    @property
    def search_days(self) -> List[datetime]:
        """
        Get the Unique Days that need to be Searched
        """
        current_date = datetime.now().date()
        return [day for day in self._original_search_days if day >= current_date]

    @property
    def search_months(self) -> List[datetime]:
        """
        Get the Unique Months that need to be Searched
        """
        current_month = datetime.now().date().replace(day=1)
        return [day for day in self._original_search_months if day >= current_month]

    @abstractmethod
    def get_all_campsites(self) -> List[AvailableCampsite]:
        """
        Perform the Search and Return Matching Availabilities.

        This method must be implemented
        on all sub-classes.

        Returns
        -------
        List[AvailableCampsite]
        """

    @property
    @abstractmethod
    def provider_class(self) -> ProviderType:
        """
        Provider Class Dependency Injection
        """

    def _get_intersection_date_overlap(
        self,
        date: datetime,
        periods: int,
        search_days: List[datetime],
    ) -> bool:
        """
        Find Date Overlap

        Parameters
        ----------
        date: datetime
        periods: int
        search_days: List[datetime]

        Returns
        -------
        bool
        """
        timestamp_range = date_range(start=date, periods=periods)
        campsite_date_range = {item.date() for item in timestamp_range}
        intersection = campsite_date_range.intersection(search_days)
        if intersection:
            return True
        else:
            return False

    def _compare_date_overlap(self, campsite: AvailableCampsite) -> bool:
        """
        See whether a campsite should be returned as found

        Parameters
        ----------
        campsite: AvailableCampsite

        Returns
        -------
        bool
        """
        intersection = self._get_intersection_date_overlap(
            date=campsite.booking_date,
            periods=campsite.booking_nights,
            search_days=self.search_days,
        )
        return intersection

    def _filter_date_overlap(self, campsites: DataFrame) -> pd.DataFrame:
        """
        See whether a campsite should be returned as found

        Parameters
        ----------
        campsites: pd.DataFrame

        Returns
        -------
        pd.DataFrame
        """
        matches = campsites.apply(
            lambda x: self._get_intersection_date_overlap(
                date=x.booking_date.to_pydatetime(),
                periods=x.booking_nights,
                search_days=self.search_days,
            ),
            axis=1,
        )
        filtered_campsites = campsites[matches].copy().reset_index(drop=True)
        return filtered_campsites

    def _search_matching_campsites_available(
        self, log: bool = False, verbose: bool = False, raise_error: bool = False
    ) -> List[AvailableCampsite]:
        """
        Perform the Search and Return Matching Availabilities

        Parameters
        ----------
        log: bool
            Whether to log found campsites
        verbose: bool
            Used with `log` to enhance the amount of info logged to the console
        raise_error: bool
            Whether to raise an error if nothing is found. Defaults to False.

        Returns
        -------
        List[AvailableCampsite]
        """
        matching_campgrounds = []
        for camp in self.get_all_campsites():
            if all(
                [
                    self._compare_date_overlap(campsite=camp) is True,
                    camp.booking_nights >= self.nights,
                ]
            ):
                matching_campgrounds.append(camp)
        logger.info(
            f"{(get_emoji(matching_campgrounds) + ' ') * 4}{len(matching_campgrounds)} "
            "Reservable Campsites Matching Search Preferences"
        )
        self.assemble_availabilities(
            matching_data=matching_campgrounds, log=log, verbose=verbose
        )
        if (
            self.offline_search is True
            and self.loaded_campsites.issuperset(matching_campgrounds)
            and raise_error is True
        ):
            campsite_availability_message = (
                "No New Campsites were found, we'll continue checking"
            )
            logger.info(campsite_availability_message)
            raise CampsiteNotFoundError(campsite_availability_message)
        elif len(matching_campgrounds) == 0 and raise_error is True:
            campsite_availability_message = (
                "No Campsites were found, we'll continue checking"
            )
            logger.info(campsite_availability_message)
            raise CampsiteNotFoundError(campsite_availability_message)
        return matching_campgrounds

    @classmethod
    def _get_polling_minutes(cls, polling_interval: Optional[int]) -> int:
        """
        Return the Number of Minutes to Search

        Parameters
        ----------
        polling_interval: Optional[int]
            Used with `continuous=True`, the amount of time to wait between searches.
            Defaults to 10 if not provided, cannot be less than 5

        Returns
        -------
        int
        """
        if polling_interval is None:
            polling_interval = getenv(
                "POLLING_INTERVAL", SearchConfig.RECOMMENDED_POLLING_INTERVAL
            )
        if int(polling_interval) < SearchConfig.POLLING_INTERVAL_MINIMUM:
            polling_interval = SearchConfig.POLLING_INTERVAL_MINIMUM
        polling_interval_minutes = int(round(float(polling_interval), 2))
        return polling_interval_minutes

    def _continuous_search_retry(
        self,
        log: bool,
        verbose: bool,
        polling_interval: int,
        continuous_search_attempts: int,
        notification_provider: Union[str, List[str], BaseNotifications, None],
        notify_first_try: bool,
        search_once: bool = False,
    ) -> List[AvailableCampsite]:
        """
        Search for Campsites until at least one is found

        Parameters
        ----------
        log: bool
            Whether to log found campsites
        verbose: bool
            Used with `log` to enhance the amount of info logged to the console
        polling_interval: Optional[int]
            Used with `continuous=True`, the amount of time to wait between searches.
            Defaults to 10 if not provided, cannot be less than 5
        continuous_search_attempts: int
            Number of preexisting search attempts
        notification_provider: provider: Union[str, List[str]]
            Used with `continuous=True`, Name of notification provider to use. Accepts
            "email", "pushover", and defaults to "silent". Also accepts a list or commma
            separated string of these options or even a notification provider object itself
        notify_first_try: bool
            Used with `continuous=True`, whether to send all non-silent notifications if more
            than 5 matching campsites are found on the first try. Defaults to false which
            only sends the first 5.
        search_once: bool
            Whether to only search once (and not actually continuously)

        Returns
        -------
        List[AvailableCampsite]
        """
        polling_interval_minutes = self._get_polling_minutes(
            polling_interval=polling_interval
        )
        self.notifier = MultiNotifierProvider(provider=notification_provider)
        logger.info(
            f"Searching for campsites every {polling_interval_minutes} minutes. "
        )
        self.notifier.log_providers()
        retryer = tenacity.Retrying(
            retry=tenacity.retry_if_exception_type(CampsiteNotFoundError),
            wait=tenacity.wait.wait_fixed(int(polling_interval_minutes) * 60),
        )
        matching_campsites = retryer.__call__(
            fn=self._search_matching_campsites_available,
            log=False,
            verbose=False,
            raise_error=not search_once,
        )
        found_campsites = set(matching_campsites)
        new_campsites = found_campsites.difference(self.campsites_found)
        self.assemble_availabilities(
            matching_data=list(new_campsites), log=log, verbose=verbose
        )
        logger.info(f"{len(new_campsites)} New Campsites Found.")
        self.campsites_found.update(new_campsites)
        logged_campsites = list(new_campsites)
        self._handle_notifications(
            retryer=retryer,
            notifier=self.notifier,
            logged_campsites=logged_campsites,
            continuous_search_attempts=continuous_search_attempts,
            notify_first_try=notify_first_try,
        )
        return list(self.campsites_found)

    @classmethod
    def _handle_notifications(
        cls,
        retryer: tenacity.Retrying,
        notifier: MultiNotifierProvider,
        logged_campsites: List[AvailableCampsite],
        continuous_search_attempts: int,
        notify_first_try: bool,
    ) -> None:
        """
        Handle sending notifications

        Parameters
        ----------
        retryer: tenacity.Retrying
        notifier: MultiNotifierProvider
        logged_campsites: List[AvailableCampsite]
        continuous_search_attempts: int
        notify_first_try: bool

        Returns
        -------
        None
        """
        attempt_number = retryer.statistics.get("attempt_number", 1)
        minimum_first_notify = SearchConfig.MINIMUM_CAMPSITES_FIRST_NOTIFY
        if max([attempt_number, continuous_search_attempts]) > 1:
            logged_campsites = cls._handle_too_many_campsites_found(
                notifier=notifier, logged_campsites=logged_campsites
            )
            notifier.send_campsites(campsites=logged_campsites)
        elif attempt_number == 1 and notify_first_try is True:
            logged_campsites = cls._handle_too_many_campsites_found(
                notifier=notifier, logged_campsites=logged_campsites
            )
            notifier.send_campsites(campsites=logged_campsites)
        else:
            if (
                len(notifier.providers) > 1
                and len(logged_campsites) > minimum_first_notify
            ):
                error_message = (
                    f"Found more than {minimum_first_notify} "
                    f"matching campsites ({len(logged_campsites)}) on the "
                    "first try. Try searching online instead. "
                    f"camply is only sending the first "
                    f"{minimum_first_notify} notifications. "
                    "Go Get your campsite! πŸ•"
                )
                logger.warning(error_message)
                notifier.send_message(message=error_message)
                logged_campsites = logged_campsites[:minimum_first_notify]
            notifier.send_campsites(campsites=logged_campsites)

    @classmethod
    def _handle_too_many_campsites_found(
        cls, notifier: MultiNotifierProvider, logged_campsites: List[AvailableCampsite]
    ) -> List[AvailableCampsite]:
        """
        Handle Scenarios Where Too Many Campsites are Found

        Parameters
        ----------
        notifier: MultiNotifierProvider
        logged_campsites: List[AvailableCampsite]

        Returns
        -------
        List[AvailableCampsite]
        """
        limit = SearchConfig.MAXIMUM_NOTIFICATION_BATCH_SIZE
        number_campsites = len(logged_campsites)
        if number_campsites > limit:
            warning_message = (
                f"Too many campsites were found during the search ({number_campsites} "
                f"total). camply will only send you the first {limit} notifications."
            )
            logger.warning(warning_message)
            restricted_campsites = logged_campsites[:limit]
            notifier.send_message(warning_message)
        else:
            restricted_campsites = logged_campsites
        return restricted_campsites

    def _search_campsites_continuous(
        self,
        log: bool = True,
        verbose: bool = False,
        polling_interval: Optional[int] = None,
        notification_provider: str = "silent",
        notify_first_try: bool = False,
        search_forever: bool = False,
        search_once: bool = False,
    ):
        """
        Continuously Search For Campsites

        Parameters
        ----------
        log: bool
            Whether to log found campsites
        verbose: bool
            Used with `log` to enhance the amount of info logged to the console
        polling_interval: Optional[int]
            Used with `continuous=True`, the amount of time to wait between searches.
            Defaults to 10 if not provided, cannot be less than 5
        notification_provider: str
            Used with `continuous=True`, Name of notification provider to use. Accepts
            "email", "pushover", and defaults to "silent". Also accepts a list or commma
            separated string of these options or even a notification provider object itself
        notify_first_try: bool
            Used with `continuous=True`, whether to send all non-silent notifications if more
            than 5 matching campsites are found on the first try. Defaults to false which
            only sends the first 5.
        search_forever: bool
            Used with `continuous=True`, This option searches for new campsites forever, with
            the caveat being that it will never notify about the same campsite.
        search_once: bool
            Whether to only search once (and not actually continuously)

        Returns
        -------
        List[AvailableCampsite]
        """
        polling_interval_minutes = self._get_polling_minutes(
            polling_interval=polling_interval
        )
        continuous_search = True
        continuous_search_attempts = 1
        while continuous_search is True:
            starting_count = len(self.campsites_found)
            self._continuous_search_retry(
                log=log,
                verbose=verbose,
                polling_interval=polling_interval,
                notification_provider=notification_provider,
                notify_first_try=notify_first_try,
                continuous_search_attempts=continuous_search_attempts,
                search_once=search_once,
            )
            ending_count = len(self.campsites_found)
            continuous_search_attempts += 1
            if self.offline_search is True and ending_count > starting_count:
                self.unload_campsites_to_file()
            if search_once is True:
                continuous_search = False
            elif search_forever is True:
                sleep(int(polling_interval_minutes) * 60)
            else:
                continuous_search = False
        return list(self.campsites_found)

    def get_matching_campsites(
        self,
        log: bool = True,
        verbose: bool = False,
        continuous: bool = False,
        polling_interval: Optional[int] = None,
        notification_provider: str = "silent",
        notify_first_try: bool = False,
        search_forever: bool = False,
        search_once: bool = False,
    ) -> List[AvailableCampsite]:
        """
        Perform the Search and Return Matching Availabilities

        Parameters
        ----------
        log: bool
            Whether to log found campsites
        verbose: bool
            Used with `log` to enhance the amount of info logged to the console
        continuous: bool
            Whether to continue searching beyond just the first time
        polling_interval: Optional[int]
            Used with `continuous=True`, the amount of time to wait between searches.
            Defaults to 10 if not provided, cannot be less than 5
        notification_provider: str
            Used with `continuous=True`, Name of notification provider to use. Accepts
            "email", "pushover", and defaults to "silent". Also accepts a list or commma
            separated string of these options or even a notification provider object itself
        notify_first_try: bool
            Used with `continuous=True`, whether to send all non-silent notifications if more
            than 5 matching campsites are found on the first try. Defaults to false which
            only sends the first 5.
        search_forever: bool
            Used with `continuous=True`, This option searches for new campsites forever, with
            the caveat being that it will never notify about the same campsite.
        search_once: bool
            Whether to only search once (and not actually continuously)

        Returns
        -------
        List[AvailableCampsite]
        """
        if continuous is True or search_once is True:
            try:
                self._search_campsites_continuous(
                    log=log,
                    verbose=verbose,
                    polling_interval=polling_interval,
                    notification_provider=notification_provider,
                    notify_first_try=notify_first_try,
                    search_forever=search_forever,
                    search_once=search_once,
                )
            except Exception as e:
                self.notifier.last_gasp(error=e)
                raise e
        else:
            starting_count = len(self.campsites_found)
            matching_campsites = self._search_matching_campsites_available(
                log=log, verbose=True
            )
            self.campsites_found.update(set(matching_campsites))
            ending_count = len(self.campsites_found)
            if self.offline_search is True and ending_count > starting_count:
                self.unload_campsites_to_file()
        return list(self.campsites_found)

    def _get_search_days(self) -> List[datetime]:
        """
        Retrieve Specific Days to Search For

        Returns
        -------
        search_days: List[datetime]
            Datetime days to search for reservations
        """
        current_date = datetime.now().date()
        search_nights = set()
        for window in self.search_window:
            generated_dates = {
                date for date in window.get_date_range() if date >= current_date
            }
            search_nights.update(generated_dates)
        search_nights: Set[datetime] = {
            x for x in search_nights if x.weekday() in self.days_of_the_week
        }
        max_nights_to_list = 2
        all_nights = 7
        if len(search_nights) > 0 and self._verbose is True:
            logger.info(
                f"{len(search_nights)} booking nights selected for search, "
                f"ranging from {min(search_nights)} to {max(search_nights)}"
            )
        if 0 < len(self.days_of_the_week) <= max_nights_to_list:
            day_mapping = {value: key for key, value in days_of_the_week_base.items()}
            week_nights = [day_mapping[item] for item in sorted(self.days_of_the_week)]
            logger.info(
                "Searching for booking nights on %s",
                " and ".join(week_nights),
            )
        elif len(self.days_of_the_week) < all_nights:
            logger.info(
                "Searching across %s specific days of the week",
                len(self.days_of_the_week),
            )
        elif len(self.days_of_the_week) == all_nights:
            pass
        else:
            logger.error(SearchConfig.ERROR_MESSAGE)
            raise RuntimeError(SearchConfig.ERROR_MESSAGE)
        return sorted(search_nights)

    @classmethod
    def _consolidate_campsites(
        cls, campsite_df: DataFrame, nights: int
    ) -> pd.DataFrame:
        """
        Consolidate Single Night Campsites into Multiple Night Campsites

        Parameters
        ----------
        campsite_df: DataFrame
            DataFrame of AvailableCampsites

        Returns
        -------
        pd.DataFrame
        """
        composed_groupings = []
        for _, campsite_slice in campsite_df.groupby(
            [CampsiteContainerFields.CAMPSITE_ID, CampsiteContainerFields.CAMPGROUND_ID]
        ):
            # SORT THE VALUES AND CREATE A COPIED SLICE
            campsite_grouping = campsite_slice.sort_values(
                by=CampsiteContainerFields.BOOKING_DATE, ascending=True
            ).copy()
            # ASSEMBLE THE CAMPSITES AVAILABILITIES INTO GROUPS THAT ARE CONSECUTIVE
            booking_date = campsite_grouping[CampsiteContainerFields.BOOKING_DATE]
            date = Timedelta("1d")
            consecutive_nights = booking_date.diff() != date
            group_identifier = consecutive_nights.cumsum()
            campsite_grouping[CampsiteContainerFields.CAMPSITE_GROUP] = group_identifier
            # USE THE ASSEMBLED GROUPS TO CREATE UPDATED CAMPSITES AND REMOVE DUPLICATES
            for _campsite_group, campsite_group_slice in campsite_grouping.groupby(
                CampsiteContainerFields.CAMPSITE_GROUP
            ):
                composed_grouping = campsite_group_slice.sort_values(
                    by=CampsiteContainerFields.BOOKING_DATE, ascending=True
                ).copy()
                composed_grouping.drop(
                    columns=[CampsiteContainerFields.CAMPSITE_GROUP], inplace=True
                )
                nightly_breakouts = cls._find_consecutive_nights(
                    dataframe=composed_grouping, nights=nights
                )
                composed_groupings.append(nightly_breakouts)
        if len(composed_groupings) == 0:
            composed_groupings = [DataFrame()]
        return concat(composed_groupings, ignore_index=True)

    @classmethod
    def _consecutive_subseq(cls, iterable: Iterable, length: int) -> Generator:
        """
        Find All Sub Sequences by length Given a List

        See https://tinyurl.com/5av5unjd

        Parameters
        ----------
        iterable: Iterable
        length: int

        Returns
        -------
        Generator
        """
        for _, consec_run in groupby(enumerate(iterable), lambda x: x[0] - x[1]):
            k_wise = tee(map(itemgetter(1), consec_run), length)
            for n, it in enumerate(k_wise):
                next(islice(it, n, n), None)
            yield from zip(*k_wise)

    @classmethod
    def _find_consecutive_nights(cls, dataframe: DataFrame, nights: int) -> DataFrame:
        """
        Explode a DataFrame of Consecutive Nightly Campsite Availabilities,

        Expand to all unique possibilities given the length of the stay.

        Parameters
        ----------
        dataframe: DataFrame
        nights: int

        Returns
        -------
        DataFrame
        """
        duplicate_subset = set(dataframe.columns) - AvailableCampsite.__unhashable__
        dataframe_slice = dataframe.copy().reset_index(drop=True)
        nights_indexes = dataframe_slice.booking_date.index
        consecutive_generator = cls._consecutive_subseq(
            iterable=nights_indexes, length=nights
        )
        sequences = list(consecutive_generator)
        concatted_data = []
        for sequence in sequences:
            index_list = list(sequence)
            data_copy = dataframe_slice.iloc[index_list].copy()
            data_copy.booking_date = data_copy.booking_date.min()
            data_copy.booking_end_date = data_copy.booking_end_date.max()
            data_copy.booking_url = data_copy.booking_url.loc[index_list[0]]
            data_copy.booking_nights = (
                data_copy.booking_end_date - data_copy.booking_date
            ).dt.days
            data_copy.drop_duplicates(inplace=True, subset=duplicate_subset)
            concatted_data.append(data_copy)
        if len(concatted_data) == 0:
            concatted_data = [DataFrame()]
        return concat(concatted_data, ignore_index=True)

    def _validate_consecutive_nights(self, nights: int) -> int:
        """
        Validate the number of consecutive nights to search

        Parameters
        ----------
        nights : int
            Number of nights to check

        Returns
        -------
        int
            The proper number of nights to search
        """
        search_days = Series(self.search_days)
        consecutive_nights = search_days.diff() != Timedelta("1d")
        largest_grouping = consecutive_nights.cumsum().value_counts().max()
        if nights > 1:
            logger.info(
                f"Searching for availabilities with {nights} consecutive night stays."
            )
        if nights > largest_grouping:
            logger.warning(
                "Too many consecutive nights selected. "
                "The consecutive night parameter will be set to "
                f"the max possible, {largest_grouping}."
            )
            return largest_grouping
        else:
            return nights

    @staticmethod
    def campsites_to_df(campsites: List[AvailableCampsite]) -> DataFrame:
        """
        Convert Campsite Array to

        Parameters
        ----------
        campsites: List[AvailableCampsite]

        Returns
        -------
        DataFrame
        """
        campsite_df = DataFrame(
            data=[campsite.dict() for campsite in campsites],
            columns=AvailableCampsite.__fields__,
        )
        return campsite_df

    @staticmethod
    def df_to_campsites(campsite_df: DataFrame) -> List[AvailableCampsite]:
        """
        Convert Campsite DataFrame to array of AvailableCampsite objects

        Parameters
        ----------
        campsite_df: DataFrame

        Returns
        -------
        List[AvailableCampsite]
        """
        composed_campsite_array = []
        composed_campsite_data_array = campsite_df.to_dict(orient="records")
        for campsite_record in composed_campsite_data_array:
            campsite_record["booking_date"] = campsite_record[
                "booking_date"
            ].to_pydatetime()
            campsite_record["booking_end_date"] = campsite_record[
                "booking_end_date"
            ].to_pydatetime()
            composed_campsite_array.append(AvailableCampsite(**campsite_record))
        return composed_campsite_array

    @classmethod
    def assemble_availabilities(
        cls,
        matching_data: List[AvailableCampsite],
        log: bool = True,
        verbose: bool = False,
    ) -> DataFrame:
        """
        Prepare a Pandas DataFrame from Array of AvailableCampsite objects

        Parameters
        ----------
        matching_data: List[AvailableCampsite]
            List of campsites to assemble
        log: bool
            Whether to log found campsites
        verbose: bool
            Used with `log` to enhance the amount of info logged to the console

        Returns
        -------
        availability_df: DataFrame
        """
        availability_df = cls.campsites_to_df(campsites=matching_data)
        if log is True:
            cls._log_availabilities(availability_df=availability_df, verbose=verbose)
        return availability_df

    @classmethod
    def _log_availabilities(
        cls, availability_df: DataFrame, verbose: bool
    ) -> DataFrame:
        """
        Log the Availabilities

        Parameters
        ----------
        availability_df: DataFrame
        verbose: bool

        Returns
        -------
        DataFrame
        """
        booking_date: datetime
        for booking_date, available_sites in availability_df.groupby("booking_date"):
            logger.info(
                f"πŸ“… {booking_date.strftime('%a, %B %d')} "
                f"πŸ•  {len(available_sites)} sites"
            )
            location_tuple: tuple
            for location_tuple, campground_availability in available_sites.groupby(
                [DataColumns.RECREATION_AREA_COLUMN, DataColumns.FACILITY_NAME_COLUMN]
            ):
                logger.info(
                    f"\t⛰️  {'  πŸ•  '.join(location_tuple)}: β›Ί "
                    f"{len(campground_availability)} sites"
                )
                if verbose is True:
                    for (
                        booking_nights,
                        nightly_availability,
                    ) in campground_availability.groupby(
                        DataColumns.BOOKING_NIGHTS_COLUMN
                    ):
                        unique_urls = nightly_availability[
                            DataColumns.BOOKING_URL_COLUMN
                        ].unique()
                        for booking_url in sorted(unique_urls):
                            logger.info(
                                f"\t\tπŸ”— {booking_url} "
                                f"({booking_nights} night"
                                f"{'s' if booking_nights > 1 else ''})"
                            )
        return availability_df

    def unload_campsites_to_file(self) -> pathlib.Path:
        """
        Unload a BaseSearch Object's campsites to a serialized Pickle file.

        Returns
        -------
        pathlib.Path
        """
        if self.offline_mode == "pickle":
            with open(self.offline_search_path, mode="wb") as file_stream:
                pickle.dump(
                    obj=self.campsites_found,
                    file=file_stream,
                    protocol=4,
                    fix_imports=True,
                )
                file_stream.seek(0)
        elif self.offline_mode == "json":
            with open(self.offline_search_path, mode="w") as file_stream:
                json.dump(
                    obj=self.campsites_found,
                    fp=file_stream,
                    sort_keys=True,
                    default=pydantic_encoder,
                    indent=4,
                )
                file_stream.seek(0)
        logger.debug(
            "%s campsites saved to file: %s",
            len(self.campsites_found),
            self.offline_search_path,
        )
        return self.offline_search_path

    def load_campsites_from_file(self) -> Set[AvailableCampsite]:
        """
        Load a BaseSearch Object's campsites from a serialized Pickle file.

        Returns
        -------
        Set[AvailableCampsite]
        """
        if self.offline_search_path.exists():
            if self.offline_mode == "pickle":
                with open(self.offline_search_path, mode="rb") as file_stream:
                    campsites: Set[AvailableCampsite] = pickle.load(
                        file=file_stream, fix_imports=True
                    )
            elif self.offline_mode == "json":
                with open(self.offline_search_path, mode="r") as file_stream:
                    campsites_dicts: List[Dict[str, Any]] = json.load(file_stream)
                campsites: Set[AvailableCampsite] = {
                    AvailableCampsite(**json_dict) for json_dict in campsites_dicts
                }
            if len(campsites) > 0:
                logger.info(
                    "%s campsites loaded from file: %s",
                    len(campsites),
                    self.offline_search_path,
                )
        else:
            campsites = set()
        return campsites

    @staticmethod
    def _set_offline_search_path(file_path: Optional[str]) -> pathlib.Path:
        default_file_path = "camply_campsites.json"
        if file_path is None:
            file_path = default_file_path
        returned_path = pathlib.Path(file_path).resolve()
        parent_dir = pathlib.Path.cwd()
        if all(
            [
                returned_path.exists(),
                returned_path.is_file(),
                set(returned_path.suffixes).issubset({".pkl", ".pickle", ".json"}),
            ]
        ):
            path_obj = returned_path
        elif all(
            [
                returned_path.exists(),
                returned_path.is_dir(),
            ]
        ):
            path_obj = returned_path.joinpath(default_file_path)
        elif all(
            [
                returned_path.parent.exists(),
                returned_path.parent.is_dir(),
            ]
        ):
            path_obj = returned_path
        else:
            path_obj = parent_dir.joinpath(file_path)
        if not path_obj.parent.exists():
            raise FileNotFoundError(f"That directory doesn't exist: {path_obj.parent}")
        return path_obj

    @abstractmethod
    def list_campsite_units(self) -> Any:
        """
        List Campsite Units

        Returns
        -------
        Any
        """

    @classmethod
    def log_listed_campsites(
        cls,
        campsites: Sequence[ListedCampsite],
        facilities: Sequence[CampgroundFacility],
    ) -> None:
        """
        Print the campsites to the console

        Parameters
        ----------
        facilities: List[CampgroundFacility]
        campsites: List[ListedCampsite]

        Returns
        -------
        None
        """
        logger.info("Found %s campgrounds to search", len(facilities))
        logger.info("Found %s Campsites", len(campsites))
        for facility in sorted(facilities, key=lambda x: x.facility_id):
            logger.info("πŸ•  %s - (#%s)", facility.facility_name, facility.facility_id)
            for item in campsites:
                if item.facility_id == facility.facility_id:
                    logger.info("    ⛺️ %s - (#%s)", item.name, item.id)

provider_class: ProviderType abstractmethod property #

Provider Class Dependency Injection

search_days: List[datetime] property #

Get the Unique Days that need to be Searched

search_months: List[datetime] property #

Get the Unique Months that need to be Searched

__init__(search_window, weekends_only=False, nights=1, offline_search=False, offline_search_path=None, days_of_the_week=None, **kwargs) #

Initialize with Search Parameters

Parameters:

Name Type Description Default
search_window Union[SearchWindow, List[SearchWindow]]

Search Window tuple containing start date and End Date

required
weekends_only bool

Whether to only search for Camping availabilities on the weekends (Friday / Saturday nights)

False
nights int

minimum number of consecutive nights to search per campsite,defaults to 1

1
offline_search bool

When set to True, the campsite search will both save the results of the campsites it's found, but also load those campsites before beginning a search for other campsites.

False
offline_search_path Optional[str]

When offline search is set to True, this is the name of the file to be saved/loaded. When not specified, the filename will default to camply_campsites.json

None
days_of_the_week Optional[Sequence[int]]

Days of the week (by weekday integer) to search for.

None
Source code in camply/search/base_search.py
def __init__(
    self,
    search_window: Union[SearchWindow, List[SearchWindow]],
    weekends_only: bool = False,
    nights: int = 1,
    offline_search: bool = False,
    offline_search_path: Optional[str] = None,
    days_of_the_week: Optional[Sequence[int]] = None,
    **kwargs,
) -> None:
    """
    Initialize with Search Parameters

    Parameters
    ----------
    search_window: Union[SearchWindow, List[SearchWindow]]
        Search Window tuple containing start date and End Date
    weekends_only: bool
        Whether to only search for Camping availabilities on the weekends (Friday /
        Saturday nights)
    nights: int
        minimum number of consecutive nights to search per campsite,defaults to 1
    offline_search: bool
        When set to True, the campsite search will both save the results of the
        campsites it's found, but also load those campsites before beginning a
        search for other campsites.
    offline_search_path: Optional[str]
        When offline search is set to True, this is the name of the file to be saved/loaded.
        When not specified, the filename will default to `camply_campsites.json`
    days_of_the_week: Optional[Sequence[int]]
        Days of the week (by weekday integer) to search for.
    """
    self._verbose = kwargs.get("verbose", True)
    self.campsite_finder: ProviderType = self.provider_class()
    self.search_window: List[SearchWindow] = make_list(search_window)
    self.days_of_the_week = set(
        days_of_the_week if days_of_the_week is not None else ()
    )
    self.weekends_only: bool = weekends_only
    if self.weekends_only is True:
        self.days_of_the_week.add(4)
        self.days_of_the_week.add(5)
    if len(self.days_of_the_week) == 0:
        self.days_of_the_week = {0, 1, 2, 3, 4, 5, 6}
    self._original_search_days: List[datetime] = self._get_search_days()
    self._original_search_months: List[
        datetime
    ] = self.campsite_finder.get_search_months(self._original_search_days)
    self.nights = self._validate_consecutive_nights(nights=nights)
    if offline_search_path is not None:
        self.offline_search = True
    else:
        self.offline_search = offline_search
    self.offline_search_path = self._set_offline_search_path(
        file_path=offline_search_path
    )
    self.campsites_found: Set[AvailableCampsite] = set()
    self.loaded_campsites: Set[AvailableCampsite] = set()
    if self.offline_search_path.suffixes[-1] == ".json":
        self.offline_mode: str = "json"
    elif self.offline_search_path.suffixes[-1] in [".pkl", ".pickle"]:
        self.offline_mode: str = "pickle"
    else:
        raise CamplyError(
            "You must provide a `.json` or a `.pickle` / `.pkl` file name for offline searches"
        )
    if self.offline_search is True:
        logger.info(
            "Campsite search is configured to save offline: %s",
            self.offline_search_path,
        )
        self.campsites_found: Set[
            AvailableCampsite
        ] = self.load_campsites_from_file()
        self.loaded_campsites: Set[AvailableCampsite] = self.campsites_found.copy()

assemble_availabilities(matching_data, log=True, verbose=False) classmethod #

Prepare a Pandas DataFrame from Array of AvailableCampsite objects

Parameters:

Name Type Description Default
matching_data List[AvailableCampsite]

List of campsites to assemble

required
log bool

Whether to log found campsites

True
verbose bool

Used with log to enhance the amount of info logged to the console

False

Returns:

Name Type Description
availability_df DataFrame
Source code in camply/search/base_search.py
@classmethod
def assemble_availabilities(
    cls,
    matching_data: List[AvailableCampsite],
    log: bool = True,
    verbose: bool = False,
) -> DataFrame:
    """
    Prepare a Pandas DataFrame from Array of AvailableCampsite objects

    Parameters
    ----------
    matching_data: List[AvailableCampsite]
        List of campsites to assemble
    log: bool
        Whether to log found campsites
    verbose: bool
        Used with `log` to enhance the amount of info logged to the console

    Returns
    -------
    availability_df: DataFrame
    """
    availability_df = cls.campsites_to_df(campsites=matching_data)
    if log is True:
        cls._log_availabilities(availability_df=availability_df, verbose=verbose)
    return availability_df

campsites_to_df(campsites) staticmethod #

Convert Campsite Array to

Parameters:

Name Type Description Default
campsites List[AvailableCampsite]
required

Returns:

Type Description
DataFrame
Source code in camply/search/base_search.py
@staticmethod
def campsites_to_df(campsites: List[AvailableCampsite]) -> DataFrame:
    """
    Convert Campsite Array to

    Parameters
    ----------
    campsites: List[AvailableCampsite]

    Returns
    -------
    DataFrame
    """
    campsite_df = DataFrame(
        data=[campsite.dict() for campsite in campsites],
        columns=AvailableCampsite.__fields__,
    )
    return campsite_df

df_to_campsites(campsite_df) staticmethod #

Convert Campsite DataFrame to array of AvailableCampsite objects

Parameters:

Name Type Description Default
campsite_df DataFrame
required

Returns:

Type Description
List[AvailableCampsite]
Source code in camply/search/base_search.py
@staticmethod
def df_to_campsites(campsite_df: DataFrame) -> List[AvailableCampsite]:
    """
    Convert Campsite DataFrame to array of AvailableCampsite objects

    Parameters
    ----------
    campsite_df: DataFrame

    Returns
    -------
    List[AvailableCampsite]
    """
    composed_campsite_array = []
    composed_campsite_data_array = campsite_df.to_dict(orient="records")
    for campsite_record in composed_campsite_data_array:
        campsite_record["booking_date"] = campsite_record[
            "booking_date"
        ].to_pydatetime()
        campsite_record["booking_end_date"] = campsite_record[
            "booking_end_date"
        ].to_pydatetime()
        composed_campsite_array.append(AvailableCampsite(**campsite_record))
    return composed_campsite_array

get_all_campsites() abstractmethod #

Perform the Search and Return Matching Availabilities.

This method must be implemented on all sub-classes.

Returns:

Type Description
List[AvailableCampsite]
Source code in camply/search/base_search.py
@abstractmethod
def get_all_campsites(self) -> List[AvailableCampsite]:
    """
    Perform the Search and Return Matching Availabilities.

    This method must be implemented
    on all sub-classes.

    Returns
    -------
    List[AvailableCampsite]
    """

get_matching_campsites(log=True, verbose=False, continuous=False, polling_interval=None, notification_provider='silent', notify_first_try=False, search_forever=False, search_once=False) #

Perform the Search and Return Matching Availabilities

Parameters:

Name Type Description Default
log bool

Whether to log found campsites

True
verbose bool

Used with log to enhance the amount of info logged to the console

False
continuous bool

Whether to continue searching beyond just the first time

False
polling_interval Optional[int]

Used with continuous=True, the amount of time to wait between searches. Defaults to 10 if not provided, cannot be less than 5

None
notification_provider str

Used with continuous=True, Name of notification provider to use. Accepts "email", "pushover", and defaults to "silent". Also accepts a list or commma separated string of these options or even a notification provider object itself

'silent'
notify_first_try bool

Used with continuous=True, whether to send all non-silent notifications if more than 5 matching campsites are found on the first try. Defaults to false which only sends the first 5.

False
search_forever bool

Used with continuous=True, This option searches for new campsites forever, with the caveat being that it will never notify about the same campsite.

False
search_once bool

Whether to only search once (and not actually continuously)

False

Returns:

Type Description
List[AvailableCampsite]
Source code in camply/search/base_search.py
def get_matching_campsites(
    self,
    log: bool = True,
    verbose: bool = False,
    continuous: bool = False,
    polling_interval: Optional[int] = None,
    notification_provider: str = "silent",
    notify_first_try: bool = False,
    search_forever: bool = False,
    search_once: bool = False,
) -> List[AvailableCampsite]:
    """
    Perform the Search and Return Matching Availabilities

    Parameters
    ----------
    log: bool
        Whether to log found campsites
    verbose: bool
        Used with `log` to enhance the amount of info logged to the console
    continuous: bool
        Whether to continue searching beyond just the first time
    polling_interval: Optional[int]
        Used with `continuous=True`, the amount of time to wait between searches.
        Defaults to 10 if not provided, cannot be less than 5
    notification_provider: str
        Used with `continuous=True`, Name of notification provider to use. Accepts
        "email", "pushover", and defaults to "silent". Also accepts a list or commma
        separated string of these options or even a notification provider object itself
    notify_first_try: bool
        Used with `continuous=True`, whether to send all non-silent notifications if more
        than 5 matching campsites are found on the first try. Defaults to false which
        only sends the first 5.
    search_forever: bool
        Used with `continuous=True`, This option searches for new campsites forever, with
        the caveat being that it will never notify about the same campsite.
    search_once: bool
        Whether to only search once (and not actually continuously)

    Returns
    -------
    List[AvailableCampsite]
    """
    if continuous is True or search_once is True:
        try:
            self._search_campsites_continuous(
                log=log,
                verbose=verbose,
                polling_interval=polling_interval,
                notification_provider=notification_provider,
                notify_first_try=notify_first_try,
                search_forever=search_forever,
                search_once=search_once,
            )
        except Exception as e:
            self.notifier.last_gasp(error=e)
            raise e
    else:
        starting_count = len(self.campsites_found)
        matching_campsites = self._search_matching_campsites_available(
            log=log, verbose=True
        )
        self.campsites_found.update(set(matching_campsites))
        ending_count = len(self.campsites_found)
        if self.offline_search is True and ending_count > starting_count:
            self.unload_campsites_to_file()
    return list(self.campsites_found)

list_campsite_units() abstractmethod #

List Campsite Units

Returns:

Type Description
Any
Source code in camply/search/base_search.py
@abstractmethod
def list_campsite_units(self) -> Any:
    """
    List Campsite Units

    Returns
    -------
    Any
    """

load_campsites_from_file() #

Load a BaseSearch Object's campsites from a serialized Pickle file.

Returns:

Type Description
Set[AvailableCampsite]
Source code in camply/search/base_search.py
def load_campsites_from_file(self) -> Set[AvailableCampsite]:
    """
    Load a BaseSearch Object's campsites from a serialized Pickle file.

    Returns
    -------
    Set[AvailableCampsite]
    """
    if self.offline_search_path.exists():
        if self.offline_mode == "pickle":
            with open(self.offline_search_path, mode="rb") as file_stream:
                campsites: Set[AvailableCampsite] = pickle.load(
                    file=file_stream, fix_imports=True
                )
        elif self.offline_mode == "json":
            with open(self.offline_search_path, mode="r") as file_stream:
                campsites_dicts: List[Dict[str, Any]] = json.load(file_stream)
            campsites: Set[AvailableCampsite] = {
                AvailableCampsite(**json_dict) for json_dict in campsites_dicts
            }
        if len(campsites) > 0:
            logger.info(
                "%s campsites loaded from file: %s",
                len(campsites),
                self.offline_search_path,
            )
    else:
        campsites = set()
    return campsites

log_listed_campsites(campsites, facilities) classmethod #

Print the campsites to the console

Parameters:

Name Type Description Default
facilities Sequence[CampgroundFacility]
required
campsites Sequence[ListedCampsite]
required

Returns:

Type Description
None
Source code in camply/search/base_search.py
@classmethod
def log_listed_campsites(
    cls,
    campsites: Sequence[ListedCampsite],
    facilities: Sequence[CampgroundFacility],
) -> None:
    """
    Print the campsites to the console

    Parameters
    ----------
    facilities: List[CampgroundFacility]
    campsites: List[ListedCampsite]

    Returns
    -------
    None
    """
    logger.info("Found %s campgrounds to search", len(facilities))
    logger.info("Found %s Campsites", len(campsites))
    for facility in sorted(facilities, key=lambda x: x.facility_id):
        logger.info("πŸ•  %s - (#%s)", facility.facility_name, facility.facility_id)
        for item in campsites:
            if item.facility_id == facility.facility_id:
                logger.info("    ⛺️ %s - (#%s)", item.name, item.id)

unload_campsites_to_file() #

Unload a BaseSearch Object's campsites to a serialized Pickle file.

Returns:

Type Description
Path
Source code in camply/search/base_search.py
def unload_campsites_to_file(self) -> pathlib.Path:
    """
    Unload a BaseSearch Object's campsites to a serialized Pickle file.

    Returns
    -------
    pathlib.Path
    """
    if self.offline_mode == "pickle":
        with open(self.offline_search_path, mode="wb") as file_stream:
            pickle.dump(
                obj=self.campsites_found,
                file=file_stream,
                protocol=4,
                fix_imports=True,
            )
            file_stream.seek(0)
    elif self.offline_mode == "json":
        with open(self.offline_search_path, mode="w") as file_stream:
            json.dump(
                obj=self.campsites_found,
                fp=file_stream,
                sort_keys=True,
                default=pydantic_encoder,
                indent=4,
            )
            file_stream.seek(0)
    logger.debug(
        "%s campsites saved to file: %s",
        len(self.campsites_found),
        self.offline_search_path,
    )
    return self.offline_search_path