-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathprice_tracking.py
More file actions
72 lines (55 loc) · 2.23 KB
/
Copy pathprice_tracking.py
File metadata and controls
72 lines (55 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
#!/usr/bin/env python3
"""Price tracking over time example.
This example demonstrates how to track flight prices over multiple days
to analyze price trends and find the best deals.
"""
import time
from fli.models import Airport, DateSearchFilters, FlightSegment, PassengerInfo
from fli.search import SearchDates
def track_prices(days=7):
"""Track flight prices over a specified number of days."""
from datetime import datetime, timedelta
base_date = datetime.now() + timedelta(days=30)
travel_date = base_date.strftime("%Y-%m-%d")
from_date = base_date.strftime("%Y-%m-%d")
to_date = (base_date + timedelta(days=7)).strftime("%Y-%m-%d")
filters = DateSearchFilters(
passenger_info=PassengerInfo(adults=1),
flight_segments=[
FlightSegment(
departure_airport=[[Airport.JFK, 0]],
arrival_airport=[[Airport.LAX, 0]],
travel_date=travel_date,
)
],
from_date=from_date,
to_date=to_date,
)
search = SearchDates()
price_history = {}
for day in range(days):
print(f"Day {day + 1}: Searching for prices...")
results = search.search(filters)
# Store prices
for result in results:
date_str = result.date[0].strftime("%Y-%m-%d")
if date_str not in price_history:
price_history[date_str] = []
price_history[date_str].append(result.price)
# Wait for next check (in a real implementation, you'd wait 24 hours)
if day < days - 1: # Don't wait on the last iteration
print("Waiting for next check...")
time.sleep(1) # In real usage: time.sleep(86400) for 24 hours
return price_history
def main():
"""Main function to demonstrate price tracking."""
print("Starting price tracking demo...")
price_history = track_prices(3) # Track for 3 iterations as demo
print("\n=== Price History Summary ===")
for date, prices in price_history.items():
min_price = min(prices)
max_price = max(prices)
avg_price = sum(prices) / len(prices)
print(f"{date}: Min: ${min_price:.2f}, Max: ${max_price:.2f}, Avg: ${avg_price:.2f}")
if __name__ == "__main__":
main()