Skip to content

Conversation

@sandeepsalwan1
Copy link

@sandeepsalwan1 sandeepsalwan1 commented May 7, 2025

Category: Data Extraction
Description: Opens CoinMarketCap “Trending”, converts table to CSV, plots top‑7 %gainers with matplotlib.
maindemocrypto

Script

`#!/usr/bin/env python3
"""
Crypto Heat Map
Script that opens CoinMarketCap "Trending", converts table to CSV,
plots top-7 %-gainers with matplotlib.
"""

import asyncio
import pandas as pd
import matplotlib.pyplot as plt
import pyperclip
from pathlib import Path
from typing import List, Optional
from pydantic import BaseModel
from browser_use import Agent, Controller
from langchain_openai import ChatOpenAI

class CryptoCurrency(BaseModel):
    name: str
    symbol: str
    price: Optional[str] = None
    percent_change_24h: float
    market_cap: Optional[str] = None
    volume_24h: Optional[str] = None

class CryptoData(BaseModel):
    cryptocurrencies: List[CryptoCurrency]

controller = Controller(output_model=CryptoData)

async def main():
    print("Opening CoinMarketCap and extracting trending cryptocurrency data...")
    
    # Initialize the agent with a task to extract cryptocurrency data
    task = """
    Go to https://coinmarketcap.com/trending-cryptocurrencies/

    Extract the trending cryptocurrencies table data including:
    - Name
    - Symbol
    - Price
    - 24h percent change (convert to numbers, not strings with %)
    - Market cap
    - 24h volume
    
    Format the data according to the CryptoData model with a list of cryptocurrencies.
    """
    
    # Create and run the agent
    agent = Agent(
        task=task,
        llm=ChatOpenAI(model="gpt-4o"),
        controller=controller
    )
    
    # Run the agent to extract data
    history = await agent.run(max_steps=5)
    
    # Get the final result from the agent
    result = history.final_result()
    
    if not result:
        print("No data was extracted. Exiting.")
        return
    
    # Parse the result using our Pydantic model
    crypto_data = CryptoData.model_validate_json(result)
    
    df = pd.DataFrame([crypto.model_dump() for crypto in crypto_data.cryptocurrencies])
    
    df['percent_change_24h'] = pd.to_numeric(df['percent_change_24h'], errors='coerce')
    
    csv_path = Path('crypto_data.csv')
    df.to_csv(csv_path, index=False)
    
    tsv_data = df.to_csv(sep='\t', index=False)
    pyperclip.copy(tsv_data)
    
    print(f"✓ Full data saved to {csv_path}")
    print(f"✓ Data copied to clipboard as TSV")
    
    top_gainers = df.sort_values(by='percent_change_24h', ascending=False).head(7)
    
    print(f"\n🔥 Top 7 Crypto Gainers (24h):")
    for _, row in top_gainers.iterrows():
        print(f"{row['name']} ({row['symbol']}): {row['percent_change_24h']}%")
    
    # Create visualization
    plt.figure(figsize=(12, 8))
    
    # Set up the bar chart
    bars = plt.bar(top_gainers['symbol'], top_gainers['percent_change_24h'])
    
    # Color the bars based on percentage change
    for i, bar in enumerate(bars):
        value = top_gainers['percent_change_24h'].iloc[i]
        if value < 0:
            bar.set_color('firebrick')  # Red for negative
        else:
            # Use a gradient of greens based on percentage
            if value > 50:
                bar.set_color('darkgreen')  # Very high gains
            elif value > 25:
                bar.set_color('forestgreen')  # High gains
            elif value > 10:
                bar.set_color('limegreen')  # Medium gains
            else:
                bar.set_color('mediumseagreen')  # Low gains
    
    # Add data labels on top of bars
    for bar in bars:
        height = bar.get_height()
        plt.text(bar.get_x() + bar.get_width()/2., height + 0.5,
                 f'{height:.2f}%', ha='center', va='bottom', fontweight='bold')
    
    # Chart styling
    plt.title('Top 7 Crypto Gainers - 24h Change', fontsize=16, fontweight='bold')
    plt.xlabel('Cryptocurrency', fontsize=14)
    plt.ylabel('24h Change (%)', fontsize=14)
    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.ylim(0, max(top_gainers['percent_change_24h']) * 1.15)  # Add 15% padding to top
    plt.tight_layout()
    
    plt.savefig('crypto_gainers.png')
    print(f"✓ Plot saved as crypto_gainers.png")
    
    plt.show()

if __name__ == "__main__":
    asyncio.run(main())`

3/30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant