mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-12-05 14:41:54 +01:00
refactor
This commit is contained in:
@@ -32,6 +32,7 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
|
||||
min_reactions = 2
|
||||
personalized = False
|
||||
result = ""
|
||||
logger = False
|
||||
|
||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
|
||||
admin_config: AdminConfig = None, options=None):
|
||||
@@ -46,17 +47,18 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
|
||||
self.request_form['options'] = json.dumps(opts)
|
||||
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if self.options is not None:
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
if self.options.get("logger"):
|
||||
self.logger = bool(self.options.get("logger"))
|
||||
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
if self.logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
|
||||
|
||||
use_logger = False
|
||||
if use_logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
|
||||
@@ -111,16 +113,10 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
|
||||
|
||||
options = DVMTaskInterface.set_options(request_form)
|
||||
|
||||
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)))
|
||||
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
|
||||
keys = Keys.parse(sk.to_hex())
|
||||
signer = NostrSigner.keys(keys)
|
||||
|
||||
database = NostrDatabase.sqlite(self.db_name)
|
||||
cli = ClientBuilder().database(database).signer(signer).opts(opts).build()
|
||||
cli = ClientBuilder().database(database).build()
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.connect()
|
||||
|
||||
# Negentropy reconciliation
|
||||
# Query events from database
|
||||
@@ -167,7 +163,11 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
self.result = self.calculate_result(self.request_form)
|
||||
if not self.personalized:
|
||||
try:
|
||||
self.result = self.calculate_result(self.request_form)
|
||||
except Exception as e:
|
||||
print("EXCEPTION: "+ e)
|
||||
return 1
|
||||
|
||||
def sync_db(self):
|
||||
@@ -180,10 +180,8 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.add_relay("wss://nostr.oxtr.dev")
|
||||
cli.add_relay("wss://relay.nostr.net")
|
||||
cli.add_relay("wss://relay.nostr.bg")
|
||||
cli.add_relay("wss://nostr.wine")
|
||||
cli.add_relay("wss://nostr21.com")
|
||||
|
||||
cli.connect()
|
||||
|
||||
timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since
|
||||
|
||||
@@ -33,6 +33,7 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
min_reactions = 1
|
||||
personalized = False
|
||||
result = ""
|
||||
logger = False
|
||||
|
||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
|
||||
admin_config: AdminConfig = None, options=None):
|
||||
@@ -47,16 +48,17 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
self.request_form['options'] = json.dumps(opts)
|
||||
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if self.options is not None:
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
if self.options.get("logger"):
|
||||
self.logger = bool(self.options.get("logger"))
|
||||
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
|
||||
use_logger = False
|
||||
if use_logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
|
||||
if self.logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
print("UPDATEDB: " + str(self.dvm_config.UPDATE_DATABASE))
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
|
||||
@@ -79,8 +81,7 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
request_form = {"jobID": event.id().to_hex()}
|
||||
|
||||
# default values
|
||||
search = ""
|
||||
max_results = 100
|
||||
max_results = 200
|
||||
|
||||
for tag in event.tags():
|
||||
if tag.as_vec()[0] == 'i':
|
||||
@@ -111,16 +112,8 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
|
||||
options = DVMTaskInterface.set_options(request_form)
|
||||
|
||||
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)))
|
||||
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
|
||||
keys = Keys.parse(sk.to_hex())
|
||||
signer = NostrSigner.keys(keys)
|
||||
|
||||
database = NostrDatabase.sqlite(self.db_name)
|
||||
cli = ClientBuilder().database(database).signer(signer).opts(opts).build()
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.connect()
|
||||
cli = ClientBuilder().database(database).build()
|
||||
|
||||
# Negentropy reconciliation
|
||||
# Query events from database
|
||||
@@ -139,13 +132,13 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
if len(reactions) >= self.min_reactions:
|
||||
for reaction in reactions:
|
||||
for tag in reaction.tags():
|
||||
#print(tag.as_vec())
|
||||
# print(tag.as_vec())
|
||||
if tag.as_vec()[0] == 'bolt11':
|
||||
#print(tag.as_vec()[1])
|
||||
# print(tag.as_vec()[1])
|
||||
invoice_amount = parse_amount_from_bolt11_invoice(tag.as_vec()[1])
|
||||
#print(invoice_amount)
|
||||
# print(invoice_amount)
|
||||
if tag.as_vec()[0] == 'preimage':
|
||||
haspreimage = True # TODO further check preimage
|
||||
haspreimage = True # TODO further check preimage
|
||||
if haspreimage:
|
||||
ns.finallist[event.id().to_hex()] = invoice_amount
|
||||
|
||||
@@ -174,10 +167,14 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
return 0
|
||||
else:
|
||||
if Timestamp.now().as_secs() >= self.last_schedule + dvm_config.SCHEDULE_UPDATES_SECONDS:
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
self.result = self.calculate_result(self.request_form)
|
||||
try:
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if not self.personalized:
|
||||
self.result = self.calculate_result(self.request_form)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return 1
|
||||
|
||||
def sync_db(self):
|
||||
@@ -189,6 +186,9 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
|
||||
cli = ClientBuilder().signer(signer).database(database).opts(opts).build()
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.add_relay("wss://nostr.oxtr.dev")
|
||||
cli.add_relay("wss://nostr21.com")
|
||||
|
||||
cli.connect()
|
||||
|
||||
timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since
|
||||
|
||||
@@ -31,6 +31,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
db_since = 2 * 3600
|
||||
db_name = "db/nostr_recent_notes2.db"
|
||||
min_reactions = 2
|
||||
logger = False
|
||||
|
||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
|
||||
admin_config: AdminConfig = None, options=None):
|
||||
@@ -39,15 +40,16 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
admin_config=admin_config, options=options)
|
||||
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if self.options is not None:
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
if self.options.get("logger"):
|
||||
self.logger = bool(self.options.get("logger"))
|
||||
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
|
||||
use_logger = False
|
||||
if use_logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
if self.logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
@@ -71,7 +73,6 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
user = event.author().to_hex()
|
||||
max_results = 100
|
||||
|
||||
|
||||
for tag in event.tags():
|
||||
if tag.as_vec()[0] == 'i':
|
||||
input_type = tag.as_vec()[2]
|
||||
@@ -82,8 +83,6 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
elif param == "user": # check for param type
|
||||
user = tag.as_vec()[2]
|
||||
|
||||
|
||||
|
||||
options = {
|
||||
"max_results": max_results,
|
||||
"user": user,
|
||||
@@ -97,12 +96,14 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
ns = SimpleNamespace()
|
||||
|
||||
options = DVMTaskInterface.set_options(request_form)
|
||||
|
||||
relaylimits = RelayLimits.disable()
|
||||
opts = (Options().wait_for_send(True).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)).relay_limits(relaylimits))
|
||||
opts = (
|
||||
Options().wait_for_send(True).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)).relay_limits(
|
||||
relaylimits))
|
||||
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
|
||||
keys = Keys.parse(sk.to_hex())
|
||||
signer = NostrSigner.keys(keys)
|
||||
|
||||
database = NostrDatabase.sqlite(self.db_name)
|
||||
cli = ClientBuilder().database(database).signer(signer).opts(opts).build()
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
@@ -113,18 +114,18 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
cli.add_relay_with_opts("wss://nostr.band", ropts)
|
||||
|
||||
cli.connect()
|
||||
cli = ClientBuilder().database(database).build()
|
||||
|
||||
user = PublicKey.parse(options["user"])
|
||||
followers_filter = Filter().author(user).kinds([Kind(3)])
|
||||
followers = cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
|
||||
#print(followers)
|
||||
cli = ClientBuilder().database(database).build()
|
||||
|
||||
# Negentropy reconciliation
|
||||
# Query events from database
|
||||
timestamp_since = Timestamp.now().as_secs() - self.db_since
|
||||
since = Timestamp.from_secs(timestamp_since)
|
||||
|
||||
|
||||
result_list = []
|
||||
|
||||
if len(followers) > 0:
|
||||
@@ -135,7 +136,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
newest = entry.created_at().as_secs()
|
||||
best_entry = entry
|
||||
|
||||
#print(best_entry.as_json())
|
||||
# print(best_entry.as_json())
|
||||
followings = []
|
||||
for tag in best_entry.tags():
|
||||
if tag.as_vec()[0] == "p":
|
||||
@@ -147,19 +148,17 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
|
||||
ns.finallist = {}
|
||||
for event in events:
|
||||
#if event.created_at().as_secs() > timestamp_since:
|
||||
# if event.created_at().as_secs() > timestamp_since:
|
||||
filt = Filter().kinds(
|
||||
[definitions.EventDefinitions.KIND_ZAP, definitions.EventDefinitions.KIND_REACTION, definitions.EventDefinitions.KIND_REPOST,
|
||||
definitions.EventDefinitions.KIND_NOTE]).event(event.id()).since(since)
|
||||
[EventDefinitions.KIND_ZAP, EventDefinitions.KIND_REACTION, EventDefinitions.KIND_REPOST,
|
||||
EventDefinitions.KIND_NOTE]).event(event.id()).since(since)
|
||||
reactions = cli.database().query([filt])
|
||||
if len(reactions) >= self.min_reactions:
|
||||
ns.finallist[event.id().to_hex()] = len(reactions)
|
||||
|
||||
|
||||
|
||||
finallist_sorted = sorted(ns.finallist.items(), key=lambda x: x[1], reverse=True)[:int(options["max_results"])]
|
||||
finallist_sorted = sorted(ns.finallist.items(), key=lambda x: x[1], reverse=True)[
|
||||
:int(options["max_results"])]
|
||||
for entry in finallist_sorted:
|
||||
# print(EventId.parse(entry[0]).to_bech32() + "/" + EventId.parse(entry[0]).to_hex() + ": " + str(entry[1]))
|
||||
e_tag = Tag.parse(["e", entry[0]])
|
||||
result_list.append(e_tag.as_vec())
|
||||
|
||||
@@ -186,6 +185,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
|
||||
return 1
|
||||
|
||||
def sync_db(self):
|
||||
@@ -197,6 +197,9 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
cli = ClientBuilder().signer(signer).database(database).opts(opts).build()
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.add_relay("wss://nostr.oxtr.dev")
|
||||
cli.add_relay("wss://nostr21.com")
|
||||
|
||||
cli.connect()
|
||||
|
||||
timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since
|
||||
@@ -206,19 +209,22 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
|
||||
definitions.EventDefinitions.KIND_ZAP]).since(lasthour) # Notes, reactions, zaps
|
||||
|
||||
# filter = Filter().author(keys.public_key())
|
||||
print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(self.db_since) + " seconds.. this might take a while..")
|
||||
print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(
|
||||
self.db_since) + " seconds.. this might take a while..")
|
||||
dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN)
|
||||
cli.reconcile(filter1, dbopts)
|
||||
database.delete(Filter().until(Timestamp.from_secs(
|
||||
Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesnt get too full.
|
||||
|
||||
print("[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")
|
||||
print(
|
||||
"[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")
|
||||
|
||||
|
||||
# We build an example here that we can call by either calling this file directly from the main directory,
|
||||
# or by adding it to our playground. You can call the example and adjust it to your needs or redefine it in the
|
||||
# playground or elsewhere
|
||||
def build_example(name, identifier, admin_config, options, cost=0, update_rate=300, processing_msg=None, update_db=True):
|
||||
def build_example(name, identifier, admin_config, options, cost=0, update_rate=300, processing_msg=None,
|
||||
update_db=True):
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
dvm_config.SHOWLOG = True
|
||||
@@ -259,7 +265,8 @@ def build_example(name, identifier, admin_config, options, cost=0, update_rate=
|
||||
# admin_config.UPDATE_PROFILE = False
|
||||
# admin_config.REBROADCAST_NIP89 = False
|
||||
|
||||
return DicoverContentCurrentlyPopularFollowers(name=name, dvm_config=dvm_config, nip89config=nip89config, options=options,
|
||||
return DicoverContentCurrentlyPopularFollowers(name=name, dvm_config=dvm_config, nip89config=nip89config,
|
||||
options=options,
|
||||
admin_config=admin_config)
|
||||
|
||||
|
||||
|
||||
@@ -28,18 +28,18 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
dvm_config: DVMConfig
|
||||
last_schedule: int
|
||||
min_reactions = 2
|
||||
db_since = 10*3600
|
||||
db_since = 10 * 3600
|
||||
db_name = "db/nostr_default_recent_notes.db"
|
||||
search_list = []
|
||||
avoid_list = []
|
||||
must_list = []
|
||||
personalized = False
|
||||
result = ""
|
||||
logger = False
|
||||
|
||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
|
||||
admin_config: AdminConfig = None, options=None):
|
||||
|
||||
|
||||
super().__init__(name=name, dvm_config=dvm_config, nip89config=nip89config, nip88config=nip88config,
|
||||
admin_config=admin_config, options=options)
|
||||
|
||||
@@ -52,26 +52,26 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
self.request_form['options'] = json.dumps(opts)
|
||||
|
||||
dvm_config.SCRIPT = os.path.abspath(__file__)
|
||||
if self.options is not None:
|
||||
if self.options.get("personalized"):
|
||||
self.personalized = bool(self.options.get("personalized"))
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if self.options.get("search_list"):
|
||||
self.search_list = self.options.get("search_list")
|
||||
# print(self.search_list)
|
||||
if self.options.get("avoid_list"):
|
||||
self.avoid_list = self.options.get("avoid_list")
|
||||
if self.options.get("must_list"):
|
||||
self.must_list = self.options.get("must_list")
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
if self.options.get("logger"):
|
||||
self.logger = bool(self.options.get("logger"))
|
||||
|
||||
if self.options.get("personalized"):
|
||||
self.personalized = bool(self.options.get("personalized"))
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if self.options.get("search_list"):
|
||||
self.search_list = self.options.get("search_list")
|
||||
#print(self.search_list)
|
||||
if self.options.get("avoid_list"):
|
||||
self.avoid_list = self.options.get("avoid_list")
|
||||
if self.options.get("must_list"):
|
||||
self.must_list = self.options.get("must_list")
|
||||
if self.options.get("db_name"):
|
||||
self.db_name = self.options.get("db_name")
|
||||
if self.options.get("db_since"):
|
||||
self.db_since = int(self.options.get("db_since"))
|
||||
|
||||
|
||||
use_logger = False
|
||||
if use_logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
if self.logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
@@ -115,11 +115,10 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
# if the dvm supports individual results, recalculate it every time for the request
|
||||
if self.personalized:
|
||||
return self.calculate_result(request_form)
|
||||
#else return the result that gets updated once every schenduled update. In this case on database update.
|
||||
# else return the result that gets updated once every schenduled update. In this case on database update.
|
||||
else:
|
||||
return self.result
|
||||
|
||||
|
||||
def post_process(self, result, event):
|
||||
"""Overwrite the interface function to return a social client readable format, if requested"""
|
||||
for tag in event.tags():
|
||||
@@ -130,6 +129,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
|
||||
# if not text/plain, don't post-process
|
||||
return result
|
||||
|
||||
def calculate_result(self, request_form):
|
||||
from nostr_sdk import Filter
|
||||
from types import SimpleNamespace
|
||||
@@ -143,10 +143,10 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
signer = NostrSigner.keys(keys)
|
||||
|
||||
database = NostrDatabase.sqlite(self.db_name)
|
||||
cli = ClientBuilder().database(database).signer(signer).opts(opts).build()
|
||||
cli = ClientBuilder().database(database).build()
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.connect()
|
||||
# cli.add_relay("wss://relay.damus.io")
|
||||
# cli.connect()
|
||||
|
||||
# Negentropy reconciliation
|
||||
# Query events from database
|
||||
@@ -157,7 +157,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
|
||||
events = cli.database().query([filter1])
|
||||
print(len(events))
|
||||
ns.finallist = {}
|
||||
ns.final_list = {}
|
||||
|
||||
for event in events:
|
||||
if all(ele in event.content().lower() for ele in self.must_list):
|
||||
@@ -170,28 +170,30 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
definitions.EventDefinitions.KIND_NOTE]).event(event.id()).since(since)
|
||||
reactions = cli.database().query([filt])
|
||||
if len(reactions) >= self.min_reactions:
|
||||
ns.finallist[event.id().to_hex()] = len(reactions)
|
||||
ns.final_list[event.id().to_hex()] = len(reactions)
|
||||
|
||||
result_list = []
|
||||
finallist_sorted = sorted(ns.finallist.items(), key=lambda x: x[1], reverse=True)[:int(options["max_results"])]
|
||||
finallist_sorted = sorted(ns.final_list.items(), key=lambda x: x[1], reverse=True)[:int(options["max_results"])]
|
||||
for entry in finallist_sorted:
|
||||
# print(EventId.parse(entry[0]).to_bech32() + "/" + EventId.parse(entry[0]).to_hex() + ": " + str(entry[1]))
|
||||
e_tag = Tag.parse(["e", entry[0]])
|
||||
result_list.append(e_tag.as_vec())
|
||||
print(len(result_list))
|
||||
return json.dumps(result_list)
|
||||
|
||||
return json.dumps(result_list)
|
||||
|
||||
def schedule(self, dvm_config):
|
||||
if dvm_config.SCHEDULE_UPDATES_SECONDS == 0:
|
||||
return 0
|
||||
else:
|
||||
if Timestamp.now().as_secs() >= self.last_schedule + dvm_config.SCHEDULE_UPDATES_SECONDS:
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
self.result = self.calculate_result(self.request_form)
|
||||
#print(self.result)
|
||||
try:
|
||||
if self.dvm_config.UPDATE_DATABASE:
|
||||
self.sync_db()
|
||||
self.last_schedule = Timestamp.now().as_secs()
|
||||
if not self.personalized:
|
||||
self.result = self.calculate_result(self.request_form)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return 1
|
||||
|
||||
def sync_db(self):
|
||||
@@ -204,33 +206,33 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
|
||||
|
||||
cli.add_relay("wss://relay.damus.io")
|
||||
cli.add_relay("wss://nostr.oxtr.dev")
|
||||
cli.add_relay("wss://relay.nostr.net")
|
||||
cli.add_relay("wss://relay.nostr.bg")
|
||||
cli.add_relay("wss://nostr.wine")
|
||||
cli.add_relay("wss://nostr21.com")
|
||||
|
||||
|
||||
cli.connect()
|
||||
|
||||
timestamp_since = Timestamp.now().as_secs() - self.db_since
|
||||
since = Timestamp.from_secs(timestamp_since)
|
||||
|
||||
filter1 = Filter().kinds([definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION, definitions.EventDefinitions.KIND_ZAP]).since(since) # Notes, reactions, zaps
|
||||
filter1 = Filter().kinds([EventDefinitions.KIND_NOTE, EventDefinitions.KIND_REACTION, EventDefinitions.KIND_ZAP,
|
||||
EventDefinitions.KIND_LONGFORM]).since(since) # Notes, reactions, zaps
|
||||
|
||||
# filter = Filter().author(keys.public_key())
|
||||
print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(self.db_since) + " seconds.. this might take a while..")
|
||||
print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(
|
||||
self.db_since) + " seconds.. this might take a while..")
|
||||
dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN)
|
||||
cli.reconcile(filter1, dbopts)
|
||||
database.delete(Filter().until(Timestamp.from_secs(
|
||||
Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesn't get too full.
|
||||
|
||||
print("[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")
|
||||
print(
|
||||
"[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")
|
||||
|
||||
|
||||
# We build an example here that we can call by either calling this file directly from the main directory,
|
||||
# or by adding it to our playground. You can call the example and adjust it to your needs or redefine it in the
|
||||
# playground or elsewhere
|
||||
def build_example(name, identifier, admin_config, options, image, description, update_rate=600, cost=0, processing_msg=None, update_db=True):
|
||||
def build_example(name, identifier, admin_config, options, image, description, update_rate=600, cost=0,
|
||||
processing_msg=None, update_db=True):
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
dvm_config.SHOWLOG = True
|
||||
@@ -243,7 +245,6 @@ def build_example(name, identifier, admin_config, options, image, description, u
|
||||
dvm_config.CUSTOM_PROCESSING_MESSAGE = processing_msg
|
||||
admin_config.LUD16 = dvm_config.LN_ADDRESS
|
||||
|
||||
|
||||
# Add NIP89
|
||||
nip89info = {
|
||||
"name": name,
|
||||
@@ -269,10 +270,11 @@ def build_example(name, identifier, admin_config, options, image, description, u
|
||||
nip89config.CONTENT = json.dumps(nip89info)
|
||||
|
||||
return DicoverContentCurrentlyPopularbyTopic(name=name, dvm_config=dvm_config, nip89config=nip89config,
|
||||
admin_config=admin_config, options=options)
|
||||
admin_config=admin_config, options=options)
|
||||
|
||||
|
||||
def build_example_subscription(name, identifier, admin_config, options, image, description, processing_msg=None, update_db=True):
|
||||
def build_example_subscription(name, identifier, admin_config, options, image, description, processing_msg=None,
|
||||
update_db=True):
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
dvm_config.SHOWLOG = True
|
||||
@@ -283,7 +285,6 @@ def build_example_subscription(name, identifier, admin_config, options, image, d
|
||||
dvm_config.CUSTOM_PROCESSING_MESSAGE = processing_msg
|
||||
admin_config.LUD16 = dvm_config.LN_ADDRESS
|
||||
|
||||
|
||||
# Add NIP89
|
||||
nip89info = {
|
||||
"name": name,
|
||||
@@ -321,11 +322,10 @@ def build_example_subscription(name, identifier, admin_config, options, image, d
|
||||
nip88config.PERK2DESC = "Support NostrDVM & NostrSDK development"
|
||||
nip88config.PAYMENT_VERIFIER_PUBKEY = "5b5c045ecdf66fb540bdf2049fe0ef7f1a566fa427a4fe50d400a011b65a3a7e"
|
||||
|
||||
|
||||
return DicoverContentCurrentlyPopularbyTopic(name=name, dvm_config=dvm_config, nip89config=nip89config,
|
||||
nip88config=nip88config,
|
||||
admin_config=admin_config,
|
||||
options=options)
|
||||
nip88config=nip88config,
|
||||
admin_config=admin_config,
|
||||
options=options)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
from nostr_sdk import Tag, Kind
|
||||
from nostr_sdk import Tag, Kind, init_logger, LogLevel
|
||||
|
||||
from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv
|
||||
from nostr_dvm.utils.admin_utils import AdminConfig
|
||||
@@ -23,6 +23,7 @@ class TrendingNotesNostrBand(DVMTaskInterface):
|
||||
TASK: str = "trending-content"
|
||||
FIX_COST: float = 0
|
||||
dvm_config: DVMConfig
|
||||
logger = False
|
||||
|
||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
|
||||
admin_config: AdminConfig = None, options=None):
|
||||
@@ -30,6 +31,13 @@ class TrendingNotesNostrBand(DVMTaskInterface):
|
||||
super().__init__(name=name, dvm_config=dvm_config, nip89config=nip89config, nip88config=nip88config,
|
||||
admin_config=admin_config, options=options)
|
||||
|
||||
if self.options is not None:
|
||||
if self.options.get("logger"):
|
||||
self.logger = bool(self.options.get("logger"))
|
||||
|
||||
if self.logger:
|
||||
init_logger(LogLevel.DEBUG)
|
||||
|
||||
def is_input_supported(self, tags, client=None, dvm_config=None):
|
||||
for tag in tags:
|
||||
if tag.as_vec()[0] == 'i':
|
||||
|
||||
@@ -11,6 +11,7 @@ class EventDefinitions:
|
||||
KIND_REACTION = Kind(7)
|
||||
KIND_ZAP = Kind(9735)
|
||||
KIND_ANNOUNCEMENT = Kind(31990)
|
||||
KIND_LONGFORM = Kind(30023)
|
||||
KIND_NIP94_METADATA = Kind(1063)
|
||||
KIND_FEEDBACK = Kind(7000)
|
||||
KIND_NIP90_EXTRACT_TEXT = Kind(5000)
|
||||
|
||||
@@ -18,7 +18,7 @@ class DVMConfig:
|
||||
|
||||
RELAY_LIST = ["wss://dvms.f7z.io", "wss://relay.damus.io", "wss://nostr.wine",
|
||||
"wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg",
|
||||
"wss://relay.f7z.io", "wss://relay.nostr.net"
|
||||
"wss://relay.nostr.net"
|
||||
]
|
||||
|
||||
RELAY_TIMEOUT = 5
|
||||
|
||||
4
setup.py
4
setup.py
@@ -1,6 +1,6 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
VERSION = '0.4.9'
|
||||
VERSION = '0.5.0'
|
||||
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
|
||||
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')
|
||||
|
||||
@@ -13,7 +13,7 @@ setup(
|
||||
description=DESCRIPTION,
|
||||
long_description=LONG_DESCRIPTION,
|
||||
packages=find_packages(include=['nostr_dvm', 'nostr_dvm.*']),
|
||||
|
||||
#nostr-sdk==0.13.0a2.dev0
|
||||
install_requires=["nostr-sdk==0.12.0",
|
||||
"bech32",
|
||||
"pycryptodome==3.20.0",
|
||||
|
||||
@@ -18,6 +18,8 @@ from nostr_dvm.utils.nip89_utils import create_amount_tag, NIP89Config, check_an
|
||||
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
|
||||
from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys
|
||||
|
||||
global_update_rate = 180 # set this high on first sync so db can fully sync before another process trys to.
|
||||
|
||||
|
||||
def build_example_nostrband(name, identifier, admin_config, image, about, custom_processing_msg):
|
||||
dvm_config: DVMConfig = build_default_config(identifier)
|
||||
@@ -87,15 +89,13 @@ def build_example_popular(name, identifier, admin_config, options, image, cost=0
|
||||
update_db=True):
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
#dvm_config.SHOWLOG = True
|
||||
# dvm_config.SHOWLOG = True
|
||||
dvm_config.SCHEDULE_UPDATES_SECONDS = update_rate # Every 10 minutes
|
||||
dvm_config.UPDATE_DATABASE = update_db
|
||||
dvm_config.FIX_COST = cost
|
||||
dvm_config.CUSTOM_PROCESSING_MESSAGE = processing_msg
|
||||
admin_config.LUD16 = dvm_config.LN_ADDRESS
|
||||
|
||||
|
||||
|
||||
# Add NIP89
|
||||
nip89info = {
|
||||
"name": name,
|
||||
@@ -162,8 +162,10 @@ def build_example_popular_followers(name, identifier, admin_config, options, ima
|
||||
options=options,
|
||||
admin_config=admin_config)
|
||||
|
||||
def build_example_top_zapped(name, identifier, admin_config, options, image, cost=0, update_rate=180, processing_msg=None,
|
||||
update_db=True):
|
||||
|
||||
def build_example_top_zapped(name, identifier, admin_config, options, image, cost=0, update_rate=180,
|
||||
processing_msg=None,
|
||||
update_db=True):
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
dvm_config.SHOWLOG = True
|
||||
@@ -202,8 +204,10 @@ def build_example_top_zapped(name, identifier, admin_config, options, image, cos
|
||||
|
||||
return DicoverContentCurrentlyPopularZaps(name=name, dvm_config=dvm_config, nip89config=nip89config,
|
||||
admin_config=admin_config, options=options)
|
||||
|
||||
|
||||
def playground():
|
||||
rebbroadcast_NIP89 = True #Announce NIP89 on startup
|
||||
rebbroadcast_NIP89 = True # Announce NIP89 on startup
|
||||
use_logger = False
|
||||
|
||||
if use_logger:
|
||||
@@ -230,7 +234,7 @@ def playground():
|
||||
# Popular top zapped
|
||||
admin_config_top_zaps = AdminConfig()
|
||||
admin_config_top_zaps.REBROADCAST_NIP89 = rebbroadcast_NIP89
|
||||
admin_config_top_zaps.UPDATE_PROFILE = True
|
||||
admin_config_top_zaps.UPDATE_PROFILE = False
|
||||
custom_processing_msg = ["Looking for most zapped notes", "Let's see which notes people currently zap..",
|
||||
"Let's find valuable notes. #value4value"]
|
||||
update_db = False
|
||||
@@ -242,14 +246,14 @@ def playground():
|
||||
cost = 0
|
||||
image = "https://image.nostr.build/c6879f458252641d04d0aa65fd7f1e005a4f7362fd407467306edc2f4acdb113.jpg"
|
||||
discovery_topzaps = build_example_top_zapped("Top Zapped notes",
|
||||
"discovery_content_top_zaps",
|
||||
admin_config=admin_config_top_zaps,
|
||||
options= options_top_zapped,
|
||||
image=image,
|
||||
cost=cost,
|
||||
update_rate=180,
|
||||
processing_msg=custom_processing_msg,
|
||||
update_db=update_db)
|
||||
"discovery_content_top_zaps",
|
||||
admin_config=admin_config_top_zaps,
|
||||
options=options_top_zapped,
|
||||
image=image,
|
||||
cost=cost,
|
||||
update_rate=global_update_rate,
|
||||
processing_msg=custom_processing_msg,
|
||||
update_db=update_db)
|
||||
|
||||
discovery_topzaps.run()
|
||||
|
||||
@@ -257,10 +261,10 @@ def playground():
|
||||
admin_config_plants = AdminConfig()
|
||||
admin_config_plants.REBROADCAST_NIP89 = rebbroadcast_NIP89
|
||||
admin_config_plants.UPDATE_PROFILE = False
|
||||
#admin_config_plants.DELETE_NIP89 = True
|
||||
# admin_config_plants.DELETE_NIP89 = True
|
||||
# admin_config_plants.PRIVKEY = "430bacf525a2f6efd6db1f049eb7c04e0c0314182ef1c17df39f46fe66416ddf"
|
||||
# admin_config_plants.EVENTID = "f42adb15f4c67b884d58b09084907d94471d1a54185dce0217a69111c703aa14"
|
||||
#admin_config_plants.POW = True
|
||||
# admin_config_plants.POW = True
|
||||
options_plants = {
|
||||
"search_list": ["garden", "gardening", "nature", " plants ", " plant ", " herb ", " herbs " " pine ",
|
||||
"homesteading", "rosemary", "chicken", "🪻", "🌿", "☘️", "🌲", "flower", "forest", "watering",
|
||||
@@ -278,8 +282,9 @@ def playground():
|
||||
"nostr-hotter-site", " ai ", "palestine", "https://boards.4chan", "https://techcrunch.com",
|
||||
"https://screenrant.com"],
|
||||
"db_name": "db/nostr_recent_notes.db",
|
||||
"db_since": 12 * 60 * 60, # 10h since gmt
|
||||
"personalized": False}
|
||||
"db_since": 12 * 60 * 60, # 12h since gmt
|
||||
"personalized": False,
|
||||
"logger": False}
|
||||
|
||||
image = "https://image.nostr.build/a816f3f5e98e91e8a47d50f4cd7a2c17545f556d9bb0a6086a659b9abdf7ab68.jpg"
|
||||
description = "I show recent notes about plants and gardening"
|
||||
@@ -291,7 +296,7 @@ def playground():
|
||||
admin_config_plants, options_plants,
|
||||
image=image,
|
||||
description=description,
|
||||
update_rate=180,
|
||||
update_rate=global_update_rate,
|
||||
cost=cost,
|
||||
processing_msg=custom_processing_msg,
|
||||
update_db=update_db)
|
||||
@@ -331,7 +336,8 @@ def playground():
|
||||
"must_list": ["http"],
|
||||
"db_name": "db/nostr_recent_notes.db",
|
||||
"db_since": 48 * 60 * 60, # 48h since gmt,
|
||||
"personalized": False}
|
||||
"personalized": False,
|
||||
"logger": False}
|
||||
|
||||
image = "https://image.nostr.build/f609311532c470f663e129510a76c9a1912ae9bc4aaaf058e5ba21cfb512c88e.jpg"
|
||||
description = "I show recent notes about animals"
|
||||
@@ -345,7 +351,7 @@ def playground():
|
||||
admin_config_animals, options_animal,
|
||||
image=image,
|
||||
description=description,
|
||||
update_rate=180,
|
||||
update_rate=global_update_rate,
|
||||
cost=cost,
|
||||
processing_msg=custom_processing_msg,
|
||||
update_db=update_db)
|
||||
@@ -378,7 +384,7 @@ def playground():
|
||||
options=options_followers_popular,
|
||||
cost=cost,
|
||||
image=image,
|
||||
update_rate=180,
|
||||
update_rate=global_update_rate,
|
||||
processing_msg=custom_processing_msg,
|
||||
update_db=update_db)
|
||||
discovery_followers.run()
|
||||
@@ -406,7 +412,7 @@ def playground():
|
||||
options=options_global_popular,
|
||||
image=image,
|
||||
cost=cost,
|
||||
update_rate=180,
|
||||
update_rate=global_update_rate,
|
||||
processing_msg=custom_processing_msg,
|
||||
update_db=update_db)
|
||||
discovery_global.run()
|
||||
@@ -415,19 +421,19 @@ def playground():
|
||||
# discovery_test_sub.run()
|
||||
|
||||
# Subscription Manager DVM
|
||||
#subscription_config = DVMConfig()
|
||||
#subscription_config.PRIVATE_KEY = check_and_set_private_key("dvm_subscription")
|
||||
#npub = Keys.parse(subscription_config.PRIVATE_KEY).public_key().to_bech32()
|
||||
#invoice_key, admin_key, wallet_id, user_id, lnaddress = check_and_set_ln_bits_keys("dvm_subscription", npub)
|
||||
#subscription_config.LNBITS_INVOICE_KEY = invoice_key
|
||||
#subscription_config.LNBITS_ADMIN_KEY = admin_key # The dvm might pay failed jobs back
|
||||
#subscription_config.LNBITS_URL = os.getenv("LNBITS_HOST")
|
||||
#sub_admin_config = AdminConfig()
|
||||
#sub_admin_config.USERNPUBS = ["7782f93c5762538e1f7ccc5af83cd8018a528b9cd965048386ca1b75335f24c6"] #Add npubs of services that can contact the subscription handler
|
||||
# subscription_config = DVMConfig()
|
||||
# subscription_config.PRIVATE_KEY = check_and_set_private_key("dvm_subscription")
|
||||
# npub = Keys.parse(subscription_config.PRIVATE_KEY).public_key().to_bech32()
|
||||
# invoice_key, admin_key, wallet_id, user_id, lnaddress = check_and_set_ln_bits_keys("dvm_subscription", npub)
|
||||
# subscription_config.LNBITS_INVOICE_KEY = invoice_key
|
||||
# subscription_config.LNBITS_ADMIN_KEY = admin_key # The dvm might pay failed jobs back
|
||||
# subscription_config.LNBITS_URL = os.getenv("LNBITS_HOST")
|
||||
# sub_admin_config = AdminConfig()
|
||||
# sub_admin_config.USERNPUBS = ["7782f93c5762538e1f7ccc5af83cd8018a528b9cd965048386ca1b75335f24c6"] #Add npubs of services that can contact the subscription handler
|
||||
|
||||
# currently there is none, but add this once subscriptions are live.
|
||||
#x = threading.Thread(target=Subscription, args=(Subscription(subscription_config, sub_admin_config),))
|
||||
#x.start()
|
||||
# x = threading.Thread(target=Subscription, args=(Subscription(subscription_config, sub_admin_config),))
|
||||
# x.start()
|
||||
|
||||
# keep_alive()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user