Building a Crypto Price Feed for the Web
About
I built a smart tool that helps anyone interested in cryptocurrency track the latest prices and data without the hassle of checking daily.
Imagine you're really excited about cryptocurrency, but keeping track of all the price changes, market caps, and volumes can feel like trying to juggle while riding a unicycle. It's a bit tricky, right? That's where this code comes in—it helps automate the process of gathering and updating cryptocurrency data, making it easier for you to keep your finger on the pulse of the crypto market.
So, how does this code solve the problem of managing crypto data? It connects to a crypto data API (like CoinGecko) to fetch the latest information, stores it in a database, and updates it regularly. This way, you don’t need to manually check prices every day. Instead, the code does the heavy lifting for you!
Key Parts of the Code
Let’s break down a few interesting parts of this code that make it so effective:
# Helper function: Get crypto data if there's none def get_crypto_data(coin_name, coin_id): # ... (code omitted for brevity) coin_in_db = db.session.query(CryptoMarketData).filter(CryptoMarketData.name == coin_name) # Clears previous data before adding new one if coin_in_db.all(): coin_in_db.delete() db.session.commit()
This function, get_crypto_data, is like your best friend who cleans your room before bringing in new stuff. It checks if there’s any existing data for a cryptocurrency. If it finds any, it clears it out before bringing in the latest data. This ensures that you always have the most accurate and up-to-date information!
# Helper function: checks if timestamp is in a date list def ts_in_date_list(ts, dates_: list): ts_date = dt.datetime.fromtimestamp(ts // 1000, dt.timezone.utc).date() return ts_date in dates_
Next up is the ts_in_date_list function. It’s like having a calendar that tells you if a specific date is on your schedule. It takes a timestamp and checks if that date is part of the dates you’re interested in. This is super helpful when the code needs to figure out if it has the right data for the days it wants to update.
# Helper function: Update crypto data rows def update_crypto_data(): # ... (code omitted for brevity) missing_dates = [date_ for date_ in date_range_required if date_ not in date_range_in_db] if missing_dates: # Get historical data in one API call instead of multiple params = {"vs_currency": "usd", "days": history_range, "interval": "daily"} # ... (code continues)
Finally, we have the update_crypto_data function. This is where the magic happens! It checks if there are any dates missing from the database. If it finds some, it fetches all the historical data in one go rather than in bits and pieces. It’s like going to the grocery store and buying everything you need for a week in one trip instead of going every day!
Wrapping It Up
In summary, this code is a handy tool that automates the process of tracking cryptocurrency data, making life a lot easier for anyone interested in the crypto market. By regularly fetching and updating data, it ensures that you’re always informed without the hassle of manual checking. Plus, it’s a great example of how coding can simplify complex tasks and help us stay on top of the latest trends. How cool is that?
You can try it out here:
Try it out