Skip to content

Instantly share code, notes, and snippets.

@mansouryaacoubi
Last active November 17, 2017 15:13
Show Gist options
  • Save mansouryaacoubi/6c07ed381b388766d6a73338bb83c6f2 to your computer and use it in GitHub Desktop.
Save mansouryaacoubi/6c07ed381b388766d6a73338bb83c6f2 to your computer and use it in GitHub Desktop.
Create JSON from Bitcoin Miner Pool Stats for own Mining (GroupFabric) with stats.csv file
<?php
header('Content-Type: application/json');
const DEFAULT_BTC_PAYOUT_ADDRESS = '1f7now3v2J56gzpYQfkmn1mv9KvcqtC6E';
define('BTC_PAYOUT_ADDRESS', isset($_GET['q'])?$_GET['q']:DEFAULT_BTC_PAYOUT_ADDRESS);
const BASE_URL = 'http://www.groupfabric.com/pool/?q=';
const SEARCH_FLAG = 'set_viewmodel(';
const FILE_NAME = 'stats.csv';
$htmldoc = file_get_contents(BASE_URL.BTC_PAYOUT_ADDRESS);
$offset = strpos($htmldoc, SEARCH_FLAG) + 1;
$start_pos = strpos($htmldoc, SEARCH_FLAG, $offset);
$htmldoc = substr($htmldoc, $start_pos);
$htmldoc = explode("\n", $htmldoc)[0];
$htmldoc = trim(str_replace(SEARCH_FLAG, '', $htmldoc));
$htmldoc = substr($htmldoc, 0, strlen($htmldoc)-1);
$obj = null;
if(empty($obj = json_decode($htmldoc))) {
echo json_encode(['status' => 'ERROR', 'message' => 'BTC Payout Address does not exist in this mining pool', 'addr' => BTC_PAYOUT_ADDRESS]);
} else {
if( is_null($obj->posix_time) ) {
$obj->posix_time = round(microtime(true)*1000);
}
$obj->hr_time = date('d.m.Y H:i:s', round($obj->posix_time/1000));
if(isset($_GET['stats'])) {
$result = date('d.m.Y H:i:s') . ';' . ($obj->num1/1000).PHP_EOL;
if(!file_exists(FILE_NAME)) {
file_put_contents(FILE_NAME, 'Timestamp; Satoshis'.PHP_EOL);
}
file_put_contents(FILE_NAME, $result, FILE_APPEND | LOCK_EX);
}
echo json_encode($obj);
}
#!/usr/bin/env
import requests
from time import sleep, time
import os
filename = 'stats.csv'
while True:
req = requests.get('https://yaacoubi.com/btc')
req = req.json()
sat = str(round(req['num1']/1000))
hrt = req['hr_time'] # human readable time
tmstamp = str(round(time()))
csvstr = hrt + ';' + sat + '\n'
if not os.path.exists(filename):
csvstr = 'Timestamp; Satoshis\n' + csvstr
with open(filename, 'a') as f:
f.write(csvstr)
print('[' + tmstamp + '] => ' + sat + ' Satoshis mined.')
sleep(5*60)
:: FOR WINDOWS
@echo off
mode con cols=50 lines=20
color 0a
cls
python start-stats.py
import requests
from time import sleep, time
while True:
r = requests.get('https://yaacoubi.com/btc?stats')
sat = str(r.json()['num1']/1000)
tmstamp = str(round(time()))
print('[' + tmstamp + '] => ' + sat + ' Satoshis mined.')
sleep(5*60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment