Browse Source

upgrade to use postgresql

rewrote to use postgresql
develop
yakamok 6 years ago committed by GitHub
parent
commit
2743405101
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 69
      api/yggapi.py

69
yggapi.py → api/yggapi.py

@ -1,35 +1,24 @@
from flask import Flask, request, render_template from flask import Flask, request, render_template
from flask_restful import Resource, Api from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps from json import dumps
#adding rate limiting support
from flask import jsonify from flask import jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import time import time
import sys import sys
import os import os
import time import time
import requests import requests
import psycopg2
#check if a database exists or not
db_path = "vservdb/yggindex.db"
if not os.path.exists(db_path):
print "could not find a database"
sys.exit(0)
db_connect = create_engine('sqlite:///' + db_path)
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
limiter = Limiter( DB_PASSWORD = "password"
app, DB_USER = "yggindex"
key_func=get_remote_address, DB_NAME = "yggindex"
default_limits=["500/day", "60/hour"] DB_HOST = "localhost"
)
def get_nodelist(): def get_nodelist():
data = requests.get("https://raw.githubusercontent.com/yakamok/yggdrasil-nodelist/master/nodelist") data = requests.get("use the raw view of the github nodelist", timeout=1)
nodes = [x.split() for x in data.text.split('\n') if x] nodes = [x.split() for x in data.text.split('\n') if x]
index_table = {} index_table = {}
@ -49,7 +38,6 @@ def check_nodelist(nodetable, key):
return key return key
#quickly figure out which is old or new
def age_calc(ustamp): def age_calc(ustamp):
if (time.time() - ustamp) <= 14400 : if (time.time() - ustamp) <= 14400 :
return True return True
@ -59,13 +47,17 @@ def age_calc(ustamp):
#active nodes in the past 4hrs #active nodes in the past 4hrs
class nodes_current(Resource): class nodes_current(Resource):
def get(self): def get(self):
conn = db_connect.connect() dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
query = conn.execute("select * from yggindex") cur = dbconn.cursor()
nodes = {} nodes = {}
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
nodes[i[0]] = [i[1],int(i[2])]
for i in query.cursor.fetchall(): dbconn.commit()
if age_calc(i[2]): cur.close()
nodes[i[0]] = [i[1],i[2]] dbconn.close()
nodelist = {} nodelist = {}
nodelist['yggnodes'] = nodes nodelist['yggnodes'] = nodes
@ -75,13 +67,18 @@ class nodes_current(Resource):
@app.route("/") @app.route("/")
def fpage(): def fpage():
conn = db_connect.connect() dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
query = conn.execute("select * from yggindex") cur = dbconn.cursor()
nodes = {} nodes = {}
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
nodes[i[0]] = [i[1],int(i[2])]
for i in query.cursor.fetchall(): dbconn.commit()
if age_calc(i[2]): cur.close()
nodes[i[0]] = [i[1],i[2]] dbconn.close()
return render_template('index.html', nodes = str(len(nodes))) return render_template('index.html', nodes = str(len(nodes)))
@ -95,26 +92,30 @@ def cpage():
print "failed" print "failed"
NODELIST = None NODELIST = None
conn = db_connect.connect() dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
query = conn.execute("select * from contrib") cur = dbconn.cursor()
cur.execute("select * from contrib")
nodes = [] nodes = []
for i in query.cursor.fetchall(): for i in cur.fetchall():
if age_calc(i[1]): if age_calc(int(i[1])):
nodes.append(i[0]) nodes.append(i[0])
dbconn.commit()
cur.close()
dbconn.close()
dnodes = [] dnodes = []
for x in nodes: for x in nodes:
dnodes.append(check_nodelist(NODELIST, x)) dnodes.append(check_nodelist(NODELIST, x))
dnodes.sort(reverse=True) dnodes.sort(reverse=True)
return render_template('contrib.html', contribnodes = dnodes) return render_template('contrib.html', contribnodes = dnodes, nocontribs=str(len(dnodes)))
#sort out the api request here for the url #sort out the api request here for the url
api.add_resource(nodes_current, '/current') api.add_resource(nodes_current, '/current')
# api.add_resource(nodes_contrib, '/contrib')
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='::', port=3000) app.run(host='::', port=3000)
Loading…
Cancel
Save