timfocus commited on
Commit
e6419df
·
verified ·
1 Parent(s): ebfb1fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import FileResponse
3
+ import csv
4
+ import random
5
+ import datetime
6
+
7
+ app = FastAPI()
8
+
9
+ # Sample data for randomization
10
+ shipper_names = ["Global Express", "Fast Ship Logistics", "Secure Courier"]
11
+ cities_states = [("New York", "NY"), ("Los Angeles", "CA"), ("Chicago", "IL"), ("Houston", "TX"), ("Miami", "FL")]
12
+ shipping_methods = ["FedEx", "UPS", "DHL", "USPS"]
13
+ weight_range = (0.5, 50) # Min and max weight in kg
14
+ csv_filename = "realistic_shipping_data.csv"
15
+
16
+ # Function to generate a random phone number
17
+ def random_phone():
18
+ return f"+1-{random.randint(200, 999)}-{random.randint(100, 999)}-{random.randint(1000, 9999)}"
19
+
20
+ # Function to generate a random tracking number
21
+ def generate_tracking(carrier):
22
+ prefix = {"FedEx": "FDX", "UPS": "UPS", "DHL": "DHL", "USPS": "USPS"}.get(carrier, "TRK")
23
+ return f"{prefix}-{random.randint(100000, 999999)}"
24
+
25
+ # Function to generate the shipping CSV file
26
+ def generate_csv():
27
+ shipping_data = []
28
+ for order_id in range(1001, 1011): # Generating 10 orders
29
+ order_date = datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 30))
30
+ delivery_date = order_date + datetime.timedelta(days=random.randint(2, 10))
31
+
32
+ shipper = random.choice(shipper_names)
33
+ shipper_city, shipper_state = random.choice(cities_states)
34
+ receiver_city, receiver_state = random.choice(cities_states)
35
+
36
+ shipping_method = random.choice(shipping_methods)
37
+ weight = round(random.uniform(*weight_range), 2)
38
+ tracking_number = generate_tracking(shipping_method)
39
+
40
+ shipping_data.append({
41
+ "Order ID": order_id,
42
+ "Order Date": order_date.strftime("%Y-%m-%d %H:%M:%S"),
43
+ "Shipper Name": shipper,
44
+ "Shipper Address": f"{random.randint(100, 999)} {shipper_city} St, {shipper_city}, {shipper_state}",
45
+ "Shipper Phone": random_phone(),
46
+ "Receiver Name": f"Customer {order_id}",
47
+ "Receiver Address": f"{random.randint(100, 999)} {receiver_city} Ave, {receiver_city}, {receiver_state}",
48
+ "Receiver Phone": random_phone(),
49
+ "Package Weight (kg)": weight,
50
+ "Shipping Method": shipping_method,
51
+ "Tracking Number": tracking_number,
52
+ "Estimated Delivery Date": delivery_date.strftime("%Y-%m-%d")
53
+ })
54
+
55
+ # Define CSV column headers
56
+ csv_columns = ["Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone", "Receiver Name",
57
+ "Receiver Address", "Receiver Phone", "Package Weight (kg)", "Shipping Method", "Tracking Number",
58
+ "Estimated Delivery Date"]
59
+
60
+ # Write data to CSV file
61
+ with open(csv_filename, mode="w", newline="") as file:
62
+ writer = csv.DictWriter(file, fieldnames=csv_columns)
63
+ writer.writeheader()
64
+ writer.writerows(shipping_data)
65
+
66
+ # API Route to Generate and Download CSV
67
+ @app.get("/generate-csv")
68
+ def download_csv():
69
+ generate_csv()
70
+ return FileResponse(csv_filename, media_type="text/csv", filename=csv_filename)
71
+
72
+ @app.get("/")
73
+ def root():
74
+ return {"message": "Welcome to the Shipping CSV Generator API! Use /generate-csv to download the file."}