Exemple #1
0
class Sensor():

    app = Flask(__name__)
    wallet = Wallet()
    payment = Payment(app, wallet)

    @app.route('/info')
    def get_info():
        """Returns details about available endpoints"""

        info = {
            'endpoints': {
                'per-req': 1,
                'returns': {
                    'name': 'timestamp',
                    'description': 'Python UTC timestamp'
                },
                'name': 'value',
                'description': '21BC hashrate in GH/s',
                'route': '/measurement',
                'description': 'Current 21BC mininig hashrate in GH/s'
            }
        }

        return jsonify(info)

    # Charge a fixed fee of 10 satoshis per request to the
    # /measurement endpoint

    @app.route('/measurement')
    @payment.required(1)
    def measurement():

        data = check_output(['21', 'status'])
        timestamp = time.time()

        data = str(data)
        index0 = data.find('Hashrate')
        index1 = data[index0:].find(':')
        index2 = data[index0:].find('GH/s')

        try:
            data = float(data[index0 + index1 + 2:index0 + index2 - 1])

        except:
            data = data[index0 + index1 + 2:index0 + index2 - 1]

        measurement = {'timestamp': timestamp, 'value': data}

        measurement_json = json.dumps(measurement)

        return measurement_json

    # Initialize and run the server
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=3002)
Exemple #2
0
# import from the 21 Developer Library
from two1.lib.bitcoin.txn import Transaction
from two1.lib.bitcoin.crypto import PublicKey
from two1.lib.wallet import Wallet, exceptions
from two1.lib.bitserv.flask import Payment

USCENT = 2801

pp = pprint.PrettyPrinter(indent=2)

db = srvdb.SrvDb('turk.db')

app = Flask(__name__)
wallet = Wallet()
payment = Payment(app, wallet)


def check_timestamp(tstamp):
    curtime = int(time.time())
    min_time = min(tstamp, curtime)
    max_time = max(tstamp, curtime)
    time_diff = max_time - min_time
    if (time_diff > 15):
        return False
    return True


@app.route('/task/<id>')
@payment.required(int(USCENT / 100))
def get_task(id):
DATABASE = "book.db"

def get_db(app):
    with app.app_context():
        db = getattr(g, '_database', None)
        if db is None:
            db = g._database = connect_to_database()
        return db
        
def connect_to_database():
    return sqlite3.connect(DATABASE)

app = machine_app.vending_machine()
conn = get_db(app)
payment = Payment(app, machine_app.wallet)

# fetch current bitcoin price
@app.route('/btc_quote')
def btc_quote():
    logging.info("btc_quote")
    q = machine_app.get_quote()
    return '%.5f' % q

# fetch option price
@app.route('/quote')
def price_quote():
    logging.info("quote")
    q = machine_app.get_quote()
    buy_price, sell_price = get_book_quote(conn, q)
    return 'BTCUSD: %.5f  buy: %.5f, sell: %.5f' % (q, buy_price, sell_price)