0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

仮想通貨の現在価格をLineに通知したい

Posted at

はじめに

特定の仮想通貨の現在価格を定期的に知りたいなと思い色々調べていたら
Line NotifyCoinMarketCap を使えばできることが判明!!
しかもどちらも無料で使えるという。。。(リクエスト回数?とか縛りはありそう)
これで億万長者...

目次

  1. 事前準備
  2. 実装
  3. 参考

事前準備

Line NotifyCoinMarketCap へ登録しAPIキーを取得する

Line Notifyの登録

すでに色々な方が記載しているのでこちらは割愛。。。

参考:PythonでLINE Notifyへ通知を送る

CoinMarketCapの登録

CoinMarketCap の「GET YOUR API KEY NOW」をクリック
image.png

② 項目を埋めて「CREATE MY ACCOUNT」をクリック
※無料で使用枠は初期設定のBasicのみ
image.png
③ ログイン後APIキーをコピー
API KEYにカーソルをもっていけばコピーできます
image.png

API詳細については画面左下のAPI DOCUMNETATIONから確認可能
今回は特定の通貨の値段を知りたいので/cryptocurrency/*を使う
SnapCrab_NoName_2022-8-13_11-2-39_No-00.png

実装

#!/usr/bin/env python3
import requests
import json
import numpy as np

# CoinMarketCap API
COIN_API_KEY = 'hogehogehogehogehogehogehogehogehogehoge'
COIN_URL = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'

# Line Notify API
LINE_API_KEY = 'hagehagehagehagehagehagehagehagehagehage'
LINE_URL = 'https://notify-api.line.me/api/notify'

def get_coin_data():
    targe = {'GMT,SOL'}

    # CoinMarketCapリクエスト情報
    parameters = {
        # 取得時の法定通貨
        'convert': 'USD',
        # 取得対
        'symbol': 'GMT,SOL' # todo target
    }
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': COIN_API_KEY,
    }

    # CoinMarketCapから通貨情報を取得
    session = requests.Session()
    session.headers.update(headers)
    try:
        response = session.get(COIN_URL, params=parameters)
        # response = requests.get(COIN_URL, headers=headers)
        resp = json.loads(response.text)
        data = resp['data']
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print(e)
    
    # とりあえず必要な情報のみ拾う
    line_message=''
    for key in data:
        symbol = key
        price = np.round(data[key]['quote']['USD']['price'],decimals=3)
        line_message+=f'\n{key} -> price: {price}$'

    # Lineに通知
    headers = {'Authorization': f'Bearer {LINE_API_KEY}'}
    data = {'message': f'message: {line_message}'}
    requests.post(LINE_URL, headers=headers, data=data)

if __name__ == '__main__':
    get_coin_data()


結果

S__13393927.jpg

参考

PythonでLINE Notifyへ通知を送る
CoinMarketCap APIを使ってみる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?