Added ttl

This commit is contained in:
Guy Van Sanden 2023-03-06 14:15:01 +01:00
parent c1c349a6f3
commit 4160deddc8
2 changed files with 27 additions and 23 deletions

View File

@ -9,8 +9,9 @@ from flask import Flask, jsonify
import yaml import yaml
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
from datetime import datetime
__VERSION__ = '0.5' __VERSION__ = '0.7'
app = Flask('app') app = Flask('app')
print("Pleroma2nip05 v%s" % (__VERSION__)) print("Pleroma2nip05 v%s" % (__VERSION__))
@ -19,16 +20,6 @@ with open(r'/etc/pleroma2nip05/config.yml') as configfile:
config = yaml.load(configfile, Loader=yaml.FullLoader) config = yaml.load(configfile, Loader=yaml.FullLoader)
# https://<domain>/.well-known/nostr.json # https://<domain>/.well-known/nostr.json
# {
# "names": {
# "bob": "b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9"
# },
# "relays": {
# "b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9": [ "wss://relay.example.com", "wss://relay2.example.com" ]
# }
# }
# {"names":{"gvs":"eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005"}}
try: try:
# Connect to an existing database # Connect to an existing database
@ -40,28 +31,40 @@ try:
cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor) cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
print("PostgreSQL server information") print("PostgreSQL server information")
print(connection.get_dsn_parameters(), "\n") print(connection.get_dsn_parameters(), "\n")
# cursor.execute("SELECT version();")
# # Fetch result
# record = cursor.fetchone()
# print("You are connected to - ", record, "\n")
except (Exception, Error) as error: except (Exception, Error) as error:
print("Error while connecting to PostgreSQL", error) print("Error while connecting to PostgreSQL", error)
# finally:
# if (connection):
# cursor.close()
# connection.close()
# print("PostgreSQL connection is closed")
# Get local users only # Get local users only
userquery = "SELECT nickname, fields FROM users WHERE nickname NOT LIKE '%@%';" userquery = "SELECT nickname, fields FROM users WHERE nickname NOT LIKE '%@%';"
relays = [ "wss://relay.rebelbase.site" , "wss://nostr-pub.wellorder.net" , "wss://relay.orangepill.dev", "wss://relay.shitforce.one" ] relays = [ "wss://relay.rebelbase.site" , "wss://nostr-pub.wellorder.net" , "wss://relay.orangepill.dev", "wss://relay.shitforce.one" ]
lastupdate = None
localusers = None
@app.route("/json") @app.route("/json")
def get_json(): def get_json():
cursor.execute(userquery) global lastupdate
localusers = cursor.fetchall() global localusers
global userquery
global userquery
print(lastupdate)
now = datetime.now()
if lastupdate is not None:
diff = (now - lastupdate)
age = diff.total_seconds()
else:
age = 9999999
if age > int(config.get('ttl')):
print('From DB')
cursor.execute(userquery)
localusers = cursor.fetchall()
lastupdate = now
else:
print('From cache')
nostr = {} nostr = {}
nostr['names'] = {} nostr['names'] = {}

View File

@ -2,3 +2,4 @@ pguser: pleroma
pgpass: pgpass:
pghost: localhost pghost: localhost
pleromadb: pleroma pleromadb: pleroma
ttl: 300