This project provides a Python wrapper for the CoinGecko API, enabling users to fetch cryptocurrency data, process it into pandas DataFrames, and upload it to AWS S3 as Parquet files. The wrapper abstracts away the complexity of asynchronous API calls and provides an easy-to-use interface for interacting with the CoinGecko API.
-
Clone the repository:
```bash git clone https://github.com/yourusername/coingecko-api-wrapper.git cd coingecko-api-wrapper ```
-
Set up a virtual environment:
```bash python3 -m venv venv source venv/bin/activate ```
-
Install the required dependencies:
```bash pip install -r requirements.txt ```
The `CoinGeckoAPI` class is responsible for interacting directly with the CoinGecko API. It handles:
- Making asynchronous GET requests to the API.
- Managing rate limits using `AsyncLimiter`.
- Fetching data from paginated endpoints.
- `fetch(endpoint: str, params: dict = None, retries: int = 3) -> dict`: Performs a GET request to the specified endpoint.
- `fetch_all_pages(endpoint: str, params: dict, max_pages: int) -> list`: Fetches all pages of data asynchronously.
The `CoinGeckoDataProcessor` class processes raw data fetched from the CoinGecko API. It handles:
- Merging timeseries data into a pandas DataFrame.
- Saving DataFrames to Parquet files.
- `merge_timeseries_data(data: dict, coingecko_id: str) -> pd.DataFrame`: Cleans and merges timeseries data.
- `save_to_parquet(df: pd.DataFrame, file_path: str)`: Saves a DataFrame to a Parquet file.
The `CoinGecko` class is the main interface for users. It abstracts away the complexities of asynchronous calls and provides simple, synchronous methods to fetch data, process it, and upload it to S3.
- `get_categories() -> pd.DataFrame`: Fetches all categories from the CoinGecko API.
- `get_coins(coins: int, category: str = None) -> pd.DataFrame`: Fetches a list of assets based on market cap.
- `get_timeseries(coingecko_ids: list) -> pd.DataFrame`: Fetches and processes timeseries data.
- `export_data(coins: Union[int, List[str]], export_format: str = 'df') -> pd.DataFrame`: Fetches and exports data as a DataFrame or saves it as a Parquet file.
- `upload_to_s3(df: pd.DataFrame, aws_access_key_id: str, aws_secret_access_key: str, bucket_name: str, folder_name: str = None, file_name: str = None)`: Uploads a DataFrame to AWS S3 as a Parquet file.
```python from coingecko import CoinGecko
cg = CoinGecko(api_key="your-api-key")
categories_df = cg.get_categories() print(categories_df) ```
```python from coingecko import CoinGecko
cg = CoinGecko(api_key="your-api-key")
coins_df = cg.get_coins(100) print(coins_df) ```
```python from coingecko import CoinGecko
cg = CoinGecko(api_key="your-api-key")
timeseries_df = cg.get_timeseries(['bitcoin', 'ethereum']) print(timeseries_df) ```
```python from coingecko import CoinGecko
cg = CoinGecko(api_key="your-api-key")
cg.export_data(coins=100, export_format='parquet') ```
```python from coingecko import CoinGecko
cg = CoinGecko(api_key="your-api-key")
coins_df = cg.get_coins(100) cg.upload_to_s3( df=coins_df, aws_access_key_id="your-aws-access-key-id", aws_secret_access_key="your-aws-secret-access-key", bucket_name="your-s3-bucket-name", folder_name="your-folder-name", file_name="your-file-name.parquet" ) ```