Last active
December 17, 2023 21:06
-
-
Save yangchenyun/676361cd78309dd3b2c2ae99b8517075 to your computer and use it in GitHub Desktop.
mileHQ Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import json | |
from datetime import datetime | |
from datetime import timedelta | |
import requests | |
import googlemaps | |
LOCATIONS = { | |
"home": { "latitude": 37.4028316, "longitude": -121.9196804 }, | |
"office": { "latitude": 37.3880879, "longitude": -121.9709286 }, | |
"sor": { "latitude": 37.257219850901876, "longitude": -121.87728876469822 } | |
} | |
start_time = datetime(2023, 12, 7, 8, 30) | |
end_time = datetime(2023, 12, 7, 8, 40) | |
home = LOCATIONS['home'] | |
office = LOCATIONS['office'] | |
sor = LOCATIONS['sor'] | |
## Google API | |
GOOGLE_API_KEY = "AIzaSyBXZ36IeO7XIo5SI2tiJLmbmvW6qKcR4Wc" | |
def calculate_distance(start: dict, end: dict) -> float: | |
gmaps = googlemaps.Client(key=GOOGLE_API_KEY) | |
# Request directions via public transit | |
now = datetime.now() | |
directions_result = gmaps.directions(start, | |
end, | |
departure_time=now) | |
# Get distance in miles from the directions result | |
distance = directions_result[0]['legs'][0]['distance']['text'] | |
distance = float(distance.replace(' mi', '')) | |
return distance | |
def create_driving_record(s_t: datetime, e_t: datetime, s_l: dict, e_l: dict) -> dict: | |
distance = calculate_distance(s_l, e_l) | |
data = { | |
"operationName": "AddDrive", | |
"variables": { | |
"data": { | |
"category": 2, | |
"purpose": "", | |
"distance": distance, # TODO, to fetch distance from Google Map | |
"startLocation": s_l, | |
"endLocation": e_l, | |
"notes": "", | |
"parkingFees": 0, | |
"tollFees": 0, | |
"startDate": s_t.strftime("%Y-%m-%dT%H:%M:%S.000Z"), | |
"endDate": e_t.strftime("%Y-%m-%dT%H:%M:%S.000Z"), | |
"timeZoneDiffInHours": 0, | |
"vehicleId": "kBoKipOcEe6mS-q5BttYbw", | |
"vehicleType": "automobile", | |
} | |
}, | |
"query": "mutation AddDrive($data: AddDriveInput) {\n addDrive(data: $data) {\n id\n purpose {\n id\n category\n __typename\n }\n distance\n googleDistance\n value\n parkingFees\n tollFees\n notes\n potentialBusinessValue {\n default\n __typename\n }\n potentialPersonalValue {\n charity\n medical\n moving\n __typename\n }\n startLocation {\n latitude\n longitude\n name\n displayName\n isNamed\n namedLocationDetails {\n id\n isActive\n __typename\n }\n __typename\n }\n startDate\n endLocation {\n latitude\n longitude\n name\n displayName\n isNamed\n namedLocationDetails {\n id\n isActive\n __typename\n }\n __typename\n }\n endDate\n isAutoClassified\n isReported\n vehicleId\n routeId\n routeShouldAutoClassify\n routeClassificationsToPrompt\n routeClassificationStreak\n routeUnclassifiedDriveCount\n map {\n startEndUrl\n startUrl\n endUrl\n __typename\n }\n __typename\n }\n}", | |
} | |
return data | |
def send_driving_record(record): | |
data_string = json.dumps(record) | |
url = "https://gql-gateway.mileiq.com/" | |
headers = { | |
"authority": "gql-gateway.mileiq.com", | |
"accept": "*/*", | |
"accept-language": "en-US,en;q=0.9", | |
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJOMDY3UkpPY0VlNllZLXE1QnR0WWJ3IiwidXNlcm5hbWUiOiJzdGV2ZS55YW5nQHVuaXR4bGFicy5jb20iLCJpc19wcmVtaXVtIjoxLCJkaXN0aW5jdF9pZCI6IjE4YzNiM2QxYzRlZjg5LTA0YzdkYjBkYjNhNWY3LTE2NTI1NjM0LTM4NDAwMC0xOGMzYjNkMWM0ZjI1YTgiLCJ2ZXJzaW9uIjoxLCJzdWJzY3JpcHRpb25fdHlwZSI6MTEsImNvdW50cnkiOiJ1cyIsIm9yZ19pZCI6MTAwMDAzNDA1LCJ0ZWFtX2lkIjo0NzE4LCJ0ZWFtX2FkbWluIjpmYWxzZSwidGVhbV9kcml2ZXIiOnRydWUsInRlYW1fbmFtZSI6IlVuaXRYIiwiZXhwIjoxNzAyOTI4MTkwfQ.mdhEi7TVvhdmIP-BN3qJeubz3DCZP4HLOqd1GB4QL0Y", | |
"content-type": "application/json", | |
"dnt": "1", | |
"origin": "https://dashboard.mileiq.com", | |
"referer": "https://dashboard.mileiq.com/", | |
"sec-ch-ua": '"Not_A Brand";v="8", "Chromium";v="120"', | |
"sec-ch-ua-mobile": "?0", | |
"sec-ch-ua-platform": '"macOS"', | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-site", | |
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", | |
} | |
response = requests.request("POST", url, headers=headers, data=data_string) | |
return response.text | |
records = [] | |
current_date = datetime(2023, 12, 17, 0, 00) # start date for iteration here | |
today = datetime.now() | |
while current_date.month == 12 and current_date <= today: | |
if current_date.weekday() in [0, 1, 2, 3, 4]: # Week days | |
# to office | |
s_t = datetime(current_date.year, current_date.month, current_date.day, 8, 30) | |
e_t = datetime(current_date.year, current_date.month, current_date.day, 8, 40) | |
records.append(create_driving_record(s_t, e_t, home, office)) | |
# back home | |
s_t = datetime(current_date.year, current_date.month, current_date.day, 18, 30) | |
e_t = datetime(current_date.year, current_date.month, current_date.day, 18, 45) | |
records.append(create_driving_record(s_t, e_t, office, home)) | |
elif current_date.weekday() in [6]: # Sundays | |
# to office | |
s_t = datetime(current_date.year, current_date.month, current_date.day, 12, 30) | |
e_t = datetime(current_date.year, current_date.month, current_date.day, 12, 40) | |
records.append(create_driving_record(s_t, e_t, home, office)) | |
# back home | |
s_t = datetime(current_date.year, current_date.month, current_date.day, 15, 30) | |
e_t = datetime(current_date.year, current_date.month, current_date.day, 16, 00) | |
records.append(create_driving_record(s_t, e_t, office, sor)) | |
current_date += timedelta(days=1) | |
# %% | |
for r in records: | |
print(r["variables"]["data"]["startDate"], r["variables"]["data"]["endDate"]) | |
send_driving_record(r) | |
# %% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment