mirror of https://github.com/r4sas/Niflheim-api
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.7 KiB
122 lines
2.7 KiB
6 years ago
|
from flask import Flask, request, render_template
|
||
6 years ago
|
from flask_restful import Resource, Api
|
||
|
from json import dumps
|
||
6 years ago
|
from flask import jsonify
|
||
6 years ago
|
import time
|
||
|
import sys
|
||
|
import os
|
||
6 years ago
|
import time
|
||
|
import requests
|
||
6 years ago
|
import psycopg2
|
||
6 years ago
|
|
||
|
app = Flask(__name__)
|
||
|
api = Api(app)
|
||
|
|
||
6 years ago
|
DB_PASSWORD = "password"
|
||
|
DB_USER = "yggindex"
|
||
|
DB_NAME = "yggindex"
|
||
|
DB_HOST = "localhost"
|
||
6 years ago
|
|
||
6 years ago
|
def get_nodelist():
|
||
6 years ago
|
data = requests.get("use the raw view of the github nodelist", timeout=1)
|
||
6 years ago
|
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
|
||
|
|
||
|
|
||
6 years ago
|
def age_calc(ustamp):
|
||
|
if (time.time() - ustamp) <= 14400 :
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
#active nodes in the past 4hrs
|
||
|
class nodes_current(Resource):
|
||
|
def get(self):
|
||
6 years ago
|
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
|
||
|
cur = dbconn.cursor()
|
||
6 years ago
|
nodes = {}
|
||
6 years ago
|
cur.execute("select * from yggindex")
|
||
|
for i in cur.fetchall():
|
||
|
if age_calc(int(i[2])):
|
||
|
nodes[i[0]] = [i[1],int(i[2])]
|
||
6 years ago
|
|
||
6 years ago
|
dbconn.commit()
|
||
|
cur.close()
|
||
|
dbconn.close()
|
||
6 years ago
|
|
||
|
nodelist = {}
|
||
|
nodelist['yggnodes'] = nodes
|
||
6 years ago
|
|
||
6 years ago
|
return nodelist
|
||
|
|
||
6 years ago
|
|
||
|
@app.route("/")
|
||
|
def fpage():
|
||
6 years ago
|
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
|
||
|
cur = dbconn.cursor()
|
||
6 years ago
|
nodes = {}
|
||
6 years ago
|
cur.execute("select * from yggindex")
|
||
|
|
||
|
for i in cur.fetchall():
|
||
|
if age_calc(int(i[2])):
|
||
|
nodes[i[0]] = [i[1],int(i[2])]
|
||
6 years ago
|
|
||
6 years ago
|
dbconn.commit()
|
||
|
cur.close()
|
||
|
dbconn.close()
|
||
6 years ago
|
|
||
|
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
|
||
|
|
||
6 years ago
|
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
|
||
|
cur = dbconn.cursor()
|
||
|
cur.execute("select * from contrib")
|
||
6 years ago
|
nodes = []
|
||
|
|
||
6 years ago
|
for i in cur.fetchall():
|
||
|
if age_calc(int(i[1])):
|
||
6 years ago
|
nodes.append(i[0])
|
||
|
|
||
6 years ago
|
dbconn.commit()
|
||
|
cur.close()
|
||
|
dbconn.close()
|
||
|
|
||
6 years ago
|
dnodes = []
|
||
|
for x in nodes:
|
||
|
dnodes.append(check_nodelist(NODELIST, x))
|
||
|
|
||
|
dnodes.sort(reverse=True)
|
||
|
|
||
6 years ago
|
return render_template('contrib.html', contribnodes = dnodes, nocontribs=str(len(dnodes)))
|
||
6 years ago
|
|
||
6 years ago
|
|
||
|
#sort out the api request here for the url
|
||
|
api.add_resource(nodes_current, '/current')
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(host='::', port=3000)
|