|
| 1 | +import random |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +import requests |
| 5 | +from bs4 import BeautifulSoup |
| 6 | + |
| 7 | +AGENTS = ( |
| 8 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36", |
| 9 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36", |
| 10 | + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36", |
| 11 | + "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0", |
| 12 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 14.5; rv:128.0) Gecko/20100101 Firefox/128.0", |
| 13 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0", |
| 14 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15", |
| 15 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.2592.113", |
| 16 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.2592.113" |
| 17 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36" |
| 18 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36", |
| 19 | + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36" |
| 20 | +) |
| 21 | + |
| 22 | +GRC_PRICE_URLS = ("https://www.bybit.com/en/coin-price/gridcoin-research/", "https://coinstats.app/coins/gridcoin/", "https://marketcapof.com/crypto/gridcoin-research/") |
| 23 | + |
| 24 | + |
| 25 | +def get_grc_price_from_site() -> tuple[Union[float, None], str, list, list, list]: |
| 26 | + headers = requests.utils.default_headers() |
| 27 | + headers["User-Agent"] = random.choice(AGENTS) |
| 28 | + found_prices = [] |
| 29 | + url_messages = [] |
| 30 | + info_logger_messages = [] |
| 31 | + error_logger_messages = [] |
| 32 | + |
| 33 | + for url in GRC_PRICE_URLS: |
| 34 | + try: |
| 35 | + response = requests.get(url, headers=headers, timeout=5) |
| 36 | + except requests.exceptions.Timeout as error: |
| 37 | + error_logger_messages.append(f"Error fetching stats from {url}: {error}") |
| 38 | + continue |
| 39 | + |
| 40 | + soup = BeautifulSoup(response.content, "html.parser") |
| 41 | + |
| 42 | + if url == "https://www.bybit.com/en/coin-price/gridcoin-research/": |
| 43 | + pre_price = soup.find("div", attrs={"data-cy": "coinPrice"}) |
| 44 | + |
| 45 | + if pre_price is not None: |
| 46 | + try: |
| 47 | + price = pre_price.text.replace("$", "").strip() |
| 48 | + float_price = float(price) |
| 49 | + found_prices.append(float_price) |
| 50 | + info_logger_messages.append(f"Found GRC price of {float_price} from {url}") |
| 51 | + except Exception: |
| 52 | + url_messages.append(f"Error getting info from {url}") |
| 53 | + else: |
| 54 | + url_messages.append(f"Error getting info from {url}") |
| 55 | + elif url == "https://coinstats.app/coins/gridcoin/": |
| 56 | + pre_price = soup.find("div", class_="CoinOverview_mainPrice__YygaC") |
| 57 | + |
| 58 | + if pre_price is not None: |
| 59 | + try: |
| 60 | + price = pre_price.p.text.replace("$", "").strip() |
| 61 | + float_price = float(price) |
| 62 | + found_prices.append(float_price) |
| 63 | + info_logger_messages.append(f"Found GRC price of {float_price} from {url}") |
| 64 | + except Exception: |
| 65 | + url_messages.append(f"Error getting info from {url}") |
| 66 | + else: |
| 67 | + url_messages.append(f"Error getting info from {url}") |
| 68 | + elif url == "https://marketcapof.com/crypto/gridcoin-research/": |
| 69 | + pre_pre_price = soup.find("div", class_="price") |
| 70 | + |
| 71 | + if pre_pre_price is not None: |
| 72 | + pre_price = pre_pre_price.find(string=True, recursive=False) |
| 73 | + |
| 74 | + if pre_price is not None: |
| 75 | + try: |
| 76 | + price = pre_price.replace("$", "").strip() |
| 77 | + float_price = float(price) |
| 78 | + found_prices.append(float_price) |
| 79 | + info_logger_messages.append(f"Found GRC price of {float_price} from {url}") |
| 80 | + except Exception: |
| 81 | + url_messages.append(f"Error getting info from {url}") |
| 82 | + else: |
| 83 | + url_messages.append(f"Error getting info from {url}") |
| 84 | + else: |
| 85 | + url_messages.append(f"Error getting info from {url}") |
| 86 | + |
| 87 | + if len(found_prices) > 0: |
| 88 | + table_message = f"Found GRC price {sum(found_prices) / len(found_prices)}" |
| 89 | + return sum(found_prices) / len(found_prices), table_message, url_messages, info_logger_messages, error_logger_messages |
| 90 | + |
| 91 | + table_message = "Unable to find GRC price" |
| 92 | + return None, table_message, url_messages, info_logger_messages, error_logger_messages |
0 commit comments