83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
#!/bin/env python3
|
|
|
|
# Pleroma2nip05: Server nip05 users for Pleroma users
|
|
# AGPL v3.0 or later licensed
|
|
# Copyright (C) Taurix IT 2023
|
|
|
|
import json
|
|
from flask import Flask, jsonify
|
|
import yaml
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
|
|
__VERSION__ = '0.5'
|
|
|
|
app = Flask('app')
|
|
print("Pleroma2nip05 v%s" % (__VERSION__))
|
|
|
|
with open(r'/etc/pleroma2nip05/config.yml') as configfile:
|
|
config = yaml.load(configfile, Loader=yaml.FullLoader)
|
|
|
|
# https://<domain>/.well-known/nostr.json
|
|
# {
|
|
# "names": {
|
|
# "bob": "b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9"
|
|
# },
|
|
# "relays": {
|
|
# "b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9": [ "wss://relay.example.com", "wss://relay2.example.com" ]
|
|
# }
|
|
# }
|
|
|
|
# {"names":{"gvs":"eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005"}}
|
|
|
|
try:
|
|
# Connect to an existing database
|
|
connection = psycopg2.connect(user=config.get('pguser'),
|
|
password=config.get('pgpass'),
|
|
host=config.get('pghost'),
|
|
database=config.get('pleromadb'))
|
|
|
|
cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
|
|
print("PostgreSQL server information")
|
|
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:
|
|
print("Error while connecting to PostgreSQL", error)
|
|
# finally:
|
|
# if (connection):
|
|
# cursor.close()
|
|
# connection.close()
|
|
# print("PostgreSQL connection is closed")
|
|
|
|
|
|
# Get local users only
|
|
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" ]
|
|
|
|
@app.route("/json")
|
|
def get_json():
|
|
cursor.execute(userquery)
|
|
localusers = cursor.fetchall()
|
|
|
|
nostr = {}
|
|
nostr['names'] = {}
|
|
if localusers is not None:
|
|
for user in localusers:
|
|
if user['nickname'] == 'gvs':
|
|
if user.get('fields') is not None:
|
|
for field in user.get('fields'):
|
|
if field.get('name').lower() == 'nostr':
|
|
nostr['names'][user['nickname']] = field.get('value')
|
|
|
|
|
|
# nostr['names']['gvs'] = 'eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005'
|
|
nostr['relays'] = {}
|
|
nostr['relays']['eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005'] = relays
|
|
|
|
print(json.dumps(nostr))
|
|
return json.dumps(nostr)
|