# ICP NFTフロア価格(その日の最低取引価格とする)のデータを引っ張りたいとき

By [Shogaku](https://paragraph.com/@shogaku) · 2023-01-30

---

* * *

ブログコピー元　↓

[https://hide.ac/articles/ocAMI6eK\_](https://hide.ac/articles/ocAMI6eK_)

* * *

ICP NFTフロア価格(その日の最低取引価格とする)のデータを引っ張りたいとき
----------------------------------------

以下に流れを記載

* * *

NFTトランザクションデータを収集するスクリプト
------------------------

!rm -r nfts\_data は、繰り返し使ったときにエラー出た対処の名残りで残しています

    !rm -r nfts_data
    !git clone https://github.com/lukasvozda/nfts_data.git
    !pip install -r nfts_data/requirements.txt
    
    from ic.agent import *
    from ic.identity import *
    from ic.client import *
    from ic.candid import Types, encode, decode
    
    # Inicialisation
    client = Client(url = "https://ic0.app")
    iden = Identity()
    agent= Agent(iden, client)
    
    # Endpoint that we want to pull data from
    function_to_call = 'transactions'
    
    # List of NFTs canisters we want to pull data from
    # You can find it on Entrepot clicking on NFTs 
    canisters = [
        {
            'name': 'Motoko',
            'id': 'oeee4-qaaaa-aaaak-qaaeq-cai'
        },
        {
            'name': 'BTC Flowers',
            'id': 'pk6rk-6aaaa-aaaae-qaazq-cai'
        },
        {
            'name': 'Poked bots',
            'id': 'bzsui-sqaaa-aaaah-qce2a-cai'
        },
    ]
    
    # File to write results to
    f  = open('result.txt','w')
    # Header row
    f.write(f"collection,index,token,icp,time_updated,timestamp,seller,buyer")
    
    for c in canisters:
        params = []
    
        params = encode(params)
    
        response = agent.query_raw(c["id"], function_to_call, params)
    
        print("Getting collection:", c["name"])
    
        trans = response[0]["value"]
        for i,t in enumerate(trans):
            price = str(t['_3364572809']/100000000)
            timestamp = t['_1291635725']
            token = t['_338395897']
            seller = t['_1782082687']
            buyer = t['_3136747827']
            f.write(f"\n{c['name']},{i},{token},{price},,{timestamp},{seller},{buyer}")
    
    f.close()
    

*   このコードは、Interledger 関連の技術を使用して、NFTトランザクションデータを収集するスクリプト[https://github.com/lukasvozda/nfts\_data.git](https://github.com/lukasvozda/nfts_data.git) を見ながら実施
    

以下のような部分はお好みのNFTに置き換えくださいnameは適当で良いはず

> 例
> 
> name: 'Motoko'
> 
> id: 'oeee4-qaaaa-aaaak-qaaeq-cai'

* * *

取得したデータ確認
---------

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    df = pd.read_csv("result.txt", header=0)
    
    print(df)
    

![](https://storage.googleapis.com/papyrus_images/0c457027adc3537282dfb7e01f997f44a49098c3092466eced042ffc2f501d80.png)

* * *

グラフ化
----

    sns.scatterplot(x='timestamp', y='icp', data=df)
    plt.show()
    

![](https://storage.googleapis.com/papyrus_images/97f0c218ebe63ccc85a2c7f73699ccc3ab4efb468522c517a8fe33c28ea67d1b.png)

* * *

日時を修正、レジェンド追加
-------------

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df = pd.read_csv("result.txt", header=0)
    df['timestamp'] = pd.to_datetime(df['timestamp'])
    
    sns.scatterplot(x='timestamp', y='icp', hue='collection', legend=True, data=df)
    
    plt.show()
    

![](https://storage.googleapis.com/papyrus_images/e7a593879543e88c38b3cf49086474b2b7520e836777bdecd10c29ffcd7d7790.png)

* * *

フロア価格
-----

その日の最低取引価格、ない場合は前日を引き継ぐ

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df['day'] = df['timestamp'].dt.date
    df_min = df.groupby(['day', 'collection'])['icp'].min()
    
    # ffillメソッドを使用して、前日のデータで空欄を埋める
    df_min = df_min.ffill()
    
    # プロットする
    ax = sns.scatterplot(x=df_min.index.get_level_values(0), y=df_min, hue=df_min.index.get_level_values(1))
    ax.legend(loc='center right', bbox_to_anchor=(1.35, 0.9))
    plt.show()
    

![](https://storage.googleapis.com/papyrus_images/5093b66e746777c80607743a8f81001ce7de6278d4e6004113ee171ad7100a93.png)

* * *

csvファイルに保存する
------------

    import pandas as pd
    
    #DataFrameを作成
    df_temp = pd.DataFrame({
    'date': df_min.index.get_level_values(0),
    'ICP': df_min,
    'collection': df_min.index.get_level_values(1)
    })
    
    #CSV形式で保存する
    df_temp.to_csv("min_icp.csv", index=False)
    

* * *

Google Driveに保存する
-----------------

    from google.colab import drive
    drive.mount('/content/gdrive')
    
    df_temp.to_csv('/content/gdrive/My Drive/min_icp.csv', index=False)
    

![](https://storage.googleapis.com/papyrus_images/94af8532cdf4f22e6059a5a22a5fec32d0e98a76f8aecae806814267197b5f10.png)

* * *

というような感じとりあえずデータを出せた。

---

*Originally published on [Shogaku](https://paragraph.com/@shogaku/icp-nft)*
