asynchronous is stable

This commit is contained in:
anon 2022-09-17 00:04:08 -03:00
parent a48df1813c
commit 2a54a9a0e6
3 changed files with 1141 additions and 1052 deletions

459
charts/asynchronous.py Normal file
View file

@ -0,0 +1,459 @@
import aiohttp
import asyncio
import json
from .synchronous import *
import datetime
from datetime import date, timedelta
from .models import Coin, Social, P2Pool
import requests
import pygsheets
####################################################################################
# Asynchronous get block data from xmrchain
####################################################################################
async def get_block_data(session, block: str):
url = 'https://xmrchain.net/api/block/' + block
async with session.get(url) as res:
data = await res.read()
data = json.loads(data)
data['provider'] = 'xmrchain'
if res.status < 299:
data['success'] = True
else:
data['success'] = False
return data
####################################################################################
# Asynchronous get network data from xmrchain
####################################################################################
async def get_network_data(session, block: str):
url = 'https://xmrchain.net/api/networkinfo'
async with session.get(url) as res:
data = await res.read()
data = json.loads(data)
data['provider'] = 'xmrchain'
if res.status < 299:
data['success'] = True
else:
data['success'] = False
return data
####################################################################################
# Asynchronous get coinmarketcap data for price USD and BTC
####################################################################################
async def get_coinmarketcap_data(session, symbol: str, convert: str):
with open("settings.json") as file:
data = json.load(file)
file.close()
url = data["metrics_provider"][0]["price_url_old"] + symbol
parameters = {'convert': convert,}
headers = {'Accepts': 'application/json', data["metrics_provider"][0]["api_key_name"]: data["metrics_provider"][0]["api_key_value"],}
async with session.get(url, headers=headers, params=parameters) as res:
data = await res.read()
data = json.loads(data)
data['provider'] = 'coinmarketcap'
if res.status < 299:
try:
if data['data'][symbol.upper()]['cmc_rank']:
data['success'] = True
else:
data['success'] = False
except:
data['success'] = False
else:
data['success'] = False
return data
####################################################################################
# Asynchronous get coinmetrics data for any coin inside URL
####################################################################################
async def get_coin_data(session, symbol, url):
update = True
count = 0
while update:
async with session.get(url) as res:
data = await res.read()
data = json.loads(data)
data_aux = data['data']
for item in data_aux:
day, hour = str(item['time']).split('T')
day = datetime.datetime.strptime(day, '%Y-%m-%d')
day = datetime.datetime.strftime(day, '%Y-%m-%d')
try:
coin = Coin.objects.filter(name=symbol).get(date=day)
except:
coin = Coin()
try:
coin.name = symbol
coin.date = day
try:
coin.priceusd = float(item['PriceUSD'])
except:
coin.priceusd = 0
try:
coin.pricebtc = float(item['PriceBTC'])
except:
coin.pricebtc = 0
try:
coin.inflation = float(item['IssContPctAnn'])
coin.stocktoflow = (100/coin.inflation)**1.65
except:
coin.inflation = 0
coin.stocktoflow = 0
try:
coin.supply = float(item['SplyCur'])
except:
coin.supply = 0
try:
coin.fee = float(item['FeeTotNtv'])
except:
coin.fee = 0
try:
coin.revenue = float(item['RevNtv'])
except:
coin.revenue = 0
try:
coin.hashrate = float(item['HashRate'])
except:
coin.hashrate = 0
try:
coin.transactions = float(item['TxCnt'])
except:
coin.transactions = 0
coin.save()
count += 1
except:
pass
try:
url = data['next_page_url']
update = True
except:
update = False
break
return count
####################################################################################
# Asynchronous get social metrics from reddit
####################################################################################
async def get_social_data(session, symbol):
date_now = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
socials = Social.objects.filter(name=symbol).filter(date=date_now)
if not(socials):
print('getting new data')
url = 'https://www.reddit.com/r/'+ symbol +'/about.json'
async with session.get(url, headers={'User-agent': 'Checking new social data'}) as res:
data = await res.read()
data = json.loads(data)
data = data['data']
subscribers = data['subscribers']
social = Social()
social.name = symbol
social.date = date_now
social.subscriberCount = subscribers
date_aux = date.today()
date_aux = datetime.datetime.strftime(date_aux, '%Y-%m-%d')
date_aux = datetime.datetime.strptime(date_aux, '%Y-%m-%d')
timestamp1 = int(datetime.datetime.timestamp(date_aux))
timestamp2 = int(timestamp1 - 86400)
limit = 2000
filters = []
data = data_prep_posts(symbol, timestamp2, timestamp1, filters, limit)
social.postsPerHour = len(data)/24
timestamp2 = int(timestamp1 - 7200)
limit = 2000
data = data_prep_comments(symbol, timestamp2, timestamp1, filters, limit)
social.commentsPerHour = len(data)/2
social.save()
return True
####################################################################################
# Asynchronous get whole xmr data calling coinmarketcap and xmrchain
####################################################################################
async def update_xmr_data(yesterday, coin):
name = coin.name
url = 'https://xmrchain.net/api/networkinfo'
response = requests.get(url)
data = json.loads(response.text)
height = int(data['data']['height'])
difficulty = int(data['data']['difficulty'])
hashrate = int(data['data']['hash_rate'])
blocksize = 0
actions = []
my_timeout = aiohttp.ClientTimeout(
total=10,
sock_connect=10,
sock_read=10
)
client_args = dict(
trust_env=True,
timeout=my_timeout
)
async with aiohttp.ClientSession(**client_args) as session:
for count in range(1, 1400):
block = str(height - count)
actions.append(asyncio.ensure_future(get_block_data(session, block)))
actions.append(asyncio.ensure_future(get_coinmarketcap_data(session, 'xmr', 'USD')))
actions.append(asyncio.ensure_future(get_coinmarketcap_data(session, 'xmr', 'BTC')))
try:
responses = await asyncio.gather(*actions, return_exceptions=True)
except asyncio.exceptions.TimeoutError:
print('Timeout!')
errors = 0
success = 0
txs = 0
revenue = 0
fees = 0
priceusd = 0
pricebtc = 0
for response in responses:
if response:
try:
if response['provider'] == 'xmrchain':
date_aux = response['data']['timestamp_utc'].split(' ')[0]
if date_aux == yesterday:
try:
blocksize += int(response['data']['size'])
for tx in response['data']['txs']:
if tx['coinbase']:
revenue += int(tx['xmr_outputs'])
else:
txs += 1
fees += int(tx['tx_fee'])
revenue += int(tx['tx_fee'])
success += 1
except:
errors += 1
if response['provider'] == 'coinmarketcap':
try:
priceusd = float(response['data']['XMR']['quote']['USD']['price'])
update_rank(response)
update_dominance(response)
except:
try:
pricebtc = float(response['data']['XMR']['quote']['BTC']['price'])
except:
errors += 1
except:
errors += 1
else:
errors += 1
blocksize = blocksize/success
revenue = float(revenue)/10**12
fees = float(fees)/10**12
inflation = 100*365*(revenue)/float(coin.supply)
stocktoflow = (100/inflation)**1.65
supply = coin.supply + revenue
print('Name: ' + name)
print('Date: ' + str(yesterday))
print('Success: ' + str(success))
print('Errors: ' + str(errors))
print('Blocksize: ' + str(blocksize))
print('Transactions: ' + str(txs))
print('Revenue: ' + str(revenue))
print('Fees: ' + str(fees))
print('Inflation: ' + str(inflation))
print('Hashrate: ' + str(hashrate))
print('Difficulty: ' + str(difficulty))
print('Stocktoflow: ' + str(stocktoflow))
print('Priceusd: ' + str(priceusd))
print('Pricebtc: ' + str(pricebtc))
print('Supply: ' + str(supply))
try:
coin = Coin()
coin.name = name
coin.date = datetime.datetime.strptime(yesterday, '%Y-%m-%d')
coin.blocksize = blocksize
coin.transactions = txs
coin.revenue = revenue
coin.fee = fees
coin.inflation = inflation
coin.hashrate = hashrate
coin.difficulty = difficulty
coin.stocktoflow = stocktoflow
coin.priceusd = priceusd
coin.pricebtc = pricebtc
coin.supply = supply
coin.save()
except:
return False
return True
####################################################################################
# Asynchronous get social and coins data
####################################################################################
async def update_others_data(date):
with open("settings.json") as file:
data = json.load(file)
file.close()
url_btc = data["metrics_provider"][0]["metrics_url_new"] + 'btc/' + date
url_dash = data["metrics_provider"][0]["metrics_url_new"] + 'dash/' + date
url_grin = data["metrics_provider"][0]["metrics_url_new"] + 'grin/' + date
url_zec = data["metrics_provider"][0]["metrics_url_new"] + 'zec/' + date
actions = []
my_timeout = aiohttp.ClientTimeout(
total=10,
sock_connect=10,
sock_read=10
)
client_args = dict(
trust_env=True,
timeout=my_timeout
)
async with aiohttp.ClientSession(**client_args) as session:
# reddit data
actions.append(asyncio.ensure_future(get_social_data(session, 'Monero')))
actions.append(asyncio.ensure_future(get_social_data(session, 'Bitcoin')))
actions.append(asyncio.ensure_future(get_social_data(session, 'Cryptocurrency')))
# coinmetrics data
actions.append(asyncio.ensure_future(get_coin_data(session, 'btc', url_btc)))
actions.append(asyncio.ensure_future(get_coin_data(session, 'dash', url_dash)))
actions.append(asyncio.ensure_future(get_coin_data(session, 'grin', url_grin)))
actions.append(asyncio.ensure_future(get_coin_data(session, 'zec', url_zec)))
actions.append(asyncio.ensure_future(get_p2pool_data(session, mini=False)))
actions.append(asyncio.ensure_future(get_p2pool_data(session, mini=True)))
try:
await asyncio.gather(*actions, return_exceptions=True)
except asyncio.exceptions.TimeoutError:
print('Timeout!')
update_database(date, date)
return True
####################################################################################
# Asynchronous get p2pool and p2poolmini data and then save to google sheets
####################################################################################
async def get_p2pool_data(session, mini):
today = date.today()
yesterday = date.today() - timedelta(1)
try:
p2pool_stat = P2Pool.objects.filter(mini=mini).get(date=today)
if p2pool_stat.percentage > 0:
update = False
else:
p2pool_stat.delete()
try:
coin = Coin.objects.filter(name='xmr').get(date=yesterday)
if coin.hashrate > 0:
update = True
else:
update = False
except:
update = False
except:
try:
coin = Coin.objects.filter(name='xmr').get(date=yesterday)
if coin.hashrate > 0:
update = True
else:
update = False
except:
update = False
if update:
p2pool_stat = P2Pool()
p2pool_stat.date = today
if not(mini):
async with session.get('https://p2pool.io/api/pool/stats') as res:
data = await res.read()
data = json.loads(data)
p2pool_stat.hashrate = data['pool_statistics']['hashRate']
p2pool_stat.percentage = 100*data['pool_statistics']['hashRate']/coin.hashrate
p2pool_stat.miners = data['pool_statistics']['miners']
p2pool_stat.totalhashes = data['pool_statistics']['totalHashes']
p2pool_stat.totalblocksfound = data['pool_statistics']['totalBlocksFound']
p2pool_stat.mini = False
p2pool_stat.save()
print('p2pool saved!')
gc = pygsheets.authorize(service_file='service_account_credentials.json')
sh = gc.open('zcash_bitcoin')
wks = sh.worksheet_by_title('p2pool')
values_mat = wks.get_values(start=(3,1), end=(9999,3), returnas='matrix')
k = len(values_mat)
date_aux = datetime.datetime.strptime(values_mat[k-1][0], '%Y-%m-%d')
date_aux2 = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d')
if date_aux < date_aux2:
cell = 'F' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalblocksfound)
cell = 'E' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalhashes)
cell = 'D' + str(k + 3)
wks.update_value(cell, p2pool_stat.percentage)
cell = 'C' + str(k + 3)
wks.update_value(cell, p2pool_stat.hashrate)
cell = 'B' + str(k + 3)
wks.update_value(cell, p2pool_stat.miners)
cell = 'A' + str(k + 3)
wks.update_value(cell, datetime.datetime.strftime(p2pool_stat.date, '%Y-%m-%d'))
print('spreadsheet updated')
else:
print('spreadsheet already with the latest data')
return data
else:
async with session.get('https://p2pool.io/mini/api/pool/stats') as res:
data = await res.read()
data = json.loads(data)
p2pool_stat.hashrate = data['pool_statistics']['hashRate']
p2pool_stat.percentage = 100*data['pool_statistics']['hashRate']/coin.hashrate
p2pool_stat.miners = data['pool_statistics']['miners']
p2pool_stat.totalhashes = data['pool_statistics']['totalHashes']
p2pool_stat.totalblocksfound = data['pool_statistics']['totalBlocksFound']
p2pool_stat.mini = True
p2pool_stat.save()
print('p2pool_mini saved!')
gc = pygsheets.authorize(service_file='service_account_credentials.json')
sh = gc.open('zcash_bitcoin')
wks = sh.worksheet_by_title('p2poolmini')
values_mat = wks.get_values(start=(3,1), end=(9999,3), returnas='matrix')
k = len(values_mat)
date_aux = datetime.datetime.strptime(values_mat[k-1][0], '%Y-%m-%d')
date_aux2 = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d')
if date_aux < date_aux2:
cell = 'F' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalblocksfound)
cell = 'E' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalhashes)
cell = 'D' + str(k + 3)
wks.update_value(cell, p2pool_stat.percentage)
cell = 'C' + str(k + 3)
wks.update_value(cell, p2pool_stat.hashrate)
cell = 'B' + str(k + 3)
wks.update_value(cell, p2pool_stat.miners)
cell = 'A' + str(k + 3)
wks.update_value(cell, datetime.datetime.strftime(p2pool_stat.date, '%Y-%m-%d'))
print('spreadsheet updated')
else:
print('spreadsheet already with the latest data')
return data
else:
return False
return True

669
charts/synchronous.py Normal file
View file

@ -0,0 +1,669 @@
from psaw import PushshiftAPI
import pandas as pd
import json
import requests
import datetime
from datetime import date, timedelta
from .models import Coin, Social, P2Pool, Dominance, Rank, Sfmodel, DailyData
from requests import Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import pygsheets
####################################################################################
# Reddit api
####################################################################################
api = PushshiftAPI()
# Get daily post on Reddit
def data_prep_posts(subreddit, start_time, end_time, filters, limit):
if(len(filters) == 0):
filters = ['id', 'author', 'created_utc', 'domain', 'url', 'title', 'num_comments']
posts = list(api.search_submissions(subreddit=subreddit, after=start_time, before=end_time, filter=filters, limit=limit))
return pd.DataFrame(posts)
# Get daily comments on Reddit
def data_prep_comments(term, start_time, end_time, filters, limit):
if (len(filters) == 0):
filters = ['id', 'author', 'created_utc','body', 'permalink', 'subreddit']
comments = list(api.search_comments(q=term, after=start_time, before=end_time, filter=filters, limit=limit))
return pd.DataFrame(comments)
####################################################################################
# Other useful functions
####################################################################################
# Get most recent metrics from a data provider of your choice for 'symbol'
def get_latest_metrics(symbol, url):
update = True
count = 0
while update:
response = requests.get(url)
data = json.loads(response.text)
data_aux = data['data']
for item in data_aux:
day, hour = str(item['time']).split('T')
day = datetime.datetime.strptime(day, '%Y-%m-%d')
day = datetime.datetime.strftime(day, '%Y-%m-%d')
try:
coin = Coin.objects.filter(name=symbol).get(date=day)
except:
coin = Coin()
try:
coin.name = symbol
coin.date = day
try:
coin.priceusd = float(item['PriceUSD'])
except:
coin.priceusd = 0
try:
coin.pricebtc = float(item['PriceBTC'])
except:
coin.pricebtc = 0
try:
coin.inflation = float(item['IssContPctAnn'])
coin.stocktoflow = (100/coin.inflation)**1.65
except:
coin.inflation = 0
coin.stocktoflow = 0
try:
coin.supply = float(item['SplyCur'])
except:
coin.supply = 0
try:
coin.fee = float(item['FeeTotNtv'])
except:
coin.fee = 0
try:
coin.revenue = float(item['RevNtv'])
except:
coin.revenue = 0
try:
coin.hashrate = float(item['HashRate'])
except:
coin.hashrate = 0
try:
coin.transactions = float(item['TxCnt'])
except:
coin.transactions = 0
coin.save()
count += 1
print(str(symbol) + ' ' + str(coin.date))
except:
pass
try:
url = data['next_page_url']
update = True
except:
update = False
break
return count
# Get latest price data for Monero
def get_latest_price(symbol):
with open("settings.json") as file:
data = json.load(file)
url = data["metrics_provider"][0]["price_url_old"] + symbol
print(url)
parameters = {
'convert':'USD',
}
headers = {
'Accepts': 'application/json',
data["metrics_provider"][0]["api_key_name"]: data["metrics_provider"][0]["api_key_value"],
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
print(response)
data = json.loads(response.text)
print('getting latest data')
print(data)
try:
if data['data'][symbol.upper()]['cmc_rank']:
print('new data received')
pass
else:
print('problem with the data provider')
data = False
except:
data = False
except (ConnectionError, Timeout, TooManyRedirects) as e:
data = False
file.close()
return data
# Get latest dominance value and update
def update_dominance(data):
if not(data):
print('error updating dominance')
return False
else:
dominance = Dominance()
dominance.name = 'xmr'
dominance.date = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
dominance.dominance = float(data['data']['XMR']['quote']['USD']['market_cap_dominance'])
dominance.save()
gc = pygsheets.authorize(service_file='service_account_credentials.json')
sh = gc.open('zcash_bitcoin')
wks = sh.worksheet_by_title('Sheet7')
values_mat = wks.get_values(start=(3,1), end=(9999,2), returnas='matrix')
k = len(values_mat)
date_aux = datetime.datetime.strptime(values_mat[k-1][0], '%Y-%m-%d')
date_aux2 = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d')
if date_aux < date_aux2:
cell = 'B' + str(k + 3)
wks.update_value(cell, dominance.dominance)
cell = 'A' + str(k + 3)
wks.update_value(cell, dominance.date)
print('spreadsheet updated')
else:
print('spreadsheet already with the latest data')
return False
return data
# Get latest rank value and update
def update_rank(data=None):
if not(data):
data = get_latest_price('xmr')
if not(data):
print('error updating rank')
return False
else:
rank = Rank()
rank.name = 'xmr'
rank.date = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
rank.rank = int(data['data']['XMR']['cmc_rank'])
rank.save()
gc = pygsheets.authorize(service_file='service_account_credentials.json')
sh = gc.open('zcash_bitcoin')
wks = sh.worksheet_by_title('Sheet8')
values_mat = wks.get_values(start=(3,1), end=(9999,2), returnas='matrix')
k = len(values_mat)
date_aux = datetime.datetime.strptime(values_mat[k-1][0], '%Y-%m-%d')
date_aux2 = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d')
if date_aux < date_aux2:
cell = 'B' + str(k + 3)
wks.update_value(cell, rank.rank)
cell = 'A' + str(k + 3)
wks.update_value(cell, rank.date)
print('spreadsheet updated')
else:
print('spreadsheet already with the latest data')
return data
return data
# Load Reddit api to check if there are new followers
def check_new_social(symbol):
date_now = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
socials = Social.objects.filter(name=symbol).filter(date=date_now)
if not(socials):
print('getting new data')
request = 'https://www.reddit.com/r/'+ symbol +'/about.json'
response = requests.get(request, headers = {'User-agent': 'Checking new social data'})
data = json.loads(response.content)
data = data['data']
subscribers = data['subscribers']
social = Social()
social.name = symbol
social.date = date_now
social.subscriberCount = subscribers
date_aux = date.today()
date_aux = datetime.datetime.strftime(date_aux, '%Y-%m-%d')
date_aux = datetime.datetime.strptime(date_aux, '%Y-%m-%d')
timestamp1 = int(datetime.datetime.timestamp(date_aux))
timestamp2 = int(timestamp1 - 43200)
limit = 1000
filters = []
data = data_prep_posts(symbol, timestamp2, timestamp1, filters, limit)
print(len(data))
social.postsPerHour = len(data)/12
timestamp2 = int(timestamp1 - 3600)
limit = 1000
data = data_prep_comments(symbol, timestamp2, timestamp1, filters, limit)
print(len(data))
social.commentsPerHour = len(data)/1
social.save()
return True
# Update database DailyData with most recent coin data
def update_database(date_from=None, date_to=None):
date_zero = '2014-05-20'
if not(date_from) or not(date_to):
date_to = date.today()
date_from = date_to - timedelta(5)
amount = date_from - datetime.datetime.strptime(date_zero, '%Y-%m-%d')
else:
print(str(date_from) + ' to ' + str(date_to))
date_from = datetime.datetime.strptime(date_from, '%Y-%m-%d')
date_to = datetime.datetime.strptime(date_to, '%Y-%m-%d')
amount = date_from - datetime.datetime.strptime(date_zero, '%Y-%m-%d')
count = 0
date_aux = date_from
while date_aux <= date_to:
date_aux = date_from + timedelta(count)
date_aux2 = date_aux - timedelta(1)
try:
coin_xmr = Coin.objects.filter(name='xmr').get(date=date_aux)
coin_xmr2 = Coin.objects.filter(name='xmr').get(date=date_aux2)
coin_btc = Coin.objects.filter(name='btc').get(date=date_aux)
coin_btc2 = Coin.objects.filter(name='btc').get(date=date_aux2)
try:
coin_dash = Coin.objects.filter(name='dash').get(date=date_aux)
except:
coin_dash = Coin()
try:
coin_zcash = Coin.objects.filter(name='zec').get(date=date_aux)
except:
coin_zcash = Coin()
try:
coin_grin = Coin.objects.filter(name='grin').get(date=date_aux)
except:
coin_grin = Coin()
if coin_btc.inflation == 0 or coin_xmr.inflation == 0:
return count
count_aux = 0
found = False
while count_aux < 100 and not(found):
try:
date_aux2 = date_aux - timedelta(count_aux)
social_btc = Social.objects.filter(name='Bitcoin').get(date=date_aux2)
social_xmr = Social.objects.filter(name='Monero').get(date=date_aux2)
social_crypto = Social.objects.filter(name='CryptoCurrency').get(date=date_aux2)
found = True
except:
count_aux += 1
found = False
except:
return count
try:
data = Sfmodel.objects.get(date=coin_xmr.date)
except:
data = Sfmodel()
data.priceusd = 0
data.pricebtc = 0
data.stocktoflow = 0
data.greyline = 0
data.color = 0
data.date = coin_xmr.date
data.pricebtc = coin_xmr.pricebtc
data.priceusd = coin_xmr.priceusd
if data.stocktoflow == 0 and coin_xmr.supply > 0:
supply = int(coin_xmr.supply)*10**12
reward = (2**64 -1 - supply) >> 19
if reward < 0.6*(10**12):
reward = 0.6*(10**12)
inflation = 100*reward*720*365/supply
data.stocktoflow = (100/(inflation))**1.65
v0 = 0.002
delta = (0.015 - 0.002)/(6*365)
data.color = 30*coin_xmr.pricebtc/((amount.days)*delta + v0)
amount += timedelta(1)
data.save()
try:
data = DailyData.objects.get(date=coin_xmr.date)
except:
data = DailyData()
# Date field
data.date = coin_xmr.date
# Basic information
data.btc_priceusd = 0
data.xmr_priceusd = 0
data.xmr_pricebtc = 0
# Marketcap charts
data.btc_marketcap = 0
data.xmr_marketcap = 0
data.dash_marketcap = 0
data.grin_marketcap = 0
data.zcash_marketcap = 0
# Transactions charts
data.xmr_transacpercentage = 0
data.btc_transactions = 0
data.zcash_transactions = 0
data.dash_transactions = 0
data.grin_transactions = 0
data.xmr_transactions = 0
data.btc_supply = 0
data.xmr_supply = 0
# Issuance charts
data.btc_inflation = 0
data.xmr_inflation = 0
data.dash_inflation = 0
data.grin_inflation = 0
data.zcash_inflation = 0
data.xmr_metcalfebtc = 0
data.xmr_metcalfeusd = 0
data.btc_return = 0
data.xmr_return = 0
data.btc_emissionusd = 0
data.btc_emissionntv = 0
data.xmr_emissionusd = 0
data.xmr_emissionntv = 0
# Mining charts
data.btc_minerrevntv = 0
data.xmr_minerrevntv = 0
data.btc_minerrevusd = 0
data.xmr_minerrevusd = 0
data.btc_minerfeesntv = 0
data.xmr_minerfeesntv = 0
data.btc_minerfeesusd = 0
data.xmr_minerfeesusd = 0
data.btc_transcostntv = 0
data.xmr_transcostntv = 0
data.btc_transcostusd = 0
data.xmr_transcostusd = 0
data.xmr_minerrevcap = 0
data.btc_minerrevcap = 0
data.btc_commitntv = 0
data.xmr_commitntv = 0
data.btc_commitusd = 0
data.xmr_commitusd = 0
data.btc_blocksize = 0
data.xmr_blocksize = 0
data.btc_difficulty = 0
data.xmr_difficulty = 0
# Reddit charts
data.btc_subscriberCount = 0
data.btc_commentsPerHour = 0
data.btc_postsPerHour = 0
data.xmr_subscriberCount = 0
data.xmr_commentsPerHour = 0
data.xmr_postsPerHour = 0
data.crypto_subscriberCount = 0
data.crypto_commentsPerHour = 0
data.crypto_postsPerHour = 0
# Date field
data.date = coin_xmr.date
# Basic information
data.btc_priceusd = coin_btc.priceusd
data.xmr_priceusd = coin_xmr.priceusd
data.xmr_pricebtc = coin_xmr.pricebtc
# Marketcap charts
data.btc_marketcap = coin_btc.priceusd*coin_btc.supply
data.xmr_marketcap = coin_xmr.priceusd*coin_xmr.supply
data.dash_marketcap = coin_dash.priceusd*coin_dash.supply
data.grin_marketcap = coin_grin.priceusd*coin_grin.supply
data.zcash_marketcap = coin_zcash.priceusd*coin_zcash.supply
# Transactions charts
try:
data.xmr_transacpercentage = coin_xmr.transactions/coin_btc.transactions
except:
pass
data.xmr_transactions = coin_xmr.transactions
data.btc_transactions = coin_btc.transactions
data.zcash_transactions = coin_zcash.transactions
data.dash_transactions = coin_dash.transactions
data.grin_transactions = coin_grin.transactions
data.btc_supply = coin_btc.supply
data.xmr_supply = coin_xmr.supply
# Issuance charts
data.btc_inflation = coin_btc.inflation
data.xmr_inflation = coin_xmr.inflation
data.dash_inflation = coin_dash.inflation
data.grin_inflation = coin_grin.inflation
data.zcash_inflation = coin_zcash.inflation
try:
data.xmr_metcalfebtc = coin_xmr.transactions*coin_xmr.supply/(coin_btc.supply*coin_btc.transactions)
data.xmr_metcalfeusd = coin_btc.priceusd*coin_xmr.transactions*coin_xmr.supply/(coin_btc.supply*coin_btc.transactions)
except:
pass
data.btc_return = coin_btc.priceusd/30
data.xmr_return = coin_xmr.priceusd/5.01
data.btc_emissionusd = (coin_btc.supply - coin_btc2.supply)*coin_btc.priceusd
data.btc_emissionntv = coin_btc.supply - coin_btc2.supply
data.xmr_emissionusd = (coin_xmr.supply - coin_xmr2.supply)*coin_xmr.priceusd
data.xmr_emissionntv = coin_xmr.supply - coin_xmr2.supply
# Mining charts
data.btc_minerrevntv = coin_btc.revenue
data.xmr_minerrevntv = coin_xmr.revenue
data.btc_minerrevusd = coin_btc.revenue*coin_btc.priceusd
data.xmr_minerrevusd = coin_xmr.revenue*coin_xmr.priceusd
data.btc_minerfeesntv = coin_btc.revenue - coin_btc.supply + coin_btc2.supply
data.xmr_minerfeesntv = coin_xmr.revenue - coin_xmr.supply + coin_xmr2.supply
data.btc_minerfeesusd = (coin_btc.revenue - coin_btc.supply + coin_btc2.supply)*coin_btc.priceusd
data.xmr_minerfeesusd = (coin_xmr.revenue - coin_xmr.supply + coin_xmr2.supply)*coin_xmr.priceusd
try:
data.btc_transcostntv = coin_btc.fee/coin_btc.transactions
data.xmr_transcostntv = coin_xmr.fee/coin_xmr.transactions
data.btc_transcostusd = coin_btc.priceusd*coin_btc.fee/coin_btc.transactions
data.xmr_transcostusd = coin_xmr.priceusd*coin_xmr.fee/coin_xmr.transactions
except:
pass
try:
data.xmr_minerrevcap = 365*100*coin_xmr.revenue/coin_xmr.supply
data.btc_minerrevcap = 365*100*coin_btc.revenue/coin_btc.supply
except:
pass
try:
data.btc_commitntv = coin_btc.hashrate/(coin_btc.revenue)
data.xmr_commitntv = coin_xmr.hashrate/(coin_xmr.revenue)
data.btc_commitusd = coin_btc.hashrate/(coin_btc.revenue*coin_btc.priceusd)
data.xmr_commitusd = coin_xmr.hashrate/(coin_xmr.revenue*coin_xmr.priceusd)
except:
pass
try:
data.btc_blocksize = coin_btc.blocksize
data.xmr_blocksize = coin_xmr.blocksize
data.btc_difficulty = coin_btc.difficulty
data.xmr_difficulty = coin_xmr.difficulty
except:
pass
# Reddit charts
try:
data.btc_subscriberCount = social_btc.subscriberCount
data.btc_commentsPerHour = social_btc.commentsPerHour
data.btc_postsPerHour = social_btc.postsPerHour
data.xmr_subscriberCount = social_xmr.subscriberCount
data.xmr_commentsPerHour = social_xmr.commentsPerHour
data.xmr_postsPerHour = social_xmr.postsPerHour
data.crypto_subscriberCount = social_crypto.subscriberCount
data.crypto_commentsPerHour = social_crypto.commentsPerHour
data.crypto_postsPerHour = social_crypto.postsPerHour
except:
data.btc_subscriberCount = 0
data.btc_commentsPerHour = 0
data.btc_postsPerHour = 0
data.xmr_subscriberCount = 0
data.xmr_commentsPerHour = 0
data.xmr_postsPerHour = 0
data.crypto_subscriberCount = 0
data.crypto_commentsPerHour = 0
data.crypto_postsPerHour = 0
data.save()
print(str(coin_xmr.date) + ' - ' + str(int(coin_xmr.supply)) + ' xmr @ ' + str(coin_xmr.priceusd) + ' = ' + str(int(data.xmr_marketcap)) + ' => ' + str(coin_xmr.inflation))
count += 1
return count
# Get latest P2Pool data
def update_p2pool():
today = date.today()
yesterday = date.today() - timedelta(1)
try:
p2pool_stat = P2Pool.objects.filter(mini=False).get(date=today)
print('achou p2pool de hoje')
if p2pool_stat.percentage > 0:
print('porcentagem > 0')
update = False
else:
print('porcentagem < 0')
p2pool_stat.delete()
try:
coin = Coin.objects.filter(name='xmr').get(date=yesterday)
print('achou coin de ontem')
if coin.hashrate > 0:
update = True
else:
update = False
except:
print('nao achou coin de ontem')
update = False
except:
print('nao achou p2pool de hoje')
try:
coin = Coin.objects.filter(name='xmr').get(date=yesterday)
if coin.hashrate > 0:
update = True
else:
update = False
except:
update = False
if update:
p2pool_stat = P2Pool()
p2pool_stat.date = today
response = requests.get('https://p2pool.io/api/pool/stats')
data = json.loads(response.text)
p2pool_stat.hashrate = data['pool_statistics']['hashRate']
p2pool_stat.percentage = 100*data['pool_statistics']['hashRate']/coin.hashrate
p2pool_stat.miners = data['pool_statistics']['miners']
p2pool_stat.totalhashes = data['pool_statistics']['totalHashes']
p2pool_stat.totalblocksfound = data['pool_statistics']['totalBlocksFound']
p2pool_stat.mini = False
p2pool_stat.save()
print('p2pool saved!')
gc = pygsheets.authorize(service_file='service_account_credentials.json')
sh = gc.open('zcash_bitcoin')
wks = sh.worksheet_by_title('p2pool')
values_mat = wks.get_values(start=(3,1), end=(9999,3), returnas='matrix')
k = len(values_mat)
date_aux = datetime.datetime.strptime(values_mat[k-1][0], '%Y-%m-%d')
date_aux2 = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d')
if date_aux < date_aux2:
cell = 'F' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalblocksfound)
cell = 'E' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalhashes)
cell = 'D' + str(k + 3)
wks.update_value(cell, p2pool_stat.percentage)
cell = 'C' + str(k + 3)
wks.update_value(cell, p2pool_stat.hashrate)
cell = 'B' + str(k + 3)
wks.update_value(cell, p2pool_stat.miners)
cell = 'A' + str(k + 3)
wks.update_value(cell, datetime.datetime.strftime(p2pool_stat.date, '%Y-%m-%d'))
print('spreadsheet updated')
else:
print('spreadsheet already with the latest data')
return data
today = date.today()
yesterday = date.today() - timedelta(1)
try:
p2pool_stat = P2Pool.objects.filter(mini=True).get(date=today)
print('achou p2pool_mini de hoje')
if p2pool_stat.percentage > 0:
print('porcentagem > 0')
update = False
else:
print('porcentagem < 0')
p2pool_stat.delete()
try:
coin = Coin.objects.filter(name='xmr').get(date=yesterday)
print('achou coin de ontem')
if coin.hashrate > 0:
update = True
else:
update = False
except:
print('nao achou coin de ontem')
update = False
except:
print('nao achou p2pool_mini de hoje')
try:
coin = Coin.objects.filter(name='xmr').get(date=yesterday)
if coin.hashrate > 0:
update = True
else:
update = False
except:
update = False
if update:
p2pool_stat = P2Pool()
p2pool_stat.date = today
response = requests.get('https://p2pool.io/mini/api/pool/stats')
data = json.loads(response.text)
p2pool_stat.hashrate = data['pool_statistics']['hashRate']
p2pool_stat.percentage = 100*data['pool_statistics']['hashRate']/coin.hashrate
p2pool_stat.miners = data['pool_statistics']['miners']
p2pool_stat.totalhashes = data['pool_statistics']['totalHashes']
p2pool_stat.totalblocksfound = data['pool_statistics']['totalBlocksFound']
p2pool_stat.mini = True
p2pool_stat.save()
print('p2pool_mini saved!')
gc = pygsheets.authorize(service_file='service_account_credentials.json')
sh = gc.open('zcash_bitcoin')
wks = sh.worksheet_by_title('p2poolmini')
values_mat = wks.get_values(start=(3,1), end=(9999,3), returnas='matrix')
k = len(values_mat)
date_aux = datetime.datetime.strptime(values_mat[k-1][0], '%Y-%m-%d')
date_aux2 = datetime.datetime.strftime(date.today(), '%Y-%m-%d')
date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d')
if date_aux < date_aux2:
cell = 'F' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalblocksfound)
cell = 'E' + str(k + 3)
wks.update_value(cell, p2pool_stat.totalhashes)
cell = 'D' + str(k + 3)
wks.update_value(cell, p2pool_stat.percentage)
cell = 'C' + str(k + 3)
wks.update_value(cell, p2pool_stat.hashrate)
cell = 'B' + str(k + 3)
wks.update_value(cell, p2pool_stat.miners)
cell = 'A' + str(k + 3)
wks.update_value(cell, datetime.datetime.strftime(p2pool_stat.date, '%Y-%m-%d'))
print('spreadsheet updated')
else:
print('spreadsheet already with the latest data')
return data
return True

File diff suppressed because it is too large Load diff