diff --git a/yggapi.py b/yggapi.py index e61ae12..5da98a2 100644 --- a/yggapi.py +++ b/yggapi.py @@ -1,14 +1,16 @@ -from flask import Flask, request +from flask import Flask, request, render_template from flask_restful import Resource, Api from sqlalchemy import create_engine from json import dumps -#rate limiting support -from flask.ext.jsonpify import jsonify +#adding rate limiting support +from flask import jsonify from flask_limiter import Limiter from flask_limiter.util import get_remote_address import time import sys import os +import time +import requests #check if a database exists or not db_path = "vservdb/yggindex.db" @@ -26,6 +28,27 @@ limiter = Limiter( default_limits=["500/day", "60/hour"] ) +def get_nodelist(): + data = requests.get("https://raw.githubusercontent.com/yakamok/yggdrasil-nodelist/master/nodelist") + nodes = [x.split() for x in data.text.split('\n') if x] + + index_table = {} + + for x in nodes: + index_table[x[0]] = x[1] + return index_table + + +def check_nodelist(nodetable, key): + if nodetable: + if nodetable.get(key): + return nodetable.get(key) + else: + return key + else: + return key + + #quickly figure out which is old or new def age_calc(ustamp): if (time.time() - ustamp) <= 14400 : @@ -39,43 +62,59 @@ class nodes_current(Resource): conn = db_connect.connect() query = conn.execute("select * from yggindex") nodes = {} + for i in query.cursor.fetchall(): if age_calc(i[2]): nodes[i[0]] = [i[1],i[2]] - nodelist = {} - nodelist['yggnodes'] = nodes - return nodelist -#nodes that may not be active anymore or have been offline for a while such as laptops -#could be used as last seen -class nodes_old(Resource): - def get(self): - conn = db_connect.connect() - query = conn.execute("select * from yggindex") - nodes = {} - for i in query.cursor.fetchall(): - if not age_calc(i[2]): - nodes[i[0]] = [i[1],i[2]] nodelist = {} nodelist['yggnodes'] = nodes + return nodelist -#return entire database of nodes regardless of age -class nodes_all(Resource): - def get(self): - conn = db_connect.connect() - query = conn.execute("select * from yggindex") - nodes = {} - for i in query.cursor.fetchall(): + +@app.route("/") +def fpage(): + conn = db_connect.connect() + query = conn.execute("select * from yggindex") + nodes = {} + + for i in query.cursor.fetchall(): + if age_calc(i[2]): nodes[i[0]] = [i[1],i[2]] - nodelist = {} - nodelist['yggnodes'] = nodes - return nodelist + + return render_template('index.html', nodes = str(len(nodes))) + + +@app.route("/contrib") +def cpage(): + try: + NODELIST = get_nodelist() + print "list exists" + except: + print "failed" + NODELIST = None + + conn = db_connect.connect() + query = conn.execute("select * from contrib") + nodes = [] + + for i in query.cursor.fetchall(): + if age_calc(i[1]): + nodes.append(i[0]) + + dnodes = [] + for x in nodes: + dnodes.append(check_nodelist(NODELIST, x)) + + dnodes.sort(reverse=True) + + return render_template('contrib.html', contribnodes = dnodes) + #sort out the api request here for the url api.add_resource(nodes_current, '/current') -api.add_resource(nodes_old, '/old') -api.add_resource(nodes_all, '/all') +# api.add_resource(nodes_contrib, '/contrib') if __name__ == '__main__': app.run(host='::', port=3000)