2022-09-17 03:04:08 +00:00
|
|
|
import pandas as pd
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import datetime
|
|
|
|
from datetime import date, timedelta
|
2024-11-12 13:09:33 +00:00
|
|
|
from .models import (
|
|
|
|
Coin,
|
|
|
|
Social,
|
|
|
|
P2Pool,
|
|
|
|
Dominance,
|
|
|
|
Rank,
|
|
|
|
Sfmodel,
|
|
|
|
DailyData,
|
|
|
|
Withdrawal,
|
|
|
|
)
|
2022-09-17 03:04:08 +00:00
|
|
|
from requests import Session
|
|
|
|
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
|
2022-12-25 18:47:41 +00:00
|
|
|
import pytz
|
2024-11-13 11:05:11 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
DATA_FILE = settings.DATA_FILE
|
|
|
|
BASE_DIR = settings.BASE_DIR
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
####################################################################################
|
|
|
|
# Reddit api
|
|
|
|
####################################################################################
|
2024-11-12 13:40:05 +00:00
|
|
|
# TODO: Fix this...
|
2024-11-12 13:09:33 +00:00
|
|
|
# api = PushshiftAPI() # When it's working
|
|
|
|
api = False # When it's not
|
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
# Get daily post on Reddit
|
|
|
|
def data_prep_posts(subreddit, start_time, end_time, filters, limit):
|
2024-11-12 13:09:33 +00:00
|
|
|
if len(filters) == 0:
|
|
|
|
filters = [
|
|
|
|
"id",
|
|
|
|
"author",
|
|
|
|
"created_utc",
|
|
|
|
"domain",
|
|
|
|
"url",
|
|
|
|
"title",
|
|
|
|
"num_comments",
|
|
|
|
]
|
|
|
|
|
|
|
|
if api:
|
|
|
|
posts = list(
|
|
|
|
api.search_submissions(
|
|
|
|
subreddit=subreddit,
|
|
|
|
after=start_time,
|
|
|
|
before=end_time,
|
|
|
|
filter=filters,
|
|
|
|
limit=limit,
|
|
|
|
)
|
|
|
|
)
|
2022-10-16 06:10:48 +00:00
|
|
|
return pd.DataFrame(posts)
|
|
|
|
return 0
|
2022-09-17 03:04:08 +00:00
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Get daily comments on Reddit
|
|
|
|
def data_prep_comments(term, start_time, end_time, filters, limit):
|
2024-11-12 13:09:33 +00:00
|
|
|
if len(filters) == 0:
|
|
|
|
filters = ["id", "author", "created_utc", "body", "permalink", "subreddit"]
|
|
|
|
|
|
|
|
if api:
|
|
|
|
comments = list(
|
|
|
|
api.search_comments(
|
|
|
|
q=term, after=start_time, before=end_time, filter=filters, limit=limit
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return pd.DataFrame(comments)
|
2022-10-16 06:10:48 +00:00
|
|
|
return 0
|
2022-09-17 03:04:08 +00:00
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
####################################################################################
|
2024-11-12 13:09:33 +00:00
|
|
|
# Other useful functions
|
2022-09-17 03:04:08 +00:00
|
|
|
####################################################################################
|
2024-11-12 13:09:33 +00:00
|
|
|
# Get data from a coin
|
2023-10-04 01:00:29 +00:00
|
|
|
def get_history_function(symbol, start_time=None, end_time=None):
|
|
|
|
update = True
|
|
|
|
count = 0
|
|
|
|
priceusd = 0
|
|
|
|
inflation = 0
|
|
|
|
pricebtc = 0
|
|
|
|
stocktoflow = 0
|
|
|
|
supply = 0
|
|
|
|
fee = 0
|
|
|
|
revenue = 0
|
|
|
|
hashrate = 0
|
|
|
|
transactions = 0
|
|
|
|
blocksize = 0
|
|
|
|
difficulty = 0
|
|
|
|
|
2024-11-13 11:05:11 +00:00
|
|
|
with open(BASE_DIR / "settings.json") as file:
|
2023-10-04 01:00:29 +00:00
|
|
|
data = json.load(file)
|
|
|
|
file.close()
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
if not (start_time and end_time):
|
|
|
|
start_time = "2000-01-01"
|
|
|
|
end_time = "2100-01-01"
|
|
|
|
|
|
|
|
url = (
|
|
|
|
data["metrics_provider"][0]["metrics_url_new"]
|
|
|
|
+ symbol
|
|
|
|
+ "/"
|
|
|
|
+ start_time
|
|
|
|
+ "/"
|
|
|
|
+ end_time
|
|
|
|
)
|
2023-10-04 01:00:29 +00:00
|
|
|
print(url)
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
coins = Coin.objects.filter(name=symbol).order_by("-date")
|
2023-10-04 01:00:29 +00:00
|
|
|
for coin in coins:
|
|
|
|
if coin.supply > 0:
|
|
|
|
supply = coin.supply
|
|
|
|
break
|
|
|
|
for coin in coins:
|
|
|
|
if coin.inflation > 0:
|
|
|
|
inflation = coin.inflation
|
|
|
|
break
|
|
|
|
for coin in coins:
|
|
|
|
if coin.hashrate > 0:
|
|
|
|
hashrate = coin.hashrate
|
|
|
|
break
|
|
|
|
for coin in coins:
|
|
|
|
if coin.transactions > 0:
|
|
|
|
transactions = coin.transactions
|
|
|
|
break
|
|
|
|
for coin in coins:
|
|
|
|
if coin.priceusd > 0:
|
|
|
|
priceusd = coin.priceusd
|
|
|
|
break
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
while update:
|
2023-10-04 01:00:29 +00:00
|
|
|
response = requests.get(url)
|
|
|
|
data_aux = json.loads(response.text)
|
2024-11-12 13:09:33 +00:00
|
|
|
data_aux2 = data_aux["data"]
|
2023-10-04 01:00:29 +00:00
|
|
|
for item in data_aux2:
|
2024-11-12 13:09:33 +00:00
|
|
|
day, hour = str(item["time"]).split("T")
|
|
|
|
day = datetime.datetime.strptime(day, "%Y-%m-%d")
|
|
|
|
day = datetime.datetime.strftime(day, "%Y-%m-%d")
|
2023-10-04 01:00:29 +00:00
|
|
|
coin = Coin.objects.filter(name=symbol).filter(date=day)
|
|
|
|
if coin:
|
|
|
|
coin.delete()
|
|
|
|
try:
|
|
|
|
coin = Coin()
|
|
|
|
coin.name = symbol
|
|
|
|
coin.date = day
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.priceusd = float(item["PriceUSD"])
|
2023-10-04 01:00:29 +00:00
|
|
|
priceusd = coin.priceusd
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.priceusd = priceusd
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.pricebtc = float(item["PriceBTC"])
|
2023-10-04 01:00:29 +00:00
|
|
|
pricebtc = coin.pricebtc
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.pricebtc = pricebtc
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.inflation = float(item["IssContPctAnn"])
|
|
|
|
coin.stocktoflow = (100 / coin.inflation) ** 1.65
|
2023-10-04 01:00:29 +00:00
|
|
|
inflation = coin.inflation
|
|
|
|
stocktoflow = coin.stocktoflow
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.inflation = inflation
|
|
|
|
coin.stocktoflow = stocktoflow
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
if symbol == "xmr":
|
|
|
|
if float(item["SplyCur"]) < 18000000:
|
|
|
|
coin.supply = float(item["SplyCur"]) + 497108
|
2023-10-04 01:00:29 +00:00
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.supply = float(item["SplyCur"])
|
2023-10-04 01:00:29 +00:00
|
|
|
supply = coin.supply
|
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.supply = float(item["SplyCur"])
|
2023-10-04 01:00:29 +00:00
|
|
|
supply = coin.supply
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.supply = supply
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.fee = float(item["FeeTotNtv"])
|
2023-10-04 01:00:29 +00:00
|
|
|
fee = coin.fee
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.fee = fee
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.revenue = float(item["RevNtv"])
|
2023-10-04 01:00:29 +00:00
|
|
|
revenue = coin.revenue
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.revenue = revenue
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.hashrate = float(item["HashRate"])
|
2023-10-04 01:00:29 +00:00
|
|
|
hashrate = coin.hashrate
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.hashrate = hashrate
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.transactions = float(item["TxCnt"])
|
2023-10-04 01:00:29 +00:00
|
|
|
transactions = coin.transactions
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.transactions = transactions
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.blocksize = float(item["BlkSizeMeanByte"])
|
2023-10-04 01:00:29 +00:00
|
|
|
blocksize = coin.blocksize
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.blocksize = blocksize
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.difficulty = float(item["DiffLast"])
|
2023-10-04 01:00:29 +00:00
|
|
|
difficulty = coin.difficulty
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
coin.difficulty = difficulty
|
|
|
|
coin.save()
|
|
|
|
count += 1
|
2024-11-12 13:09:33 +00:00
|
|
|
print(coin.name + " " + str(coin.date) + " " + str(item["SplyCur"]))
|
2023-10-04 01:00:29 +00:00
|
|
|
|
2024-11-13 11:05:11 +00:00
|
|
|
except (ValueError, KeyError, TypeError):
|
2023-10-04 01:00:29 +00:00
|
|
|
pass
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
url = (
|
|
|
|
data["metrics_provider"][0]["metrics_url_new"]
|
|
|
|
+ symbol
|
|
|
|
+ "/"
|
|
|
|
+ start_time
|
|
|
|
+ "/"
|
|
|
|
+ end_time
|
|
|
|
+ "/"
|
|
|
|
+ data_aux["next_page_token"]
|
|
|
|
)
|
|
|
|
except (ValueError, KeyError):
|
2023-10-04 01:00:29 +00:00
|
|
|
update = False
|
|
|
|
break
|
|
|
|
return count
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Get most recent metrics from a data provider of your choice for 'symbol'
|
|
|
|
def get_latest_metrics(symbol, url):
|
|
|
|
update = True
|
|
|
|
count = 0
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
while update:
|
2022-09-17 03:04:08 +00:00
|
|
|
response = requests.get(url)
|
|
|
|
data = json.loads(response.text)
|
2024-11-12 13:09:33 +00:00
|
|
|
data_aux = data["data"]
|
2022-09-17 03:04:08 +00:00
|
|
|
for item in data_aux:
|
2024-11-12 13:09:33 +00:00
|
|
|
day, hour = str(item["time"]).split("T")
|
|
|
|
day = datetime.datetime.strptime(day, "%Y-%m-%d")
|
|
|
|
day = datetime.datetime.strftime(day, "%Y-%m-%d")
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
|
|
|
coin = Coin.objects.filter(name=symbol).get(date=day)
|
2024-11-12 13:09:33 +00:00
|
|
|
except Coin.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
coin = Coin()
|
|
|
|
|
|
|
|
try:
|
|
|
|
coin.name = symbol
|
|
|
|
coin.date = day
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.priceusd = float(item["PriceUSD"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.priceusd = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.pricebtc = float(item["PriceBTC"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.pricebtc = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.inflation = float(item["IssContPctAnn"])
|
|
|
|
coin.stocktoflow = (100 / coin.inflation) ** 1.65
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.inflation = 0
|
|
|
|
coin.stocktoflow = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.supply = float(item["SplyCur"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.supply = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.fee = float(item["FeeTotNtv"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.fee = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.revenue = float(item["RevNtv"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.revenue = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.hashrate = float(item["HashRate"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.hashrate = 0
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin.transactions = float(item["TxCnt"])
|
|
|
|
except (ValueError, KeyError):
|
2022-09-17 03:04:08 +00:00
|
|
|
coin.transactions = 0
|
|
|
|
|
|
|
|
coin.save()
|
|
|
|
count += 1
|
2024-11-12 13:09:33 +00:00
|
|
|
print(str(symbol) + " " + str(coin.date))
|
2022-09-17 03:04:08 +00:00
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
except Exception:
|
2022-09-17 03:04:08 +00:00
|
|
|
pass
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
url = data["next_page_url"]
|
2022-09-17 03:04:08 +00:00
|
|
|
update = True
|
2024-11-12 13:09:33 +00:00
|
|
|
except KeyError:
|
2022-09-17 03:04:08 +00:00
|
|
|
update = False
|
2024-11-12 13:09:33 +00:00
|
|
|
break
|
2022-09-17 03:04:08 +00:00
|
|
|
return count
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-12-25 17:59:02 +00:00
|
|
|
# Get binance withdrawal state
|
|
|
|
def get_binance_withdrawal(symbol):
|
2024-11-12 13:09:33 +00:00
|
|
|
url = "https://www.binance.com/en/network"
|
|
|
|
|
|
|
|
withdrawals = Withdrawal.objects.order_by("-date")
|
2022-12-25 17:59:02 +00:00
|
|
|
if len(withdrawals) > 0:
|
|
|
|
for withdrawal in withdrawals:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
withdrawal = Withdrawal()
|
|
|
|
withdrawal.state = True
|
|
|
|
withdrawal.save()
|
|
|
|
return True
|
|
|
|
|
2022-12-25 18:47:41 +00:00
|
|
|
current_date = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
|
2022-12-25 17:59:02 +00:00
|
|
|
response = requests.get(url)
|
|
|
|
result = response.text
|
|
|
|
position = result.find(symbol)
|
2024-11-12 13:09:33 +00:00
|
|
|
result = result[position : position + 400]
|
|
|
|
position = result.find("withdrawEnable")
|
|
|
|
result = result[position : position + 25]
|
2022-12-25 17:59:02 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
result.index("true")
|
2022-12-25 18:47:41 +00:00
|
|
|
if (current_date - withdrawal.date).seconds > 3600:
|
2022-12-25 17:59:02 +00:00
|
|
|
new_withdrawal = Withdrawal()
|
|
|
|
new_withdrawal.state = True
|
|
|
|
new_withdrawal.save()
|
|
|
|
return True
|
2024-11-12 13:09:33 +00:00
|
|
|
except Exception:
|
2022-12-25 17:59:02 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
result.index("false")
|
2022-12-26 19:37:25 +00:00
|
|
|
if ((current_date - withdrawal.date).seconds > 3600) or withdrawal.state:
|
2022-12-25 17:59:02 +00:00
|
|
|
new_withdrawal = Withdrawal()
|
|
|
|
new_withdrawal.state = False
|
|
|
|
new_withdrawal.save()
|
|
|
|
return False
|
2024-11-12 13:09:33 +00:00
|
|
|
except Exception:
|
2022-12-25 17:59:02 +00:00
|
|
|
return None
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Get latest price data for Monero
|
|
|
|
def get_latest_price(symbol):
|
2024-11-13 11:05:11 +00:00
|
|
|
with open(BASE_DIR / "settings.json") as file:
|
2022-09-17 03:04:08 +00:00
|
|
|
data = json.load(file)
|
|
|
|
|
|
|
|
url = data["metrics_provider"][0]["price_url_old"] + symbol
|
|
|
|
print(url)
|
|
|
|
parameters = {
|
2024-11-12 13:09:33 +00:00
|
|
|
"convert": "USD",
|
2022-09-17 03:04:08 +00:00
|
|
|
}
|
|
|
|
headers = {
|
2024-11-12 13:09:33 +00:00
|
|
|
"Accepts": "application/json",
|
|
|
|
data["metrics_provider"][0]["api_key_name"]: data["metrics_provider"][0][
|
|
|
|
"api_key_value"
|
|
|
|
],
|
2022-09-17 03:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session = Session()
|
|
|
|
session.headers.update(headers)
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = session.get(url, params=parameters)
|
|
|
|
data = json.loads(response.text)
|
2024-11-12 13:09:33 +00:00
|
|
|
print("getting latest data")
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
if data["data"][symbol.upper()]["cmc_rank"]:
|
|
|
|
print("new data received")
|
2022-09-17 03:04:08 +00:00
|
|
|
pass
|
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
print("problem with the data provider")
|
2022-09-17 03:04:08 +00:00
|
|
|
data = False
|
2024-11-12 13:09:33 +00:00
|
|
|
except KeyError:
|
2022-09-17 03:04:08 +00:00
|
|
|
data = False
|
2024-11-12 13:09:33 +00:00
|
|
|
except (ConnectionError, Timeout, TooManyRedirects):
|
2022-09-17 03:04:08 +00:00
|
|
|
data = False
|
|
|
|
|
|
|
|
file.close()
|
|
|
|
return data
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Get latest dominance value and update
|
|
|
|
def update_dominance(data):
|
2024-11-12 13:09:33 +00:00
|
|
|
if not (data):
|
|
|
|
print("error updating dominance")
|
2022-09-17 03:04:08 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
dominance = Dominance()
|
2024-11-12 13:09:33 +00:00
|
|
|
dominance.name = "xmr"
|
|
|
|
dominance.date = datetime.datetime.strftime(date.today(), "%Y-%m-%d")
|
|
|
|
dominance.dominance = float(
|
|
|
|
data["data"]["XMR"]["quote"]["USD"]["market_cap_dominance"]
|
|
|
|
)
|
2022-09-17 03:04:08 +00:00
|
|
|
dominance.save()
|
|
|
|
|
2024-11-13 11:55:17 +00:00
|
|
|
df = pd.read_excel(DATA_FILE, engine="odf", sheet_name="Sheet7")
|
|
|
|
start_row, end_row = 2, 9999
|
|
|
|
start_col, end_col = 0, 2
|
|
|
|
values_mat = df.iloc[start_row:end_row, start_col:end_col].to_numpy()
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
k = len(values_mat)
|
2024-11-12 13:09:33 +00:00
|
|
|
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")
|
2022-09-17 03:04:08 +00:00
|
|
|
if date_aux < date_aux2:
|
2024-11-13 11:55:17 +00:00
|
|
|
values_mat[k][1] = dominance.dominance
|
|
|
|
values_mat[k][0] = dominance.date
|
2024-11-13 15:17:46 +00:00
|
|
|
|
2024-11-13 11:55:17 +00:00
|
|
|
df.iloc[start_row:end_row, start_col:end_col] = values_mat
|
|
|
|
df.to_excel(DATA_FILE, sheet_name="Sheet7", index=False)
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
print("spreadsheet updated")
|
2022-09-17 03:04:08 +00:00
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
print("spreadsheet already with the latest data")
|
2022-09-17 03:04:08 +00:00
|
|
|
return False
|
|
|
|
return data
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Get latest rank value and update
|
|
|
|
def update_rank(data=None):
|
2024-11-12 13:09:33 +00:00
|
|
|
if not (data):
|
|
|
|
data = get_latest_price("xmr")
|
|
|
|
if not (data):
|
|
|
|
print("error updating rank")
|
2022-09-17 03:04:08 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
rank = Rank()
|
2024-11-12 13:09:33 +00:00
|
|
|
rank.name = "xmr"
|
|
|
|
rank.date = datetime.datetime.strftime(date.today(), "%Y-%m-%d")
|
|
|
|
rank.rank = int(data["data"]["XMR"]["cmc_rank"])
|
2022-09-17 03:04:08 +00:00
|
|
|
rank.save()
|
|
|
|
|
2024-11-13 11:55:17 +00:00
|
|
|
df = pd.read_excel(DATA_FILE, engine="odf", sheet_name="Sheet8")
|
|
|
|
start_row, end_row = 2, 9999
|
|
|
|
start_col, end_col = 0, 2
|
|
|
|
values_mat = df.iloc[start_row:end_row, start_col:end_col].to_numpy()
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
k = len(values_mat)
|
2024-11-12 13:09:33 +00:00
|
|
|
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")
|
2022-09-17 03:04:08 +00:00
|
|
|
if date_aux < date_aux2:
|
2024-11-13 11:55:17 +00:00
|
|
|
values_mat[k][1] = rank.rank
|
|
|
|
values_mat[k][0] = rank.date
|
2024-11-13 15:17:46 +00:00
|
|
|
|
2024-11-13 11:55:17 +00:00
|
|
|
df.iloc[start_row:end_row, start_col:end_col] = values_mat
|
|
|
|
df.to_excel(DATA_FILE, sheet_name="Sheet8", index=False)
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
print("spreadsheet updated")
|
2022-09-17 03:04:08 +00:00
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
print("spreadsheet already with the latest data")
|
2022-09-17 03:04:08 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Load Reddit api to check if there are new followers
|
|
|
|
def check_new_social(symbol):
|
2024-11-12 13:09:33 +00:00
|
|
|
yesterday = datetime.datetime.strftime(date.today() - timedelta(1), "%Y-%m-%d")
|
2022-09-23 20:38:20 +00:00
|
|
|
socials = Social.objects.filter(name=symbol).filter(date=yesterday)
|
2024-11-12 13:09:33 +00:00
|
|
|
if symbol == "Bitcoin":
|
2023-10-01 04:57:53 +00:00
|
|
|
timeframe = 14400
|
2024-11-12 13:09:33 +00:00
|
|
|
hours = 86400 / timeframe
|
2023-10-01 04:57:53 +00:00
|
|
|
timeframe2 = 3600
|
2024-11-12 13:09:33 +00:00
|
|
|
hours2 = 86400 / timeframe2
|
|
|
|
elif symbol == "Monero":
|
2023-10-01 04:57:53 +00:00
|
|
|
timeframe = 43200
|
2024-11-12 13:09:33 +00:00
|
|
|
hours = 86400 / timeframe
|
2023-10-01 04:57:53 +00:00
|
|
|
timeframe2 = 43200
|
2024-11-12 13:09:33 +00:00
|
|
|
hours2 = 86400 / timeframe2
|
|
|
|
elif symbol == "Cryptocurrency":
|
2023-10-01 04:57:53 +00:00
|
|
|
timeframe = 14400
|
2024-11-12 13:09:33 +00:00
|
|
|
hours = 86400 / timeframe
|
2023-10-01 04:57:53 +00:00
|
|
|
timeframe2 = 1800
|
2024-11-12 13:09:33 +00:00
|
|
|
hours2 = 86400 / timeframe2
|
|
|
|
|
|
|
|
if not (socials):
|
|
|
|
print("new social")
|
|
|
|
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"]
|
2022-09-17 03:04:08 +00:00
|
|
|
social = Social()
|
|
|
|
social.name = symbol
|
2022-09-23 20:38:20 +00:00
|
|
|
social.date = yesterday
|
2022-09-17 03:04:08 +00:00
|
|
|
social.subscriberCount = subscribers
|
|
|
|
|
|
|
|
date_aux = date.today()
|
2024-11-12 13:09:33 +00:00
|
|
|
date_aux = datetime.datetime.strftime(date_aux, "%Y-%m-%d")
|
|
|
|
date_aux = datetime.datetime.strptime(date_aux, "%Y-%m-%d")
|
2022-09-17 03:04:08 +00:00
|
|
|
timestamp1 = int(datetime.datetime.timestamp(date_aux))
|
|
|
|
|
2023-10-01 04:57:53 +00:00
|
|
|
timestamp2 = int(timestamp1 - timeframe)
|
2022-09-17 03:04:08 +00:00
|
|
|
limit = 1000
|
|
|
|
filters = []
|
|
|
|
data = data_prep_posts(symbol, timestamp2, timestamp1, filters, limit)
|
2022-10-16 06:10:48 +00:00
|
|
|
if data != 0:
|
2024-11-12 13:09:33 +00:00
|
|
|
social.postsPerHour = len(data) / hours
|
2022-10-16 06:10:48 +00:00
|
|
|
else:
|
|
|
|
social.postsPerHour = 0
|
2022-09-17 03:04:08 +00:00
|
|
|
|
2023-10-01 04:57:53 +00:00
|
|
|
timestamp2 = int(timestamp1 - timeframe2)
|
2022-09-17 03:04:08 +00:00
|
|
|
limit = 1000
|
|
|
|
data = data_prep_comments(symbol, timestamp2, timestamp1, filters, limit)
|
2022-10-16 06:10:48 +00:00
|
|
|
if data != 0:
|
2024-11-12 13:09:33 +00:00
|
|
|
social.commentsPerHour = len(data) / hours2
|
2022-10-16 06:10:48 +00:00
|
|
|
else:
|
|
|
|
social.commentsPerHour = 0
|
2022-09-17 03:04:08 +00:00
|
|
|
social.save()
|
2024-11-12 13:09:33 +00:00
|
|
|
print("getting new data - " + str(social.name) + " - " + str(social.date))
|
2022-09-17 03:04:08 +00:00
|
|
|
return True
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# Update database DailyData with most recent coin data
|
|
|
|
def update_database(date_from=None, date_to=None):
|
2024-11-12 13:09:33 +00:00
|
|
|
date_zero = "2014-05-20"
|
2022-09-17 03:04:08 +00:00
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
if not (date_from) or not (date_to):
|
2022-09-17 03:04:08 +00:00
|
|
|
date_to = date.today()
|
|
|
|
date_from = date_to - timedelta(5)
|
2024-11-13 15:10:27 +00:00
|
|
|
amount = date_from - datetime.datetime.strptime(date_zero, "%Y-%m-%d").date()
|
2022-09-17 03:04:08 +00:00
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
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")
|
2024-11-13 15:10:27 +00:00
|
|
|
amount = date_from - datetime.datetime.strptime(date_zero, "%Y-%m-%d").date()
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
date_aux = date_from
|
|
|
|
while date_aux <= date_to:
|
|
|
|
date_aux = date_from + timedelta(count)
|
|
|
|
date_aux2 = date_aux - timedelta(1)
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
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)
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin_dash = Coin.objects.filter(name="dash").get(date=date_aux)
|
|
|
|
except Coin.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
coin_dash = Coin()
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin_zcash = Coin.objects.filter(name="zec").get(date=date_aux)
|
|
|
|
except Coin.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
coin_zcash = Coin()
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin_grin = Coin.objects.filter(name="grin").get(date=date_aux)
|
|
|
|
except Coin.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
coin_grin = Coin()
|
|
|
|
|
|
|
|
if coin_btc.inflation == 0 or coin_xmr.inflation == 0:
|
|
|
|
return count
|
|
|
|
|
|
|
|
count_aux = 0
|
|
|
|
found = False
|
2024-11-12 13:09:33 +00:00
|
|
|
while count_aux < 100 and not (found):
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
2022-09-23 20:38:20 +00:00
|
|
|
date_aux3 = date_aux - timedelta(count_aux)
|
2024-11-12 13:09:33 +00:00
|
|
|
social_btc = Social.objects.filter(name="Bitcoin").get(
|
|
|
|
date=date_aux3
|
|
|
|
)
|
|
|
|
social_xmr = Social.objects.filter(name="Monero").get(
|
|
|
|
date=date_aux3
|
|
|
|
)
|
|
|
|
social_crypto = Social.objects.filter(name="Cryptocurrency").get(
|
|
|
|
date=date_aux3
|
|
|
|
)
|
2022-09-17 03:04:08 +00:00
|
|
|
found = True
|
2024-11-12 13:09:33 +00:00
|
|
|
except Social.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
found = False
|
2022-09-23 20:38:20 +00:00
|
|
|
count_aux += 1
|
2024-11-12 13:09:33 +00:00
|
|
|
except Coin.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
return count
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = Sfmodel.objects.get(date=coin_xmr.date)
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
except Sfmodel.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
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:
|
2024-11-12 13:09:33 +00:00
|
|
|
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
|
2022-09-17 03:04:08 +00:00
|
|
|
v0 = 0.002
|
2024-11-12 13:09:33 +00:00
|
|
|
delta = (0.015 - 0.002) / (6 * 365)
|
|
|
|
data.color = 30 * coin_xmr.pricebtc / ((amount.days) * delta + v0)
|
2022-09-17 03:04:08 +00:00
|
|
|
amount += timedelta(1)
|
|
|
|
data.save()
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = DailyData.objects.get(date=coin_xmr.date)
|
2024-11-12 13:09:33 +00:00
|
|
|
except DailyData.DoesNotExist:
|
2022-09-17 03:04:08 +00:00
|
|
|
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
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# 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
|
2024-11-12 13:09:33 +00:00
|
|
|
data.btc_marketcap = float(coin_btc.priceusd) * float(coin_btc.supply)
|
|
|
|
data.xmr_marketcap = float(coin_xmr.priceusd) * float(coin_xmr.supply)
|
|
|
|
data.dash_marketcap = float(coin_dash.priceusd) * float(coin_dash.supply)
|
|
|
|
data.grin_marketcap = float(coin_grin.priceusd) * float(coin_grin.supply)
|
|
|
|
data.zcash_marketcap = float(coin_zcash.priceusd) * float(coin_zcash.supply)
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
# Transactions charts
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
data.xmr_transacpercentage = coin_xmr.transactions / coin_btc.transactions
|
|
|
|
except ZeroDivisionError:
|
2022-09-17 03:04:08 +00:00
|
|
|
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:
|
2024-11-12 13:09:33 +00:00
|
|
|
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 ZeroDivisionError:
|
2022-09-17 03:04:08 +00:00
|
|
|
pass
|
2024-11-12 13:09:33 +00:00
|
|
|
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
|
2022-09-17 03:04:08 +00:00
|
|
|
data.btc_emissionntv = coin_btc.supply - coin_btc2.supply
|
2024-11-12 13:09:33 +00:00
|
|
|
data.xmr_emissionusd = (coin_xmr.supply - coin_xmr2.supply) * coin_xmr.priceusd
|
2022-09-17 03:04:08 +00:00
|
|
|
data.xmr_emissionntv = coin_xmr.supply - coin_xmr2.supply
|
|
|
|
# Mining charts
|
|
|
|
data.btc_minerrevntv = coin_btc.revenue
|
|
|
|
data.xmr_minerrevntv = coin_xmr.revenue
|
2024-11-12 13:09:33 +00:00
|
|
|
data.btc_minerrevusd = coin_btc.revenue * coin_btc.priceusd
|
|
|
|
data.xmr_minerrevusd = coin_xmr.revenue * coin_xmr.priceusd
|
2022-09-17 03:04:08 +00:00
|
|
|
data.btc_minerfeesntv = coin_btc.revenue - coin_btc.supply + coin_btc2.supply
|
|
|
|
data.xmr_minerfeesntv = coin_xmr.revenue - coin_xmr.supply + coin_xmr2.supply
|
2024-11-12 13:09:33 +00:00
|
|
|
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
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
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 ZeroDivisionError:
|
2022-09-17 03:04:08 +00:00
|
|
|
pass
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
data.xmr_minerrevcap = 365 * 100 * coin_xmr.revenue / coin_xmr.supply
|
|
|
|
data.btc_minerrevcap = 365 * 100 * coin_btc.revenue / coin_btc.supply
|
|
|
|
except ZeroDivisionError:
|
2022-09-17 03:04:08 +00:00
|
|
|
pass
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
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 ZeroDivisionError:
|
2022-09-17 03:04:08 +00:00
|
|
|
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
|
2024-11-12 13:09:33 +00:00
|
|
|
except ZeroDivisionError:
|
2022-09-17 03:04:08 +00:00
|
|
|
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
|
2024-11-13 15:17:46 +00:00
|
|
|
except (Social.DoesNotExist, UnboundLocalError):
|
2022-09-17 03:04:08 +00:00
|
|
|
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()
|
2024-11-13 15:18:33 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
print(
|
|
|
|
str(coin_xmr.supply)
|
|
|
|
+ " xmr "
|
|
|
|
+ str(data.xmr_subscriberCount)
|
|
|
|
+ " - "
|
|
|
|
+ str(social_xmr.subscriberCount)
|
|
|
|
+ " = "
|
|
|
|
+ str(int(data.xmr_marketcap))
|
|
|
|
+ " => "
|
|
|
|
+ str(coin_xmr.inflation)
|
|
|
|
)
|
|
|
|
except (Social.DoesNotExist, UnboundLocalError):
|
|
|
|
pass
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
return count
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
# 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)
|
2024-11-12 13:09:33 +00:00
|
|
|
print("achou p2pool de hoje")
|
2022-09-17 03:04:08 +00:00
|
|
|
if p2pool_stat.percentage > 0:
|
2024-11-12 13:09:33 +00:00
|
|
|
print("porcentagem > 0")
|
|
|
|
update = False
|
2022-09-17 03:04:08 +00:00
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
print("porcentagem < 0")
|
2022-09-17 03:04:08 +00:00
|
|
|
p2pool_stat.delete()
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin = Coin.objects.filter(name="xmr").get(date=yesterday)
|
|
|
|
print("achou coin de ontem")
|
2022-09-17 03:04:08 +00:00
|
|
|
if coin.hashrate > 0:
|
|
|
|
update = True
|
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
update = False
|
|
|
|
except Coin.DoesNotExist:
|
|
|
|
print("nao achou coin de ontem")
|
|
|
|
update = False
|
|
|
|
except P2Pool.DoesNotExist:
|
|
|
|
print("nao achou p2pool de hoje")
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin = Coin.objects.filter(name="xmr").get(date=yesterday)
|
2022-09-17 03:04:08 +00:00
|
|
|
if coin.hashrate > 0:
|
|
|
|
update = True
|
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
update = False
|
|
|
|
except Coin.DoesNotExist:
|
|
|
|
update = False
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
if update:
|
|
|
|
p2pool_stat = P2Pool()
|
|
|
|
p2pool_stat.date = today
|
2024-11-12 13:09:33 +00:00
|
|
|
response = requests.get("https://p2pool.io/api/pool/stats")
|
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
data = json.loads(response.text)
|
2024-11-12 13:09:33 +00:00
|
|
|
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"]
|
2022-09-17 03:04:08 +00:00
|
|
|
p2pool_stat.mini = False
|
|
|
|
p2pool_stat.save()
|
2024-11-12 13:09:33 +00:00
|
|
|
print("p2pool saved!")
|
|
|
|
|
2024-11-13 11:05:11 +00:00
|
|
|
df = pd.read_excel(DATA_FILE, engine="odf", sheet_name="p2pool")
|
|
|
|
start_row, end_row = 2, 9999
|
|
|
|
start_col, end_col = 0, 2
|
|
|
|
values_mat = df.iloc[start_row:end_row, start_col:end_col].to_numpy()
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
k = len(values_mat)
|
2024-11-12 13:09:33 +00:00
|
|
|
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")
|
2022-09-17 03:04:08 +00:00
|
|
|
if date_aux < date_aux2:
|
2024-11-13 11:05:11 +00:00
|
|
|
values_mat[k][5] = p2pool_stat.totalblocksfound
|
|
|
|
values_mat[k][4] = p2pool_stat.totalhashes
|
|
|
|
values_mat[k][3] = p2pool_stat.percentage
|
|
|
|
values_mat[k][2] = p2pool_stat.hashrate
|
|
|
|
values_mat[k][1] = p2pool_stat.miners
|
|
|
|
values_mat[k][0] = datetime.datetime.strftime(p2pool_stat.date, "%Y-%m-%d")
|
|
|
|
|
|
|
|
df.iloc[start_row:end_row, start_col:end_col] = values_mat
|
|
|
|
df.to_excel(DATA_FILE, sheet_name="p2pool", index=False)
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
print("spreadsheet updated")
|
|
|
|
else:
|
|
|
|
print("spreadsheet already with the latest data")
|
2022-09-17 03:04:08 +00:00
|
|
|
return data
|
2024-11-12 13:09:33 +00:00
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
today = date.today()
|
|
|
|
yesterday = date.today() - timedelta(1)
|
|
|
|
try:
|
|
|
|
p2pool_stat = P2Pool.objects.filter(mini=True).get(date=today)
|
2024-11-12 13:09:33 +00:00
|
|
|
print("achou p2pool_mini de hoje")
|
2022-09-17 03:04:08 +00:00
|
|
|
if p2pool_stat.percentage > 0:
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
print("porcentagem > 0")
|
|
|
|
update = False
|
2022-09-17 03:04:08 +00:00
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
print("porcentagem < 0")
|
2022-09-17 03:04:08 +00:00
|
|
|
p2pool_stat.delete()
|
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin = Coin.objects.filter(name="xmr").get(date=yesterday)
|
2022-09-17 03:04:08 +00:00
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
print("achou coin de ontem")
|
2022-09-17 03:04:08 +00:00
|
|
|
if coin.hashrate > 0:
|
|
|
|
update = True
|
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
update = False
|
|
|
|
except Coin.DoesNotExist:
|
|
|
|
print("nao achou coin de ontem")
|
|
|
|
update = False
|
|
|
|
except P2Pool.DoesNotExist:
|
|
|
|
print("nao achou p2pool_mini de hoje")
|
2022-09-17 03:04:08 +00:00
|
|
|
try:
|
2024-11-12 13:09:33 +00:00
|
|
|
coin = Coin.objects.filter(name="xmr").get(date=yesterday)
|
2022-09-17 03:04:08 +00:00
|
|
|
if coin.hashrate > 0:
|
|
|
|
update = True
|
|
|
|
else:
|
2024-11-12 13:09:33 +00:00
|
|
|
update = False
|
|
|
|
except Coin.DoesNotExist:
|
|
|
|
update = False
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
if update:
|
|
|
|
p2pool_stat = P2Pool()
|
|
|
|
p2pool_stat.date = today
|
2024-11-12 13:09:33 +00:00
|
|
|
response = requests.get("https://p2pool.io/mini/api/pool/stats")
|
|
|
|
|
2022-09-17 03:04:08 +00:00
|
|
|
data = json.loads(response.text)
|
2024-11-12 13:09:33 +00:00
|
|
|
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"]
|
2022-09-17 03:04:08 +00:00
|
|
|
p2pool_stat.mini = True
|
|
|
|
p2pool_stat.save()
|
2024-11-12 13:09:33 +00:00
|
|
|
print("p2pool_mini saved!")
|
|
|
|
|
2024-11-13 11:05:11 +00:00
|
|
|
df = pd.read_excel(DATA_FILE, engine="odf", sheet_name="p2poolmini")
|
|
|
|
start_row, end_row = 2, 9999
|
|
|
|
start_col, end_col = 0, 2
|
|
|
|
values_mat = df.iloc[start_row:end_row, start_col:end_col].to_numpy()
|
2022-09-17 03:04:08 +00:00
|
|
|
|
|
|
|
k = len(values_mat)
|
2024-11-12 13:09:33 +00:00
|
|
|
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")
|
2022-09-17 03:04:08 +00:00
|
|
|
if date_aux < date_aux2:
|
2024-11-13 11:05:11 +00:00
|
|
|
values_mat[k][5] = p2pool_stat.totalblocksfound
|
|
|
|
values_mat[k][4] = p2pool_stat.totalhashes
|
|
|
|
values_mat[k][3] = p2pool_stat.percentage
|
|
|
|
values_mat[k][2] = p2pool_stat.hashrate
|
|
|
|
values_mat[k][1] = p2pool_stat.miners
|
|
|
|
values_mat[k][0] = datetime.datetime.strftime(p2pool_stat.date, "%Y-%m-%d")
|
|
|
|
|
|
|
|
df.iloc[start_row:end_row, start_col:end_col] = values_mat
|
|
|
|
df.to_excel(DATA_FILE, sheet_name="p2poolmini", index=False)
|
|
|
|
|
2024-11-12 13:09:33 +00:00
|
|
|
print("spreadsheet updated")
|
|
|
|
else:
|
|
|
|
print("spreadsheet already with the latest data")
|
2022-09-17 03:04:08 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
return True
|