add dunbar option to wotm, handle exceptions on send

This commit is contained in:
Believethehype
2024-06-20 08:11:47 +02:00
parent 6e73e56959
commit 3ce318613b
3 changed files with 25 additions and 20 deletions

View File

@ -84,6 +84,7 @@ class DiscoverPeopleWOT(DVMTaskInterface):
user = event.author().to_hex() user = event.author().to_hex()
print(user) print(user)
hops = 2 hops = 2
dunbar = 1000
for tag in event.tags(): for tag in event.tags():
if tag.as_vec()[0] == 'i': if tag.as_vec()[0] == 'i':
@ -98,11 +99,15 @@ class DiscoverPeopleWOT(DVMTaskInterface):
elif param == "hops": # check for param type elif param == "hops": # check for param type
hops = int(tag.as_vec()[2]) hops = int(tag.as_vec()[2])
print(hops) print(hops)
elif param == "dunbar": # check for param type
dunbar = int(tag.as_vec()[2])
print(dunbar)
options = { options = {
"user": user, "user": user,
"max_results": max_results, "max_results": max_results,
"hops": hops, "hops": hops,
"dunbar": dunbar,
} }
request_form['options'] = json.dumps(options) request_form['options'] = json.dumps(options)
return request_form return request_form
@ -115,7 +120,7 @@ class DiscoverPeopleWOT(DVMTaskInterface):
else: else:
return self.result return self.result
async def calculate_hop(self, options, list_users, hop): async def calculate_hop(self, options, list_users, hop, dunbar):
print("Fetching hop: " + str(hop)) print("Fetching hop: " + str(hop))
file = "db/" + options["user"] + ".csv" file = "db/" + options["user"] + ".csv"
friendlist = [] friendlist = []
@ -123,7 +128,7 @@ class DiscoverPeopleWOT(DVMTaskInterface):
for npub in friend.friends: for npub in friend.friends:
friendlist.append(npub) friendlist.append(npub)
print(len(friendlist)) print(len(friendlist))
user_friends_next_level = await analyse_users(friendlist) user_friends_next_level = await analyse_users(friendlist, dunbar)
write_to_csv(user_friends_next_level, file) write_to_csv(user_friends_next_level, file)
return user_friends_next_level return user_friends_next_level
@ -145,7 +150,7 @@ class DiscoverPeopleWOT(DVMTaskInterface):
user_id = PublicKey.parse(options["user"]).to_hex() user_id = PublicKey.parse(options["user"]).to_hex()
user_friends_level1 = await analyse_users([user_id]) user_friends_level1 = await analyse_users([user_id]) # for the first user, ignore dunbar, thats the user after all.
friendlist = [] friendlist = []
for npub in user_friends_level1[0].friends: for npub in user_friends_level1[0].friends:
friendlist.append(npub) friendlist.append(npub)
@ -153,7 +158,7 @@ class DiscoverPeopleWOT(DVMTaskInterface):
write_to_csv(levelusers, file) write_to_csv(levelusers, file)
for i in range(1, int(options["hops"])): for i in range(1, int(options["hops"])):
levelusers = await self.calculate_hop(options, levelusers, i) levelusers = await self.calculate_hop(options, levelusers, i, int(options["dunbar"]))
df = pd.read_csv(file, sep=',') df = pd.read_csv(file, sep=',')
df.info() df.info()
@ -164,12 +169,6 @@ class DiscoverPeopleWOT(DVMTaskInterface):
pr = nx.pagerank(G_fb) pr = nx.pagerank(G_fb)
# Use this to find people your followers follow # Use this to find people your followers follow
user_id = PublicKey.parse(options["user"]).to_hex()
user_friends_level1 = await analyse_users([user_id])
friendlist = []
for npub in user_friends_level1[0].friends:
friendlist.append(npub)
sorted_nodes = sorted([(node, pagerank) for node, pagerank in pr.items() if node not in friendlist], sorted_nodes = sorted([(node, pagerank) for node, pagerank in pr.items() if node not in friendlist],
key=lambda x: pr[x[0]], key=lambda x: pr[x[0]],
reverse=True)[:int(options["max_results"])] reverse=True)[:int(options["max_results"])]
@ -244,7 +243,7 @@ class DiscoverPeopleWOT(DVMTaskInterface):
self.db_since) + " seconds..") self.db_since) + " seconds..")
async def analyse_users(user_ids=None): async def analyse_users(user_ids=None, dunbar=100000000):
if user_ids is None: if user_ids is None:
user_ids = [] user_ids = []
try: try:
@ -263,10 +262,13 @@ async def analyse_users(user_ids=None):
if len(followers) > 0: if len(followers) > 0:
for follower in followers: for follower in followers:
frens = [] frens = []
for tag in follower.tags(): if len(follower.tags()) < dunbar:
if tag.as_vec()[0] == "p": for tag in follower.tags():
frens.append(tag.as_vec()[1]) if tag.as_vec()[0] == "p":
allfriends.append(Friend(follower.author().to_hex(), frens)) frens.append(tag.as_vec()[1])
allfriends.append(Friend(follower.author().to_hex(), frens))
else:
print("Skipping friend: " + follower.author().to_hex() + "Following: " + str(len(follower.tags())) + " npubs")
return allfriends return allfriends
else: else:

View File

@ -7,7 +7,7 @@ from typing import List
import dotenv import dotenv
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt, Metadata, Options, \ from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt, Metadata, Options, \
Nip19Event, SingleLetterTag, RelayOptions, RelayLimits, SecretKey, NostrSigner Nip19Event, SingleLetterTag, RelayOptions, RelayLimits, SecretKey, NostrSigner, SendEventOutput
from nostr_dvm.utils.definitions import EventDefinitions from nostr_dvm.utils.definitions import EventDefinitions
@ -202,7 +202,7 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
async def send_event(event: Event, client: Client, dvm_config, blastr=False) -> EventId: async def send_event(event: Event, client: Client, dvm_config, blastr=False) -> SendEventOutput | None:
try: try:
relays = [] relays = []
for tag in event.tags(): for tag in event.tags():
@ -223,8 +223,11 @@ async def send_event(event: Event, client: Client, dvm_config, blastr=False) ->
#if blastr: #if blastr:
# client.add_relay("wss://nostr.mutinywallet.com") # client.add_relay("wss://nostr.mutinywallet.com")
try:
event_id = await client.send_event(event) event_id = await client.send_event(event)
except Exception as e:
print(e)
event_id = None
for relay in relays: for relay in relays:
if relay not in dvm_config.RELAY_LIST: if relay not in dvm_config.RELAY_LIST:

View File

@ -1,6 +1,6 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = '0.6.17' VERSION = '0.6.18'
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines' 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') LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')