#!/usr/bin/env python3

import os
import json
import random
import apsw
from settings import *

from flask import Flask
from flask import request
from flask import send_from_directory

from two1.lib.wallet import Wallet
from two1.lib.bitserv.flask import Payment

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

connection = apsw.Connection(DB)
c = connection.cursor()


@app.route('/')
@app.route('/info')
@app.route('/help')
def home():
    '''Return service, pricing and endpoint information'''
    home_obj = [{"name": "up/1",            # service name/version
                 "pricing-type": "per-req",
                 "pricing" : [{"rpc": "up",
                               "per-req": PRICE,
                               "description": "Lists available services at time of last crawl."
                              },
                              {"rpc": "info",        # True indicates default
                               "per-req": 0,
                               "per-mb": 0,
                               "description": "This listing of endpoints provided by this server. "\
                                    "Available at / and /info."
                              },

                              # default
                              {"rpc": True,        # True indicates default
                               "per-req": 0,
                               "per-mb": 0
                              }],
                  "description": "This Up server provides directory services backed by a crawler. "\
                      "Download the client and server at https://github.com/weex/up"
                }
               ]

    body = json.dumps(home_obj, indent=2)

    return (body, 200, {'Content-length': len(body),
                        'Content-type': 'application/json',
                       }
           )

@app.route('/up')
@payment.required(PRICE)
def listing():
    services = []
    for url, owner, name in c.execute("SELECT url, owner, name from service where updated > datetime('now','-1 day')"):
        services.append({'name': name, 'url': url})

    body = json.dumps(services, indent=2) 
    code = 201
    return (body, code, {'Content-length': len(body),
                        'Content-type': 'application/json',
                        }
            )

if __name__ == '__main__':
    if DEBUG:
       app.debug = True
    app.run(host='0.0.0.0', port=SERVER_PORT)

@@ -848,16 +848,672 @@
      %7D,%0A
+                              %7B%22rpc%22: %22put%22,        # True indicates default%0A                               %22per-req%22: PRICE,%0A                               %22per-mb%22: 0,%0A                               %22description%22: %22List your own endpoint here. From your 21BC, the %22%5C%0A                                       %22command is: 21 buy --maxprice %22+ str(PRICE) +%22 url %22%5C%0A                                       %22http://10.244.34.100/put?url=%7Byour_endpoint_url%7D. If you %22%5C%0A                                       %22a url that we already have, you'll get a note that it's %22%5C%0A                                       %22already registered.%22%0A                              %7D,%0A
         
@@ -2525,16 +2525,735 @@
      )%0A%0A
+%0A@app.route('/put')%0A@payment.required(PRICE)%0Adef add_listing():%0A    url = request.args.get('url')%0A%0A    res = c.execute(%22SELECT url from service where url = ?%22, (url,))%0A    count = 0%0A    for row in res:%0A        count = count + 1%0A%0A    if count == 0:%0A        c.execute(%22INSERT INTO service (url) VALUES (?)%22, (url,))%0A        body = json.dumps(%7B'result': 'success'%7D, %0A                          indent=2)%0A    else:%0A        body = json.dumps(%7B'result': 'success',%0A                           'note': 'Endpoint already registered.'%7D,%0A                          indent=2)%0A%0A    return (body, 200, %7B'Content-length': len(body),%0A                        'Content-type': 'application/json',%0A                       %7D%0A           )%0A    %0A
 @app.rou

add /put to add new listings