Skip to content

Instantly share code, notes, and snippets.

@perk11
Created March 31, 2024 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perk11/7b54b613ec9d726575157f8670ed5fee to your computer and use it in GitHub Desktop.
Save perk11/7b54b613ec9d726575157f8670ed5fee to your computer and use it in GitHub Desktop.
Telegram top authors by message per day
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 output.py authors.json
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.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment