fear and greed indexをXにポストするプログラム

※素人プログラミングですので自己責任の参考程度でお願いします。

OS:Ubuntu20.04(64bit)
言語:python

・consumer_key
・consumer_secret
・access_token
・access_token_secret
はXで別途取得して置き換える必要があります(XのAPIを使用するので)。
取得方法については再度やる機会があれば追記します。

ライブラリは別途インストールが必要ですがそこまでハードルは高くありません。

半日に1回、株のfear and greed indexとBitcoinのfear and greed indexをpostするプログラムです(post時刻が徐々にずれてはしまいますが)。

import tweepy
import requests
import time

def tweet(message):
        client = tweepy.Client(
                consumer_key="xxx",
                consumer_secret="yyy",
                access_token="zzz",
                access_token_secret="ttt",
        )
        client.create_tweet(text = message)


counter = 0

while True:
        url = "https://production.dataviz.cnn.io/index/fearandgreed/graphdata"

        r = requests.get(url,
                headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                        'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
        })
        score_ = r.json()['fear_and_greed']['score']
        score = int(score_)

        message = "Fear and Greed Index (now):\n" + str(score) + "\n\n" + "0-25:Extreme Fear\n" + "25-45:Fear\n" + "45-55:Neutoral\n" + "55-75:Greed\n" + "75-100:Extreme Greed\n\n\n\n" + "\n\nAlive Monitor:" + str(counter) + "\n\n"

        print(message)
        tweet(message)

        time.sleep(200)

        consumer_key="xxx"
        consumer_secret="yyy"
        access_token="zzz"
        access_token_secret="ttt"
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        api = tweepy.API(auth)

        client = tweepy.Client(
                consumer_key = consumer_key,
                consumer_secret = consumer_secret,
                access_token = access_token,
                access_token_secret = access_token_secret,
        )

        file_path = './data/file.png'
        url = "https://alternative.me/crypto/fear-and-greed-index.png"
        response = requests.get(url)
        image = response.content
        with open(file_path, "wb") as f:
                f.write(image)

        media = api.media_upload(filename=file_path)

        message = "Bitcoin Fear and Greed Index (now)\n" + "\n" + "0-25:Extreme Fear\n" + "25-45:Fear\n" + "45-55:Neutoral\n" + "55-75:Greed\n" + "75-100:Extreme Greed\n\n\n\n" + "BTC Open Interest URL:\n" + "https://coinalyze.net/b>

        client.create_tweet(text=message, media_ids=[media.media_id])
        print(message)

        time.sleep(43000)

        counter = counter + 1
        if counter > 99:
                counter = 0

この記事が気に入ったらサポートをしてみませんか?