Browse Source

Cleanup

develop
yakamok 6 years ago committed by GitHub
parent
commit
5cbf32f2e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 54
      api/niflheim-api.py

54
api/niflheim-api.py

@ -1,11 +1,6 @@
from flask import Flask, request, render_template
from flask_restful import Resource, Api
from json import dumps
from flask import jsonify
import time
import sys
import os
import time import time
from flask import Flask, render_template
from flask_restful import Resource, Api
import requests import requests
import psycopg2 import psycopg2
@ -17,14 +12,16 @@ DB_USER = "yggindex"
DB_NAME = "yggindex" DB_NAME = "yggindex"
DB_HOST = "localhost" DB_HOST = "localhost"
#will add this as something seperate and pull down to a file rather
#than request it everytime.
def get_nodelist(): def get_nodelist():
data = requests.get("use the raw view of the github nodelist", timeout=1) 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 = {}
for x in nodes: for key in nodes:
index_table[x[0]] = x[1] index_table[key[0]] = key[1]
return index_table return index_table
@ -39,21 +36,24 @@ def check_nodelist(nodetable, key):
def age_calc(ustamp): def age_calc(ustamp):
if (time.time() - ustamp) <= 14400 : if (time.time() - ustamp) <= 14400:
return True return True
else: else:
return False return False
#active nodes in the past 4hrs #active nodes in the past 4hrs
class nodes_current(Resource): class nodesCurrent(Resource):
def get(self): def get(self):
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD) dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor() cur = dbconn.cursor()
nodes = {} nodes = {}
cur.execute("select * from yggindex") cur.execute("select * from yggindex")
for i in cur.fetchall(): for i in cur.fetchall():
if age_calc(int(i[2])): if age_calc(int(i[2])):
nodes[i[0]] = [i[1],int(i[2])] nodes[i[0]] = [i[1], int(i[2])]
dbconn.commit() dbconn.commit()
cur.close() cur.close()
@ -67,32 +67,38 @@ class nodes_current(Resource):
@app.route("/") @app.route("/")
def fpage(): def fpage():
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD) dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor() cur = dbconn.cursor()
nodes = {} nodes = {}
cur.execute("select * from yggindex") cur.execute("select * from yggindex")
for i in cur.fetchall(): for i in cur.fetchall():
if age_calc(int(i[2])): if age_calc(int(i[2])):
nodes[i[0]] = [i[1],int(i[2])] nodes[i[0]] = [i[1], int(i[2])]
dbconn.commit() dbconn.commit()
cur.close() cur.close()
dbconn.close() dbconn.close()
return render_template('index.html', nodes = str(len(nodes))) return render_template('index.html', nodes=str(len(nodes)))
@app.route("/contrib") @app.route("/contrib")
def cpage(): def cpage():
try: try:
NODELIST = get_nodelist() domain_nodelist = get_nodelist()
print "list exists" print "list exists"
except: except:
print "failed" print "failed"
NODELIST = None domain_nodelist = None
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD) dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor() cur = dbconn.cursor()
cur.execute("select * from contrib") cur.execute("select * from contrib")
nodes = [] nodes = []
@ -106,16 +112,16 @@ def cpage():
dbconn.close() dbconn.close()
dnodes = [] dnodes = []
for x in nodes: for key in nodes:
dnodes.append(check_nodelist(NODELIST, x)) dnodes.append(check_nodelist(domain_nodelist, key))
dnodes.sort(reverse=True) dnodes.sort(reverse=True)
return render_template('contrib.html', contribnodes = dnodes, nocontribs=str(len(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(nodesCurrent, '/current')
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='::', port=3000) app.run(host='::', port=3000)

Loading…
Cancel
Save