Skip to content

Instantly share code, notes, and snippets.

@Partysun
Created November 2, 2020 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Partysun/7f9fe6a0c357a41d22676867810bdd35 to your computer and use it in GitHub Desktop.
Save Partysun/7f9fe6a0c357a41d22676867810bdd35 to your computer and use it in GitHub Desktop.
How to download csv data from Sanbase.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "How to download csv data from Sanbase.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyOtusSn+LPJWG2ahG+DKGoj",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Partysun/7f9fe6a0c357a41d22676867810bdd35/how-to-download-csv-data-from-sanbase.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "E5x3QnjiMxGb"
},
"source": [
"## Install sanpy library"
],
"execution_count": 40,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "_BgyvAh-OjJQ"
},
"source": [
"1. Install sanpy library\n",
" \n",
" For more details -> visit [sanpy github](https://github.com/santiment/sanpy)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "JNjk4lGYKZue",
"outputId": "ebedd501-3d9c-4ced-bff7-f238429bc6ea",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"!pip install sanpy"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Collecting sanpy\n",
" Downloading https://files.pythonhosted.org/packages/57/10/3c7974cdfe76f827afb57fbb05b15058287c29d0ae1c0911faa16dfbac36/sanpy-0.8.5-py3-none-any.whl\n",
"Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from sanpy) (1.1.3)\n",
"Collecting iso8601\n",
" Downloading https://files.pythonhosted.org/packages/f5/c7/06b5938e79a09112a256f26a1ae0551b04bc6aafcb71fd7ee2390c2c54c6/iso8601-0.1.13-py2.py3-none-any.whl\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from sanpy) (2.23.0)\n",
"Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from sanpy) (50.3.2)\n",
"Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->sanpy) (2018.9)\n",
"Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.6/dist-packages (from pandas->sanpy) (1.18.5)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.6/dist-packages (from pandas->sanpy) (2.8.1)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->sanpy) (1.24.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->sanpy) (2020.6.20)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->sanpy) (3.0.4)\n",
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->sanpy) (2.10)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.7.3->pandas->sanpy) (1.15.0)\n",
"Installing collected packages: iso8601, sanpy\n",
"Successfully installed iso8601-0.1.13 sanpy-0.8.5\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6u0QXuBMO5d3"
},
"source": [
"2. Pick desirable timeframe\n",
" \n",
" In this example we take last 7 days"
]
},
{
"cell_type": "code",
"metadata": {
"id": "kTjqKhzbNXMw",
"outputId": "138f8a32-15b7-4b34-ba19-81188d3c913c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 37
}
},
"source": [
"from datetime import datetime, timedelta\n",
"utc_now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')\n",
"from_date = (datetime.utcnow() - timedelta(days=7)).strftime('%Y-%m-%dT%H:%M:%SZ')"
],
"execution_count": 37,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'2020-10-26T13:50:06Z'"
]
},
"metadata": {
"tags": []
},
"execution_count": 37
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CBZrIbwVPGXU"
},
"source": [
"3. Fetch data\n",
" \n",
" In this example we fetch daily_active_address metric.\n",
" For more metrics you can check [this](https://api.santiment.net/graphiql?query=%7BgetAvailableMetrics%7D) or [Santiment Academy](https://academy.santiment.net/metrics/)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "r3BZewx3K8Rb"
},
"source": [
"import san\n",
"\n",
"df = san.get(\n",
" \"daily_active_addresses/santiment\",\n",
" from_date=from_date,\n",
" to_date=utc_now,\n",
" interval=\"1d\"\n",
")"
],
"execution_count": 38,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "q5epQDbnMMlu",
"outputId": "f9d9a9a0-8a6a-496d-e7c9-21964bb0fc8c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"from IPython.display import HTML\n",
"import base64\n",
"\n",
"def create_download_link(df, title = \"Download CSV file\", filename = \"data.csv\"): \n",
" csv = df.to_csv()\n",
" b64 = base64.b64encode(csv.encode())\n",
" payload = b64.decode()\n",
" html = '<a download=\"{filename}\" href=\"data:text/csv;base64,{payload}\" target=\"_blank\">{title}</a>'\n",
" html = html.format(payload=payload,title=title,filename=filename)\n",
" return HTML(html)\n",
"\n",
"## Let's create a link for CSV file\n",
"create_download_link(df)"
],
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"<a download=\"data.csv\" href=\"data:text/csv;base64,ZGF0ZXRpbWUsdmFsdWUKMjAyMC0xMC0yNyAwMDowMDowMCswMDowMCw5LjAKMjAyMC0xMC0yOCAwMDowMDowMCswMDowMCwxNS4wCjIwMjAtMTAtMjkgMDA6MDA6MDArMDA6MDAsMTYuMAoyMDIwLTEwLTMwIDAwOjAwOjAwKzAwOjAwLDE0LjAKMjAyMC0xMC0zMSAwMDowMDowMCswMDowMCwyLjAKMjAyMC0xMS0wMSAwMDowMDowMCswMDowMCwyLjAKMjAyMC0xMS0wMiAwMDowMDowMCswMDowMCw1LjAK\" target=\"_blank\">Download CSV file</a>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
},
"execution_count": 41
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EEGcKIe9Obe5"
},
"source": [
""
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment