🚀 Automating Crypto Data Collection with Python, Email, and Cron

kasumbi philkasumbi phil
4 min read

Introduction

Today, I worked on an exciting Python project: automating the collection of cryptocurrency data from CoinGecko, saving it in a CSV file, and emailing it to myself automatically. It started as a simple script but ended up teaching me a lot about APIs, file handling, email automation, and system schedulers like cron.

Here’s what I did, the challenges I encountered, and how I solved them.


🛠️ What I Built

The project involves a Python script that:

  1. Fetches real-time crypto market data using the CoinGecko API

  2. Saves the data into a CSV file

  3. Sends that file to my email (using Gmail SMTP)

  4. Automatically repeats this process every 2 hours using crontab


🧩 Technologies Used

  • Python 3

  • requests for API calls

  • csv for file writing

  • smtplib and email for sending emails

  • cron for scheduling the task

  • Gmail (App Passwords) for email authentication

  • Git & GitHub for version control


✅ Step-by-Step Summary

1. Fetching Crypto Data

I used the CoinGecko API to fetch the top 100 cryptocurrencies and their details like name, symbol, price, market cap, 24-hour change, etc.

url = "https://api.coingecko.com/api/v3/coins/markets"
params = {'vs_currency': 'usd', 'order': 'market_cap_desc', 'per_page': 100}
response = requests.get(url, params=params)

2. Writing to CSV

I created a CSV file (crypto_data.csv) with a timestamp and wrote the data in rows.

with open("crypto_data.csv", "w") as file:
    writer = csv.writer(file)
    writer.writerow([...])  # column headers
    writer.writerow([...])  # crypto data rows

3. Sending Email

Using smtplib, I attached the CSV file and emailed it to myself.

with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
    smtp.send_message(msg)

4. Automating with Cron

I used crontab to automate the script every 2 hours:

crontab -e

Then added:

0 */20 * * * /usr/bin/python3 /home/philip-kasumbi/Desktop/crypto_data/coins_info.py

This runs the script every 20 hours.

⚠️ Challenges I Faced (And How I Solved Them)

❌ 1. File Being Rewritten Every Run

At first, the script created a new CSV file every 30 seconds (for testing). That wasn’t practical.

Solution: I used a constant file name (crypto_data.csv) and made sure the file gets overwritten each time to avoid duplicates.


❌ 2. Repeating Headers and Bitcoin Showing as Header

Sometimes, the header row got printed repeatedly, or Bitcoin’s name appeared as the header.

Solution: I corrected the logic to write headers only once and format the file write process properly.


❌ 3. Email Not Sending

I initially had issues sending the email — it kept failing or being blocked.

Solution: I switched to a Gmail account, enabled App Passwords, and used SSL over port 465 for smtplib.


❌ 4. Crontab Confusion

Understanding how crontab works was a bit of a maze. I saw errors like:

Warning: Step size 24 higher than possible maximum of 23

Solution: I learned the format of a crontab schedule:
minute hour day month weekday command

For example:

  • */5 * * * * — every 5 minutes

  • 0 */2 * * * — every 2 hours

I updated my crontab safely using crontab -e, then saved it with CTRL+X, then Y, and Enter.


🧠 Lessons Learned

  • Using external APIs is easier than I thought with requests

  • Automating emails requires attention to email security (App Passwords)

  • cron is powerful for scheduling tasks, but requires understanding time expressions

  • A system must be on and connected to the internet for cron to work

  • After a shutdown, cron resumes when the system powers on — no manual restart needed!


✅ Bonus: GitHub Version

I pushed this project to GitHub to make it public. The README includes setup instructions and cron tips.

📂 GitHub Repository (https://github.com/philkasumbi/crypto-tracker.git)


Conclusion

This project helped me connect various Python skills with real-world automation. I now have a working crypto data tracker that runs on its own and keeps me updated via email. I'm excited to expand this further — maybe adding data visualization or Telegram notifications.

If you’re learning Python and want a hands-on project — try this out!


Thanks for reading.

0
Subscribe to my newsletter

Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

kasumbi phil
kasumbi phil