refactored cgf_util_sg to its own folder
This commit is contained in:
16
README.md
16
README.md
@@ -46,7 +46,7 @@ A basic script to find all miners on the network and get the hashrate from them
|
|||||||
```python
|
```python
|
||||||
import asyncio
|
import asyncio
|
||||||
from network import MinerNetwork
|
from network import MinerNetwork
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_hashrate():
|
async def get_hashrate():
|
||||||
@@ -82,7 +82,7 @@ You can also create your own miner without scanning if you know the IP:
|
|||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from miners.miner_factory import MinerFactory
|
from miners.miner_factory import MinerFactory
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_hashrate(ip: str):
|
async def get_miner_hashrate(ip: str):
|
||||||
@@ -110,7 +110,7 @@ Or generate a miner directly without the factory:
|
|||||||
```python
|
```python
|
||||||
import asyncio
|
import asyncio
|
||||||
from miners.bosminer import BOSminer
|
from miners.bosminer import BOSminer
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_hashrate(ip: str):
|
async def get_miner_hashrate(ip: str):
|
||||||
@@ -134,7 +134,7 @@ Or finally, just get the API directly:
|
|||||||
```python
|
```python
|
||||||
import asyncio
|
import asyncio
|
||||||
from API.bosminer import BOSMinerAPI
|
from API.bosminer import BOSMinerAPI
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_hashrate(ip: str):
|
async def get_miner_hashrate(ip: str):
|
||||||
@@ -163,7 +163,7 @@ Now that you know that, lets move on to some common API functions that you might
|
|||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from miners.miner_factory import MinerFactory
|
from miners.miner_factory import MinerFactory
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_pool_data(ip: str):
|
async def get_miner_pool_data(ip: str):
|
||||||
@@ -202,7 +202,7 @@ A pretty good example of really trying to make this robust is in ```cfg_util.fun
|
|||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from miners.miner_factory import MinerFactory
|
from miners.miner_factory import MinerFactory
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_temperature_data(ip: str):
|
async def get_miner_temperature_data(ip: str):
|
||||||
@@ -232,7 +232,7 @@ How about data on the power usage of the miner? This one only works for Whatsmi
|
|||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from miners.miner_factory import MinerFactory
|
from miners.miner_factory import MinerFactory
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_power_data(ip: str):
|
async def get_miner_power_data(ip: str):
|
||||||
@@ -271,7 +271,7 @@ How about we get the current pool user and hashrate in 1 command?
|
|||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from miners.miner_factory import MinerFactory
|
from miners.miner_factory import MinerFactory
|
||||||
from cfg_util.cfg_util_sg.func.parse_data import safe_parse_api_data
|
from cfg_util.func.parse_data import safe_parse_api_data
|
||||||
|
|
||||||
|
|
||||||
async def get_miner_hashrate_and_pool(ip: str):
|
async def get_miner_hashrate_and_pool(ip: str):
|
||||||
|
|||||||
0
cfg_util/func/__init__.py
Normal file
0
cfg_util/func/__init__.py
Normal file
50
cfg_util/func/parse_data.py
Normal file
50
cfg_util/func/parse_data.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
from API import APIError
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyPep8
|
||||||
|
async def safe_parse_api_data(data: dict or list, *path: str or int, idx: int = 0):
|
||||||
|
path = [*path]
|
||||||
|
if len(path) == idx+1:
|
||||||
|
if isinstance(path[idx], str):
|
||||||
|
if isinstance(data, dict):
|
||||||
|
if path[idx] in data.keys():
|
||||||
|
return data[path[idx]]
|
||||||
|
elif isinstance(path[idx], int):
|
||||||
|
if isinstance(data, list):
|
||||||
|
if len(data) > path[idx]:
|
||||||
|
return data[path[idx]]
|
||||||
|
else:
|
||||||
|
if isinstance(path[idx], str):
|
||||||
|
if isinstance(data, dict):
|
||||||
|
if path[idx] in data.keys():
|
||||||
|
parsed_data = await safe_parse_api_data(data[path[idx]], idx=idx+1, *path)
|
||||||
|
# has to be == None, or else it fails on 0.0 hashrates
|
||||||
|
# noinspection PyPep8
|
||||||
|
if parsed_data == None:
|
||||||
|
raise APIError(f"Data parsing failed on path index {idx} - \nKey: {path[idx]} \nData: {data}")
|
||||||
|
return parsed_data
|
||||||
|
else:
|
||||||
|
if idx == 0:
|
||||||
|
raise APIError(f"Data parsing failed on path index {idx} - \nKey: {path[idx]} \nData: {data}")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if idx == 0:
|
||||||
|
raise APIError(f"Data parsing failed on path index {idx} - \nKey: {path[idx]} \nData: {data}")
|
||||||
|
return False
|
||||||
|
elif isinstance(path[idx], int):
|
||||||
|
if isinstance(data, list):
|
||||||
|
if len(data) > path[idx]:
|
||||||
|
parsed_data = await safe_parse_api_data(data[path[idx]], idx=idx+1, *path)
|
||||||
|
# has to be == None, or else it fails on 0.0 hashrates
|
||||||
|
# noinspection PyPep8
|
||||||
|
if parsed_data == None:
|
||||||
|
raise APIError(f"Data parsing failed on path index {idx} - \nKey: {path[idx]} \nData: {data}")
|
||||||
|
return parsed_data
|
||||||
|
else:
|
||||||
|
if idx == 0:
|
||||||
|
raise APIError(f"Data parsing failed on path index {idx} - \nKey: {path[idx]} \nData: {data}")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if idx == 0:
|
||||||
|
raise APIError(f"Data parsing failed on path index {idx} - \nKey: {path[idx]} \nData: {data}")
|
||||||
|
return False
|
||||||
Reference in New Issue
Block a user