Skip to content

Instantly share code, notes, and snippets.

@seqis
Created September 2, 2024 05:44
Show Gist options
  • Select an option

  • Save seqis/34b7c37c577cf232d0c98de98a619d9d to your computer and use it in GitHub Desktop.

Select an option

Save seqis/34b7c37c577cf232d0c98de98a619d9d to your computer and use it in GitHub Desktop.
This script will track your daily writing progress by reading the total word count from your novel or document's Markdown file (right now it's coded to be NaNoWriMo.md but you can change that) and logs the cumulative and net-new words written each day in a log file (NaNoWriMo_Log.md).
import os
import datetime
import matplotlib.pyplot as plt
# Configuration: User needs to set these variables
vault_path = '/path/to/your/obsidian/vault' # Replace with the actual path to your vault
novel_file = 'NaNoWriMo.md' # Replace with the actual file name if different
log_file = 'NaNoWriMo_Log.md' # This file will store the word counts for each day
target_words = 50000 # NaNoWriMo target word count
start_date = datetime.date(2024, 11, 1)
end_date = datetime.date(2024, 11, 30)
# Step 1: Calculate the total word count in the novel file
novel_path = os.path.join(vault_path, novel_file)
with open(novel_path, 'r') as file:
content = file.read()
total_words = len(content.split())
# Step 2: Read or create the log file to track daily counts
log_path = os.path.join(vault_path, log_file)
log_data = {}
# Initialize previous total to 0 (if no previous data exists)
previous_total_words = 0
# If the log file exists, read the existing log data
if os.path.exists(log_path):
with open(log_path, 'r') as file:
for line in file.read().split('------------------------------------\n'):
if line.strip():
date_str = line.split('\n')[0].split(': ')[1]
count = int(line.split('\n')[1].split(': ')[1])
net_new = int(line.split('\n')[2].split(': ')[1])
log_data[date_str] = (count, net_new)
if log_data:
last_entry = list(log_data.values())[-1]
previous_total_words = last_entry[0] # Get the last recorded total word count
# Step 3: Calculate the net-new words written today
today = datetime.date.today()
today_str = today.strftime('%Y-%m-%d')
# Calculate the net-new words written today
net_new_words = total_words - previous_total_words
# Update the log data with today's total and net-new word count
log_data[today_str] = (total_words, net_new_words)
# Write the updated log data back to the log file
with open(log_path, 'w') as file:
for date_str, (count, net_new) in sorted(log_data.items()):
file.write(f"Date: {date_str}\n")
file.write(f"Total Words Written: {count}\n")
file.write(f"Net-New Words Written Today: {net_new}\n")
file.write(f"------------------------------------\n")
# Step 4: Generate a bar graph of the progress
dates = []
word_counts = []
net_new_word_counts = []
# Populate the lists with data from the log
for date_str, (count, net_new) in sorted(log_data.items()):
dates.append(datetime.datetime.strptime(date_str, '%Y-%m-%d').date())
word_counts.append(count)
net_new_word_counts.append(net_new)
plt.figure(figsize=(10, 6))
plt.bar(dates, net_new_word_counts, color='skyblue', label='Net-New Words')
plt.xlabel('Date')
plt.ylabel('Net-New Words Written')
plt.title('NaNoWriMo Daily Progress')
plt.axhline(y=1667, color='r', linestyle='--', label='Daily Target (1667 words)')
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
# Save the bar graph as a single, updated image file in the vault
output_image_path = os.path.join(vault_path, 'NaNoWriMo_Progress.png')
plt.savefig(output_image_path)
plt.show()
# Step 5: Print a summary of today's progress
words_needed = max(0, target_words - total_words)
days_remaining = (end_date - today).days + 1
words_per_day_needed = words_needed // days_remaining if days_remaining > 0 else 0
print(f"Total words written: {total_words}")
print(f"Net-new words written today: {net_new_words}")
print(f"Words remaining: {words_needed}")
print(f"Words per day needed to finish on time: {words_per_day_needed}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment