Created
September 2, 2023 07:24
-
-
Save skalahonza/4e2c5e4d9a9a853b5d941a74c8a7ac96 to your computer and use it in GitHub Desktop.
Get all overlaping clockify time entries
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 requests | |
from datetime import datetime | |
import pytz | |
# Replace with your Clockify API key, workspace ID and user ID | |
API_KEY = '' | |
WORKSPACE_ID = '' | |
USER_ID = '' | |
# Set the time zone | |
TIMEZONE = pytz.timezone('Europe/Prague') | |
def print_entry(entry): | |
start = datetime.strptime(entry['timeInterval']['start'], '%Y-%m-%dT%H:%M:%SZ') | |
end = datetime.strptime(entry['timeInterval']['end'], '%Y-%m-%dT%H:%M:%SZ') | |
# Set their time zone to UTC | |
utc_timezone = pytz.timezone('UTC') | |
start = utc_timezone.localize(start) | |
end = utc_timezone.localize(end) | |
# Convert to Prague time zone | |
start = start.astimezone(TIMEZONE) | |
end = end.astimezone(TIMEZONE) | |
# format to YYYY-MM-DD HH:MM:SS | |
start = start.strftime('%Y-%m-%d %H:%M:%S') | |
end = end.strftime('%Y-%m-%d %H:%M:%S') | |
print(f'{start} - {end} ({entry["description"]})') | |
# Define the API endpoint and parameters | |
base_url = f'https://api.clockify.me/api/v1/workspaces/{WORKSPACE_ID}/user/{USER_ID}' | |
time_entries_url = f'{base_url}/time-entries' | |
headers = { | |
'X-Api-Key': API_KEY | |
} | |
# Define the start and end dates for August 2023 in Prague time | |
start_date = TIMEZONE.localize(datetime(2023, 8, 1, 0, 0, 0)).isoformat() | |
end_date = TIMEZONE.localize(datetime(2023, 8, 31, 23, 59, 59)).isoformat() | |
# Define query parameters for the API request | |
params = { | |
'start': start_date, | |
'end': end_date | |
} | |
# Send the API request to get time entries | |
response = requests.get(time_entries_url, headers=headers, params=params) | |
if response.status_code == 200: | |
time_entries = response.json() | |
intervals = [] | |
for entry in time_entries: | |
start = datetime.strptime(entry['timeInterval']['start'], '%Y-%m-%dT%H:%M:%SZ') | |
end = datetime.strptime(entry['timeInterval']['end'], '%Y-%m-%dT%H:%M:%SZ') | |
intervals.append((start, end)) | |
# Find overlapping intervals | |
for i, entry in enumerate(time_entries): | |
x = datetime.strptime(entry['timeInterval']['start'], '%Y-%m-%dT%H:%M:%SZ') | |
y = datetime.strptime(entry['timeInterval']['end'], '%Y-%m-%dT%H:%M:%SZ') | |
collisions = [] | |
for j, (a, b) in enumerate(intervals): | |
if i != j and (a < y and x < b): | |
collisions.append(j) | |
if len(collisions) > 0: | |
print('Overlapping intervals found') | |
print_entry(entry) | |
for j in collisions: | |
print_entry(time_entries[j]) | |
else: | |
print('Error:', response.status_code) | |
print(response.text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment