getd=ed dates

This commit is contained in:
2025-06-02 23:45:18 +03:00
parent a4c5e406c2
commit bfe81692b6

View File

@@ -1,49 +1,37 @@
import requests import requests
def get_positions(server_url, token, device_id=None, from_time=None, to_time=None): def get_positions(server_url, token, device_ids, from_time, to_time):
""" """
Fetch position information from the Traccar server. Fetch position information from the Traccar server.
Args: Args:
server_url (str): The URL of the Traccar server. server_url (str): The URL of the Traccar server.
token (str): The authentication token. token (str): The authentication token.
device_id (int, optional): The ID of the device. If not provided, fetches positions for all devices. device_ids (list of int): List of device IDs.
from_time (str, optional): The start time in ISO 8601 format (e.g., '2024-04-05T00:00:00Z'). from_time (str): The start time in ISO 8601 format.
to_time (str, optional): The end time in ISO 8601 format (e.g., '2024-04-05T23:59:59Z'). to_time (str): The end time in ISO 8601 format.
Returns: Returns:
list: The position information. list: The position information.
""" """
# Ensure the server_url has a valid scheme if not device_ids or not from_time or not to_time:
if not server_url.startswith("http://") and not server_url.startswith("https://"): print("deviceId, from, and to are required parameters.")
server_url = f"https://{server_url}" # Default to https:// if no scheme is provided return None
# Set the Authorization header with the token
headers = {"Authorization": f"Bearer {token}"} headers = {"Authorization": f"Bearer {token}"}
# API endpoint for fetching positions
url = f"{server_url}/positions" url = f"{server_url}/positions"
payload = {
# Request payload "deviceId": device_ids, # Send as list
payload = {} "from": from_time,
if device_id: "to": to_time
payload["deviceId"] = device_id }
if from_time and to_time:
payload["from"] = from_time
payload["to"] = to_time
try: try:
# Log the payload for debugging
print(f"Request Payload: {payload}") print(f"Request Payload: {payload}")
# Make the API request
response = requests.get(url, params=payload, headers=headers) response = requests.get(url, params=payload, headers=headers)
# Log the response status and content for debugging
print(f"Response Status Code: {response.status_code}") print(f"Response Status Code: {response.status_code}")
print(f"Response Content: {response.text}") print(f"Response Content: {response.text}")
# Check if the response was successful
if response.status_code == 200: if response.status_code == 200:
positions = response.json() positions = response.json()
print(f"Retrieved {len(positions)} positions:") print(f"Retrieved {len(positions)} positions:")
@@ -67,13 +55,18 @@ if __name__ == "__main__":
server_url = "https://gps.moto-adv.com/api" server_url = "https://gps.moto-adv.com/api"
token = "RjBEAiB69jflCQJ9HQwGyFdAHF8J5JPMwIIclglbWiKMWeX6PwIgdBWc-YllE_-RwoQi7zX25WxQJqQX_OTIKOHk9a3mmBN7InUiOjEsImUiOiIyMDI1LTA2LTA4VDIxOjAwOjAwLjAwMCswMDowMCJ9" token = "RjBEAiB69jflCQJ9HQwGyFdAHF8J5JPMwIIclglbWiKMWeX6PwIgdBWc-YllE_-RwoQi7zX25WxQJqQX_OTIKOHk9a3mmBN7InUiOjEsImUiOiIyMDI1LTA2LTA4VDIxOjAwOjAwLjAwMCswMDowMCJ9"
# Optional: Enter device ID and date range device_id = input("Enter the device ID (required): ").strip()
device_id = input("Enter the device ID (leave blank for all devices): ").strip() if not device_id:
device_id = int(device_id) if device_id else None print("Device ID is required.")
from_time = input("Enter the start time (ISO 8601, e.g., 2024-04-05T00:00:00Z, leave blank for none): ").strip() exit(1)
to_time = input("Enter the end time (ISO 8601, e.g., 2024-04-05T23:59:59Z, leave blank for none): ").strip() device_ids = [int(device_id)]
positions = get_positions(server_url, token, device_id, from_time, to_time) # Set fixed time interval
from_time = "2024-06-01T12:00:00Z"
to_time = "2024-06-01T23:00:00Z"
print(f"Using time interval: {from_time} to {to_time}")
positions = get_positions(server_url, token, device_ids, from_time, to_time)
if positions: if positions:
print("Position data retrieved successfully!") print("Position data retrieved successfully!")
else: else: