1
0
mirror of https://github.com/r4sas/Niflheim-api synced 2025-02-05 03:24:20 +00:00
yggdrasil-api/api/niflheim-api.py

128 lines
3.0 KiB
Python
Raw Normal View History

2018-11-13 14:10:35 +01:00
import time
2018-11-27 17:07:48 +01:00
from flask import Flask, render_template
from flask_restful import Resource, Api
2018-11-13 14:10:35 +01:00
import requests
import psycopg2
2018-07-09 15:47:21 +02:00
app = Flask(__name__)
api = Api(app)
DB_PASSWORD = "password"
DB_USER = "yggindex"
DB_NAME = "yggindex"
DB_HOST = "localhost"
2018-07-09 17:16:09 +02:00
2018-11-27 17:07:48 +01:00
#will add this as something seperate and pull down to a file rather
#than request it everytime.
2018-11-13 14:10:35 +01:00
def get_nodelist():
data = requests.get("use the raw view of the github nodelist", timeout=1)
2018-11-13 14:10:35 +01:00
nodes = [x.split() for x in data.text.split('\n') if x]
2018-11-27 17:07:48 +01:00
2018-11-13 14:10:35 +01:00
index_table = {}
2018-11-27 17:07:48 +01:00
for key in nodes:
index_table[key[0]] = key[1]
2018-11-13 14:10:35 +01:00
return index_table
def check_nodelist(nodetable, key):
if nodetable:
if nodetable.get(key):
return nodetable.get(key)
else:
return key
else:
return key
2018-07-09 15:47:21 +02:00
def age_calc(ustamp):
2018-11-27 17:07:48 +01:00
if (time.time() - ustamp) <= 14400:
2018-07-09 15:47:21 +02:00
return True
else:
return False
#active nodes in the past 4hrs
2018-11-27 17:07:48 +01:00
class nodesCurrent(Resource):
2018-07-09 15:47:21 +02:00
def get(self):
2018-11-27 17:07:48 +01:00
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
2018-07-09 15:47:21 +02:00
nodes = {}
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
2018-11-27 17:07:48 +01:00
nodes[i[0]] = [i[1], int(i[2])]
2018-11-13 14:10:35 +01:00
dbconn.commit()
cur.close()
dbconn.close()
2018-07-09 15:47:21 +02:00
nodelist = {}
nodelist['yggnodes'] = nodes
2018-11-13 14:10:35 +01:00
2018-07-09 15:47:21 +02:00
return nodelist
2018-11-13 14:10:35 +01:00
@app.route("/")
def fpage():
2018-11-27 17:07:48 +01:00
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
2018-11-13 14:10:35 +01:00
nodes = {}
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
2018-11-27 17:07:48 +01:00
nodes[i[0]] = [i[1], int(i[2])]
2018-11-13 14:10:35 +01:00
dbconn.commit()
cur.close()
dbconn.close()
2018-11-13 14:10:35 +01:00
2018-11-27 17:07:48 +01:00
return render_template('index.html', nodes=str(len(nodes)))
2018-11-13 14:10:35 +01:00
@app.route("/contrib")
def cpage():
try:
2018-11-27 17:07:48 +01:00
domain_nodelist = get_nodelist()
2018-11-13 14:10:35 +01:00
print "list exists"
except:
print "failed"
2018-11-27 17:07:48 +01:00
domain_nodelist = None
2018-11-13 14:10:35 +01:00
2018-11-27 17:07:48 +01:00
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
cur.execute("select * from contrib")
2018-11-13 14:10:35 +01:00
nodes = []
for i in cur.fetchall():
if age_calc(int(i[1])):
2018-11-13 14:10:35 +01:00
nodes.append(i[0])
dbconn.commit()
cur.close()
dbconn.close()
2018-11-13 14:10:35 +01:00
dnodes = []
2018-11-27 17:07:48 +01:00
for key in nodes:
dnodes.append(check_nodelist(domain_nodelist, key))
2018-11-13 14:10:35 +01:00
dnodes.sort(reverse=True)
2018-11-27 17:07:48 +01:00
return render_template('contrib.html', contribnodes=dnodes, nocontribs=str(len(dnodes)))
2018-11-13 14:10:35 +01:00
2018-07-09 15:47:21 +02:00
#sort out the api request here for the url
2018-11-27 17:07:48 +01:00
api.add_resource(nodesCurrent, '/current')
2018-07-09 15:47:21 +02:00
if __name__ == '__main__':
2018-11-27 17:07:48 +01:00
app.run(host='::', port=3000)