Skip to content

Instantly share code, notes, and snippets.

@perk11
Created March 31, 2024 19:44
Show Gist options
  • Save perk11/7b54b613ec9d726575157f8670ed5fee to your computer and use it in GitHub Desktop.
Save perk11/7b54b613ec9d726575157f8670ed5fee to your computer and use it in GitHub Desktop.

Revisions

  1. perk11 revised this gist Mar 31, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jq.sh
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,4 @@ jq '
    firstDate: (map(.date) | sort | .[0])
    })
    ' export.json >authors.json
    python3 authors.json
    python3 output.py authors.json
  2. perk11 created this gist Mar 31, 2024.
    14 changes: 14 additions & 0 deletions jq.sh
    Original 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
    40 changes: 40 additions & 0 deletions output.py
    Original 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.")