diff --git a/grc_price_utils.py b/grc_price_utils.py
index 057ffe5..0c48628 100644
--- a/grc_price_utils.py
+++ b/grc_price_utils.py
@@ -21,8 +21,59 @@
GRC_PRICE_URLS = ("https://www.bybit.com/en/coin-price/gridcoin-research/", "https://coinstats.app/coins/gridcoin/", "https://marketcapof.com/crypto/gridcoin-research/")
+def parse_grc_price_soup(url: str, price_soup: str) -> tuple[Union[float, None], str, str]:
+ float_price = None
+ info_message = ""
+ url_message = ""
-def get_grc_price_from_site() -> tuple[Union[float, None], str, list, list, list]:
+ soup = BeautifulSoup(price_soup, "html.parser")
+
+ if url == "https://www.bybit.com/en/coin-price/gridcoin-research/":
+ pre_price = soup.find("div", attrs={"data-cy": "coinPrice"})
+
+ if pre_price is not None:
+ try:
+ price = pre_price.text.replace("$", "").strip()
+ float_price = float(price)
+ info_message = f"Found GRC price of {float_price} from {url}"
+ except Exception:
+ url_message = f"Error getting info from {url}"
+ else:
+ url_message = f"Error getting info from {url}"
+ elif url == "https://coinstats.app/coins/gridcoin/":
+ pre_price = soup.find("div", class_="CoinOverview_mainPrice__YygaC")
+
+ if pre_price is not None:
+ try:
+ price = pre_price.p.text.replace("$", "").strip()
+ float_price = float(price)
+ info_message = f"Found GRC price of {float_price} from {url}"
+ except Exception:
+ url_message = f"Error getting info from {url}"
+ else:
+ url_message = f"Error getting info from {url}"
+ elif url == "https://marketcapof.com/crypto/gridcoin-research/":
+ pre_pre_price = soup.find("div", class_="price")
+
+ if pre_pre_price is not None:
+ pre_price = pre_pre_price.find(string=True, recursive=False)
+
+ if pre_price is not None:
+ try:
+ price = pre_price.replace("$", "").strip()
+ float_price = float(price)
+ info_message = f"Found GRC price of {float_price} from {url}"
+ except Exception:
+ url_message = f"Error getting info from {url}"
+ else:
+ url_message = f"Error getting info from {url}"
+ else:
+ url_message = f"Error getting info from {url}"
+
+ return float_price, url_message, info_message
+
+
+def get_grc_price_from_sites() -> tuple[Union[float, None], str, list, list, list]:
headers = requests.utils.default_headers()
headers["User-Agent"] = random.choice(AGENTS)
found_prices = []
@@ -37,52 +88,13 @@ def get_grc_price_from_site() -> tuple[Union[float, None], str, list, list, list
error_logger_messages.append(f"Error fetching stats from {url}: {error}")
continue
- soup = BeautifulSoup(response.content, "html.parser")
+ price, url_message, info_message = parse_grc_price_soup(url, response.content)
- if url == "https://www.bybit.com/en/coin-price/gridcoin-research/":
- pre_price = soup.find("div", attrs={"data-cy": "coinPrice"})
+ if price is not None:
+ found_prices.append(price)
- if pre_price is not None:
- try:
- price = pre_price.text.replace("$", "").strip()
- float_price = float(price)
- found_prices.append(float_price)
- info_logger_messages.append(f"Found GRC price of {float_price} from {url}")
- except Exception:
- url_messages.append(f"Error getting info from {url}")
- else:
- url_messages.append(f"Error getting info from {url}")
- elif url == "https://coinstats.app/coins/gridcoin/":
- pre_price = soup.find("div", class_="CoinOverview_mainPrice__YygaC")
-
- if pre_price is not None:
- try:
- price = pre_price.p.text.replace("$", "").strip()
- float_price = float(price)
- found_prices.append(float_price)
- info_logger_messages.append(f"Found GRC price of {float_price} from {url}")
- except Exception:
- url_messages.append(f"Error getting info from {url}")
- else:
- url_messages.append(f"Error getting info from {url}")
- elif url == "https://marketcapof.com/crypto/gridcoin-research/":
- pre_pre_price = soup.find("div", class_="price")
-
- if pre_pre_price is not None:
- pre_price = pre_pre_price.find(string=True, recursive=False)
-
- if pre_price is not None:
- try:
- price = pre_price.replace("$", "").strip()
- float_price = float(price)
- found_prices.append(float_price)
- info_logger_messages.append(f"Found GRC price of {float_price} from {url}")
- except Exception:
- url_messages.append(f"Error getting info from {url}")
- else:
- url_messages.append(f"Error getting info from {url}")
- else:
- url_messages.append(f"Error getting info from {url}")
+ url_messages.append(url_message)
+ info_logger_messages.append(info_message)
if len(found_prices) > 0:
table_message = f"Found GRC price {sum(found_prices) / len(found_prices)}"
diff --git a/main.py b/main.py
index 6ffd467..751e776 100644
--- a/main.py
+++ b/main.py
@@ -26,7 +26,7 @@
from requests.auth import HTTPBasicAuth
from typing import List, Union, Dict, Tuple, Set, Any
import sys, signal
- from grc_price_utils import get_grc_price_from_site
+ from grc_price_utils import get_grc_price_from_sites
# This is needed for some async stuff
import nest_asyncio
@@ -968,7 +968,7 @@ def get_grc_price(sample_text: str = None) -> Union[float, None]:
Raises:
Exception: An error occurred accessing an online GRC price source.
"""
- price, table_message, url_messages, info_log_messages, error_log_messages = get_grc_price_from_site()
+ price, table_message, url_messages, info_log_messages, error_log_messages = get_grc_price_from_sites()
for log_message in info_log_messages:
log.info(log_message)
diff --git a/pyproject.toml b/pyproject.toml
index 1eaaf7d..8d17cf5 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -25,6 +25,7 @@ urllib3 = "^2.2.3"
xmltodict = "^0.14.2"
zope-interface = "^7.2"
nest-asyncio = "^1.6.0"
+beautifulsoup4 = "^4.13.3"
[build-system]
diff --git a/tests/main_tests.py b/tests/main_tests.py
index 1569822..bd93b0f 100644
--- a/tests/main_tests.py
+++ b/tests/main_tests.py
@@ -1,12 +1,15 @@
-import sys
import os
+import sys
sys.path.append(os.getcwd() + '/..')
import json
-import pytest, main, datetime
-from typing import Dict, List, Union, Any, Tuple
+from soups import SOUP_DICTIONARY
+from grc_price_utils import parse_grc_price_soup
+
+import main, datetime
+from typing import Dict, List
def test_check_sidestake_original():
@@ -191,13 +194,18 @@ def test_update_fetch():
main.DATABASE["LASTUPDATECHECK"] = actual_update_check
-def test_get_grc_price():
- sample_text = """CMC }