forked from edenau/discord-sidebar-price-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgi_run.py
78 lines (68 loc) · 2.43 KB
/
cfgi_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Run a Discord sidebar bot that shows the Crypto Fear and Greed Index (CFGI)
"""
# Example:
# python3 cfgi_run.py -l 7 &
from typing import Tuple
def get_index(lengthscale: int = 7,
verbose: bool = False) -> Tuple[int]:
"""
Fetch index from Alternative Me API
"""
import requests
import time
r = requests.get('https://api.alternative.me/fng/',
params={'limit':lengthscale+1})
if r.status_code == 200:
if verbose:
print('200 OK')
data = r.json()['data']
past_indices = [int(dict_['value']) for dict_ in data[1:]]
return int(data[0]['value']), sum(past_indices)/len(past_indices)
else:
if verbose:
print(r.status_code)
time.sleep(10)
def main(lengthscale: int = 7,
verbose: bool = False):
import yaml
import discord
import asyncio
# 1. Load config
filename = 'cfgi_config.yaml'
with open(filename) as f:
config = yaml.load(f, Loader=yaml.Loader)
# 2. Connect w the bot
client = discord.Client()
async def send_update(now, past, lengthscale):
nickname = f'😨🤑 {now}'
status = f'{lengthscale}D-SMA: {round(past,1)}'
await client.get_guild(config['guildId']).me.edit(nick=nickname)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching,
name=status))
await asyncio.sleep(config['updateFreq']) # in seconds
@client.event
async def on_ready():
"""
When discord client is ready
"""
while True:
# 3. Fetch index
# now is today's index; past is {lengthscale}-day SMA
now, past = get_index(lengthscale=lengthscale, verbose=verbose)
# 4. Feed it to the bot
await send_update(now, past, lengthscale)
client.run(config['discordBotKey'])
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--length',
type=int,
default=7,
help='Specify #-day SMA lengthscale')
parser.add_argument('-v', '--verbose',
action='store_true', # equiv. default is False
help='toggle verbose')
args = parser.parse_args()
main(lengthscale=args.length,
verbose=args.verbose)