Google Colaboratory からツイートツリー (ICP 価格 & ICP NFT データで試しに実施)

グーグルコラボからツイートツリー

ICP 価格 & ICP NFT データで実験

備忘録

***

# ICP 価格 & ICP NFT transaction
<pre><code>!pip install requests</code></pre>

***

<pre><code>!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': 'Clown Skateboards',
        'id': '2v5zm-uaaaa-aaaae-qaewa-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()
</code></pre>

![](https://pbs.twimg.com/media/Fq6Nt0jaIAAV-lu?format=png&name=small)

***

<pre><code>import tweepy
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import requests
import json
import tweepy

# Twitterに認証
auth = tweepy.OAuthHandler(os.environ["TWITTER_CONSUMER_KEY"], os.environ["TWITTER_CONSUMER_SECRET"])
auth.set_access_token(os.environ["TWITTER_ACCESS_TOKEN"], os.environ["TWITTER_ACCESS_SECRET"])

# APIオブジェクトを作成
api = tweepy.API(auth)

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.grid(linestyle='dotted')

#レジェンド
plt.legend(loc='upper left')

#Y軸 Max1000
import matplotlib.pyplot as plt
#plt.ylim(0, 700)

#X軸のラベルを変更
import matplotlib.dates as mdates

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%y/%m'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=2))
plt.gcf().autofmt_xdate()

plt.savefig('plot.png')

def getCryptoPrice():
    API_KEY = "ここはCoinMarketCapで取得したAPIを書く"
    responseJPY = requests.get(f"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=ICP&convert=JPY&CMC_PRO_API_KEY={API_KEY}")
    jsonJPY = json.loads(responseJPY.text)
    price_jpy = jsonJPY['data']['ICP']['quote']['JPY']['price']

    responseUSD = requests.get(f"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=ICP&convert=USD&CMC_PRO_API_KEY={API_KEY}")
    jsonUSD = json.loads(responseUSD.text)
    price_usd = jsonUSD['data']['ICP']['quote']['USD']['price']
    
    # ICPの価格をツイート
    tweet_text = f"ICPの価格\nUSD: {price_usd:.2f}\nJPY: {price_jpy:.2f}"
    tweet = api.update_status(tweet_text)

    # スクリーンショットを添付したツイートを返信
    media = api.media_upload("plot.png")
    api.update_status("#Ludo #ClownSkateboards", in_reply_to_status_id=tweet.id_str, media_ids=[media.media_id])

getCryptoPrice() #

</code></pre>

--------------

以下のような感じ

[https://twitter.com/ussu_ussu_ussu/status/1633675144265887744](https://twitter.com/ussu_ussu_ussu/status/1633675144265887744)

![](https://pbs.twimg.com/media/Fq6OZbFaAAEq2ls?format=png&name=900x900)