Last active
February 20, 2018 15:07
-
-
Save onyb/99659ab381a4517ec95119b7cf2a65ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Exploring the CoinMarketCap API using reobject\n", | |
"\n", | |
"In this demo, we are going to play with the API of CoinMarketCap using reobject.\n", | |
"\n", | |
"Install the packages used in this notebook by doing: `pip install reobject requests`\n", | |
"\n", | |
"Example response of GET https://api.coinmarketcap.com/v1/ticker/\n", | |
"\n", | |
"```json\n", | |
"[\n", | |
" {\n", | |
" \"name\": \"Bitcoin\",\n", | |
" \"symbol\": \"BTC\",\n", | |
" \"rank\": \"1\",\n", | |
" \"price_usd\": \"11109.3\",\n", | |
" \"24h_volume_usd\": \"7389880000.0\",\n", | |
" \"market_cap_usd\": \"187461794302\",\n", | |
" \"available_supply\": \"16874312.0\",\n", | |
" \"total_supply\": \"16874312.0\",\n", | |
" \"max_supply\": \"21000000.0\",\n", | |
" \"percent_change_24h\": \"2.57\",\n", | |
" \"percent_change_7d\": \"26.87\",\n", | |
" ...\n", | |
" },\n", | |
" ]\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Imports" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from reobject.models import Model\n", | |
"from reobject.models.fields import Field" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Basic implementation of Coin model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Coin(Model):\n", | |
" rank = Field()\n", | |
" name = Field()\n", | |
" symbol = Field()\n", | |
" percent_change_24h = Field()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create some instances of our Coin class. Notice how we aren't storing\n", | |
"# references to our objects!\n", | |
"\n", | |
"Coin(rank=1, name='Bitcoin', symbol='BTC', percent_change_24h=-10.0)\n", | |
"Coin(rank=2, name='Ethereum', symbol='ETH', percent_change_24h=-70.0)\n", | |
"\n", | |
"Coin.objects.all()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Adding some goodness to our Coin model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Add a model property, and some automatic type converters to fields\n", | |
"\n", | |
"class Coin(Model):\n", | |
" rank = Field(converter=int)\n", | |
" name = Field()\n", | |
" symbol = Field()\n", | |
" percent_change_24h = Field(converter=float)\n", | |
" \n", | |
" @property\n", | |
" def is_green(self) -> bool:\n", | |
" return self.percent_change_24h > 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Populate real data from CoinMarketCap\n", | |
"\n", | |
"import requests\n", | |
"\n", | |
"Coin.objects.all().delete()\n", | |
"\n", | |
"for coin in requests.get('https://api.coinmarketcap.com/v1/ticker/?limit=200').json():\n", | |
" Coin(rank=coin['rank'], name=coin['name'],\n", | |
" symbol=coin['symbol'], percent_change_24h=coin['percent_change_24h'])\n", | |
"\n", | |
"Coin.objects.count()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Get all coins that made a loss in the last 24 hours\n", | |
"\n", | |
"Coin.objects.filter(percent_change_24h__lt=0.0).order_by('percent_change_24h')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Get 24h change of your favorite coins\n", | |
"\n", | |
"my_coins = {'BTC', 'ETH', 'LTC', 'BCH', 'XLM'}\n", | |
"\n", | |
"Coin.objects.filter(symbol__in=my_coins).values('percent_change_24h', 'name')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Get coins that have either \"network\" or \"shares\" in their names.\n", | |
"\n", | |
"from reobject.query import Q\n", | |
"\n", | |
"Coin.objects.filter(Q(name__icontains='network') | Q(name__icontains='shares'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# You can even use properties as lookup params in the query.\n", | |
"\n", | |
"Coin.objects.filter(is_green=True, symbol__in=my_coins)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "env3", | |
"language": "python", | |
"name": "env3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment