Created
March 31, 2024 19:44
-
-
Save perk11/7b54b613ec9d726575157f8670ed5fee to your computer and use it in GitHub Desktop.
Revisions
-
perk11 revised this gist
Mar 31, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,4 +11,4 @@ jq ' firstDate: (map(.date) | sort | .[0]) }) ' export.json >authors.json python3 output.py authors.json -
perk11 created this gist
Mar 31, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ jq ' [ .messages[] | {from, date} | select(.from != null) ] | group_by(.from) | map({ from: .[0].from, count: length, firstDate: (map(.date) | sort | .[0]) }) ' export.json >authors.json python3 authors.json This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ import json import csv from datetime import datetime, timedelta # Load the JSON output from jq with open('authors.json', 'r') as file: authors = json.load(file) # Current date current_date = datetime.now().date() # Calculate days span and average messages per day for author in authors: first_date = datetime.strptime(author['firstDate'], '%Y-%m-%dT%H:%M:%S').date() days_span = (current_date - first_date).days + 1 # Adding 1 to include the first day author['avgMessagesPerDay'] = author['count'] / days_span # Sort authors by avgMessagesPerDay authors_sorted = sorted(authors, key=lambda x: x['avgMessagesPerDay'], reverse=True) # Define CSV file name csv_file_name = 'authors_avg_messages_per_day.csv' # Define CSV headers headers = ['from', 'avgMessagesPerDay'] # Write data to CSV with open(csv_file_name, mode='w', newline='') as file: writer = csv.DictWriter(file, fieldnames=headers) # Write the header writer.writeheader() # Write each author's data for author in authors_sorted: # Ensure to round the avgMessagesPerDay or format it as desired author['avgMessagesPerDay'] = round(author['avgMessagesPerDay'], 2) writer.writerow({'from': author['from'], 'avgMessagesPerDay': author['avgMessagesPerDay']}) print(f"CSV file '{csv_file_name}' created successfully.")