Skip to content

Instantly share code, notes, and snippets.

@dfeldman
Created March 4, 2024 22:35
Show Gist options
  • Save dfeldman/daffac4815e2c5b2cdb89718175d1430 to your computer and use it in GitHub Desktop.
Save dfeldman/daffac4815e2c5b2cdb89718175d1430 to your computer and use it in GitHub Desktop.
Process chatgpt conversations.json into the length of time of conversations
# GOAL:
# Determine how long (in seconds) you've spent talking to ChatGPT.
# This goes through each message sent in order. If there are two messages closer than 5 minutes apart,
# it adds their time delta to the total. If the gap is over 5 minutes, it assumes this is idle time
# and ignores the time delta entirely.
# Outputs a CSV file of total time per conversation, and prints the grand total of all conversations.
# All output is in seconds.
import json
import csv
# Load JSON data
with open('conversations.json', 'r') as file:
data = json.load(file)
# Initialize a variable to hold the sum of all conversation times
total_time_of_all_conversations = 0
# Prepare CSV output
with open('output_times.csv', 'w', newline='') as csvfile:
fieldnames = ['first_message_time', 'last_message_time', 'total_time_seconds']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for item in data:
# Extract all create_time values, ignoring None
times = [message['message']['create_time'] for key, message in item['mapping'].items() if message['message'] and message['message']['create_time'] is not None]
if not times:
continue # Skip to the next conversation if no messages
# Sort times to find the first and last message times
sorted_times = sorted(times)
first_time = sorted_times[0]
last_time = sorted_times[-1]
# Initialize total active time for the current conversation
total_active_time = 0
# Iterate through sorted times and calculate active time
for i in range(len(sorted_times) - 1):
current_time = sorted_times[i]
next_time = sorted_times[i + 1]
time_diff = next_time - current_time
# If two messages are <= 5 minutes (300 seconds) apart, add their time difference
if time_diff <= 300:
total_active_time += time_diff
# For conversations with only one message, consider active time as 1 second
if len(sorted_times) == 1:
total_active_time = 1
# Accumulate total time of all conversations
total_time_of_all_conversations += total_active_time
# Write to CSV
writer.writerow({
'first_message_time': first_time,
'last_message_time': last_time,
'total_time_seconds': total_active_time
})
# Print the total time of all conversations in seconds
print(f'Total time of all conversations: {total_time_of_all_conversations} seconds')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment