diff --git a/fapi/factory.py b/fapi/factory.py index 8948894..93ccd1d 100644 --- a/fapi/factory.py +++ b/fapi/factory.py @@ -66,7 +66,7 @@ def create_app(): f.close() from fapi.fapi import FeatherApi - from fapi.utils import loopyloop, TxFiatDb + from fapi.utils import loopyloop, TxFiatDb, XmrRig txfiatdb = TxFiatDb(settings.crypto_name, settings.crypto_block_date_start) loop.create_task(loopyloop(20, FeatherApi.xmrto_rates, FeatherApi.after_xmrto)) loop.create_task(loopyloop(120, FeatherApi.crypto_rates, FeatherApi.after_crypto)) @@ -76,6 +76,7 @@ def create_app(): loop.create_task(loopyloop(60, FeatherApi.blockheight, FeatherApi.after_blockheight)) loop.create_task(loopyloop(60, FeatherApi.check_nodes, FeatherApi.after_check_nodes)) loop.create_task(loopyloop(43200, txfiatdb.update)) + loop.create_task(loopyloop(43200, XmrRig.releases, XmrRig.after_releases)) import fapi.routes return app diff --git a/fapi/utils.py b/fapi/utils.py index 0698b9b..dc3fb4c 100644 --- a/fapi/utils.py +++ b/fapi/utils.py @@ -12,6 +12,8 @@ from datetime import datetime import aiohttp +import settings + class BlockHeight: @staticmethod @@ -161,3 +163,65 @@ class TxFiatDb: self.data[_date.year][_date.month][_date.day] = v self.write() + + +class XmrRig: + @staticmethod + async def releases(): + from fapi.factory import app, cache + from fapi.fapi import FeatherApi + + blob = await FeatherApi.redis_get("xmrig_releases") + if blob and app.config["DEBUG"]: + return blob + + try: + result = await httpget(settings.urls["xmrig"]) + if not isinstance(result, list): + raise Exception("JSON response was not a list") + if len(result) <= 1: + raise Exception("JSON response list was 1 or less") + result = result[0] + await cache.set("xmrig_releases", json.dumps(result)) + blob = result + except Exception as ex: + app.logger.error(f"error parsing xmrig blob: {ex}") + if blob: + app.logger.warning(f"passing xmrig output from cache") + return blob + + return blob + + @staticmethod + async def after_releases(data): + from fapi.factory import api_data + from dateutil.parser import parse + _data = [] + for asset in data['assets']: + for expected in ["tar.gz", ".zip"]: + if asset["state"] != "uploaded": + continue + if asset["name"].endswith(expected): + _data.append(asset) + version = data['tag_name'] + assets = {} + + for asset in _data: + operating_system = "linux" + if "msvc" in asset['name'] or "win64" in asset['name']: + operating_system = "windows" + elif "macos" in asset["name"]: + operating_system = "macos" + + assets.setdefault(operating_system, []) + assets[operating_system].append({ + "name": asset["name"], + "created_at": parse(asset["created_at"]).strftime("%Y-%m-%d"), + "url": f"https://github.com/xmrig/xmrig/releases/download/{version}/{asset['name']}", + "download_count": int(asset["download_count"]) + }) + + api_data["xmrig"] = { + "version": version, + "assets": assets + } diff --git a/settings.py_example b/settings.py_example index a09c7b9..6898901 100644 --- a/settings.py_example +++ b/settings.py_example @@ -14,6 +14,7 @@ crypto_symbol = "xmr" crypto_block_date_start = "20140418" urls = { + "xmrig": "https://api.github.com/repos/xmrig/xmrig/releases", "reddit": "https://www.reddit.com/r/monero/top.json?limit=100", "ccs": "https://ccs.getmonero.org", "fiat_rates": "https://api.exchangeratesapi.io/latest?base=USD",