mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-17 21:31:52 +01:00
add one per follower dvm (beta)
This commit is contained in:
parent
a46b593df1
commit
13ca9c8938
4
.gitignore
vendored
4
.gitignore
vendored
@ -187,3 +187,7 @@ todo.txt
|
||||
.env_bkp
|
||||
output.txt
|
||||
tests/output.wav
|
||||
tests/db/Cashu/wallet.sqlite3
|
||||
tests/pagerank/index_map_99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64.json
|
||||
tests/pagerank/network_graph_99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64.json
|
||||
tests/test_data/wallet_mint_api/wallet.sqlite3
|
||||
|
236
nostr_dvm/tasks/content_discovery_latest_one_per_follower.py
Normal file
236
nostr_dvm/tasks/content_discovery_latest_one_per_follower.py
Normal file
@ -0,0 +1,236 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
from datetime import timedelta
|
||||
from threading import Thread
|
||||
|
||||
from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKey, NostrSigner, Kind, RelayOptions, \
|
||||
RelayLimits
|
||||
|
||||
from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv
|
||||
from nostr_dvm.utils.admin_utils import AdminConfig
|
||||
from nostr_dvm.utils.definitions import EventDefinitions
|
||||
from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config
|
||||
from nostr_dvm.utils.nip88_utils import NIP88Config
|
||||
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag
|
||||
from nostr_dvm.utils.output_utils import post_process_list_to_users
|
||||
|
||||
"""
|
||||
This File contains a Module to find inactive follows for a user on nostr
|
||||
|
||||
Accepted Inputs: None needed
|
||||
Outputs: A list of users that have been inactive
|
||||
Params: None
|
||||
"""
|
||||
|
||||
|
||||
class Discoverlatestperfollower(DVMTaskInterface):
|
||||
KIND: Kind = EventDefinitions.KIND_NIP90_CONTENT_DISCOVERY
|
||||
TASK: str = "content-discovery"
|
||||
FIX_COST: float = 0
|
||||
client: Client
|
||||
dvm_config: DVMConfig
|
||||
|
||||
async def init_dvm(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
|
||||
admin_config: AdminConfig = None, options=None):
|
||||
dvm_config.SCRIPT = os.path.abspath(__file__)
|
||||
|
||||
async def is_input_supported(self, tags, client=None, dvm_config=None):
|
||||
# no input required
|
||||
return True
|
||||
|
||||
async def create_request_from_nostr_event(self, event, client=None, dvm_config=None):
|
||||
self.dvm_config = dvm_config
|
||||
|
||||
request_form = {"jobID": event.id().to_hex()}
|
||||
|
||||
# default values
|
||||
user = event.author().to_hex()
|
||||
since_days = 30
|
||||
max_results = 200
|
||||
|
||||
for tag in event.tags():
|
||||
if tag.as_vec()[0] == 'param':
|
||||
param = tag.as_vec()[1]
|
||||
if param == "user": # check for param type
|
||||
user = tag.as_vec()[2]
|
||||
elif param == "since_days": # check for param type
|
||||
since_days = int(tag.as_vec()[2])
|
||||
|
||||
options = {
|
||||
"user": user,
|
||||
"since_days": since_days,
|
||||
"max_results": max_results
|
||||
}
|
||||
request_form['options'] = json.dumps(options)
|
||||
return request_form
|
||||
|
||||
async def process(self, request_form):
|
||||
from nostr_sdk import Filter
|
||||
from types import SimpleNamespace
|
||||
ns = SimpleNamespace()
|
||||
|
||||
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
|
||||
keys = Keys.parse(sk.to_hex())
|
||||
signer = NostrSigner.keys(keys)
|
||||
|
||||
# relaylimits = RelayLimits().event_max_num_tags(max_num_tags=10000)
|
||||
# relaylimits.event_max_size(None)
|
||||
relaylimits = RelayLimits.disable()
|
||||
|
||||
opts = (
|
||||
Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))).relay_limits(
|
||||
relaylimits)
|
||||
|
||||
cli = Client.with_opts(signer, opts)
|
||||
for relay in self.dvm_config.RELAY_LIST:
|
||||
await cli.add_relay(relay)
|
||||
ropts = RelayOptions().ping(False)
|
||||
await cli.add_relay_with_opts("wss://nostr.band", ropts)
|
||||
|
||||
await cli.connect()
|
||||
|
||||
options = self.set_options(request_form)
|
||||
step = 20
|
||||
|
||||
followers_filter = Filter().author(PublicKey.parse(options["user"])).kind(Kind(3))
|
||||
followers = await cli.get_events_of([followers_filter], timedelta(seconds=5))
|
||||
|
||||
if len(followers) > 0:
|
||||
result_list = []
|
||||
newest = 0
|
||||
best_entry = followers[0]
|
||||
for entry in followers:
|
||||
print(len(best_entry.tags()))
|
||||
print(best_entry.created_at().as_secs())
|
||||
if entry.created_at().as_secs() > newest:
|
||||
newest = entry.created_at().as_secs()
|
||||
best_entry = entry
|
||||
|
||||
print(best_entry.as_json())
|
||||
print(len(best_entry.tags()))
|
||||
print(best_entry.created_at().as_secs())
|
||||
print(Timestamp.now().as_secs())
|
||||
followings = []
|
||||
ns.dic = {}
|
||||
tagcount = 0
|
||||
for tag in best_entry.tags():
|
||||
tagcount += 1
|
||||
if tag.as_vec()[0] == "p":
|
||||
following = tag.as_vec()[1]
|
||||
followings.append(following)
|
||||
ns.dic[following] = None
|
||||
print("Followings: " + str(len(followings)) + " Tags: " + str(tagcount))
|
||||
|
||||
not_active_since_seconds = int(options["since_days"]) * 24 * 60 * 60
|
||||
dif = Timestamp.now().as_secs() - not_active_since_seconds
|
||||
not_active_since = Timestamp.from_secs(dif)
|
||||
|
||||
async def scanList(users, instance, i, st, notactivesince):
|
||||
from nostr_sdk import Filter
|
||||
|
||||
keys = Keys.parse(self.dvm_config.PRIVATE_KEY)
|
||||
opts = Options().wait_for_send(True).send_timeout(
|
||||
timedelta(seconds=5)).skip_disconnected_relays(True)
|
||||
signer = NostrSigner.keys(keys)
|
||||
cli = Client.with_opts(signer, opts)
|
||||
for relay in self.dvm_config.RELAY_LIST:
|
||||
await cli.add_relay(relay)
|
||||
await cli.connect()
|
||||
|
||||
filters = []
|
||||
for i in range(i, i + st):
|
||||
filter1 = (Filter().author(PublicKey.from_hex(users[i])).since(notactivesince).kind(Kind(1))
|
||||
.limit(1))
|
||||
filters.append(filter1)
|
||||
event_from_authors = await cli.get_events_of(filters, timedelta(seconds=10))
|
||||
for author in event_from_authors:
|
||||
instance.dic[author.author().to_hex()] = author
|
||||
print(str(i) + "/" + str(len(users)))
|
||||
await cli.shutdown()
|
||||
|
||||
threads = []
|
||||
begin = 0
|
||||
# Spawn some threads to speed things up
|
||||
while begin < len(followings) - step:
|
||||
t = Thread(target=asyncio.run, args=(scanList(followings, ns, begin, step, not_active_since),))
|
||||
threads.append(t)
|
||||
begin = begin + step - 1
|
||||
|
||||
# last to step size
|
||||
missing_scans = (len(followings) - begin)
|
||||
t = Thread(target=asyncio.run, args=(scanList(followings, ns, begin, missing_scans, not_active_since),))
|
||||
|
||||
threads.append(t)
|
||||
|
||||
# Start all threads
|
||||
for x in threads:
|
||||
x.start()
|
||||
|
||||
# Wait for all of them to finish
|
||||
for x in threads:
|
||||
x.join()
|
||||
|
||||
result = {v for (k, v) in ns.dic.items() if v is not None}
|
||||
#print(result)
|
||||
result = sorted(result, key=lambda x: x.created_at().as_secs(), reverse=True)[:int(options["max_results"])]
|
||||
#result = {v.id().to_hex() for (k, v) in finallist_sorted if v is not None}
|
||||
|
||||
#[: int(options["max_results"])]
|
||||
print("Inactive accounts found: " + str(len(result)))
|
||||
for v in result:
|
||||
e_tag = Tag.parse(["e", v.id().to_hex()])
|
||||
result_list.append(e_tag.as_vec())
|
||||
|
||||
return json.dumps(result_list)
|
||||
|
||||
async def post_process(self, result, event):
|
||||
"""Overwrite the interface function to return a social client readable format, if requested"""
|
||||
for tag in event.tags():
|
||||
if tag.as_vec()[0] == 'output':
|
||||
format = tag.as_vec()[1]
|
||||
if format == "text/plain": # check for output type
|
||||
result = post_process_list_to_users(result)
|
||||
|
||||
# if not text/plain, don't post-process
|
||||
return result
|
||||
|
||||
|
||||
# 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):
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
admin_config.LUD16 = dvm_config.LN_ADDRESS
|
||||
# Add NIP89
|
||||
nip89info = {
|
||||
"name": name,
|
||||
"image": "https://i.nostr.build/H6SMmCl7eRDvkbAn.jpg",
|
||||
"about": "I discover users you follow, but that have been inactive on Nostr",
|
||||
"action": "unfollow", # follow, mute, unmute
|
||||
"encryptionSupported": True,
|
||||
"cashuAccepted": True,
|
||||
"nip90Params": {
|
||||
"user": {
|
||||
"required": False,
|
||||
"values": [],
|
||||
"description": "Do the task for another user"
|
||||
},
|
||||
"since_days": {
|
||||
"required": False,
|
||||
"values": [],
|
||||
"description": "The number of days a user has not been active to be considered inactive"
|
||||
}
|
||||
}
|
||||
}
|
||||
nip89config = NIP89Config()
|
||||
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"])
|
||||
nip89config.CONTENT = json.dumps(nip89info)
|
||||
|
||||
return Discoverlatestperfollower(name=name, dvm_config=dvm_config, nip89config=nip89config,
|
||||
admin_config=admin_config)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
process_venv(Discoverlatestperfollower)
|
@ -51,6 +51,8 @@ class EventDefinitions:
|
||||
KIND_NIP88_SUBSCRIBE_EVENT = Kind(7001)
|
||||
KIND_NIP88_STOP_SUBSCRIPTION_EVENT = Kind(7002)
|
||||
KIND_NIP88_PAYMENT_RECIPE = Kind(7003)
|
||||
KIND_NIP60_NUT_PROOF = Kind(7375)
|
||||
KIND_NIP60_NUT_HISTORY = Kind(7376)
|
||||
KIND_ZAP = Kind(9735)
|
||||
KIND_RELAY_ANNOUNCEMENT = Kind(10002)
|
||||
KIND_ANNOUNCEMENT = Kind(31990)
|
||||
|
@ -15,7 +15,7 @@ class DVMConfig:
|
||||
FIX_COST: float = None
|
||||
PER_UNIT_COST: float = None
|
||||
|
||||
RELAY_LIST = ["wss://dvms.f7z.io", "wss://relay.primal.net",
|
||||
RELAY_LIST = ["wss://relay.primal.net",
|
||||
"wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg",
|
||||
"wss://relay.nostr.net"
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user