update search engines

This commit is contained in:
Believethehype
2024-02-01 14:07:01 +01:00
parent d38a7cf183
commit 7a21454572
4 changed files with 45 additions and 48 deletions

View File

@@ -29,7 +29,8 @@ Current List of Tasks:
| TextToSpeech | 5250 | Generate Audio from a prompt | local | | TextToSpeech | 5250 | Generate Audio from a prompt | local |
| TrendingNotesNostrBand | 5300 | Show trending notes on nostr.band | nostr.band api | | TrendingNotesNostrBand | 5300 | Show trending notes on nostr.band | nostr.band api |
| DiscoverInactiveFollows | 5301 | Find inactive Nostr users | local | | DiscoverInactiveFollows | 5301 | Find inactive Nostr users | local |
| AdvancedSearch | 5302 | Search Content on nostr.band | local/nostr.band | | AdvancedSearch | 5302 | Search Content on relays (nostr.band) | local/nostr.band |
| AdvancedSearchWine | 5302 | Search Content on nostr.wine | api/nostr.wine |
Kinds with (inoff) are suggestions and not merged yet and might change in the future. Kinds with (inoff) are suggestions and not merged yet and might change in the future.
Backends might require to add an API key to the .env file or run an external server/framework the dvm will communicate with. Backends might require to add an API key to the .env file or run an external server/framework the dvm will communicate with.

View File

@@ -48,10 +48,13 @@ class AdvancedSearch(DVMTaskInterface):
# default values # default values
user = "" user = ""
users = [] users = []
since_days = 800 # days ago since_seconds = Timestamp.now().as_secs() - (365 * 24 * 60 * 60)
until_days = 0 # days ago until_seconds = Timestamp.now().as_secs()
search = "" search = ""
max_results = 20 max_results = 100
relay = "wss://relay.nostr.band"
if self.options.get("relay") and self.options.get("relay") != "":
relay = self.options['relay']
for tag in event.tags(): for tag in event.tags():
if tag.as_vec()[0] == 'i': if tag.as_vec()[0] == 'i':
@@ -61,23 +64,24 @@ class AdvancedSearch(DVMTaskInterface):
elif tag.as_vec()[0] == 'param': elif tag.as_vec()[0] == 'param':
param = tag.as_vec()[1] param = tag.as_vec()[1]
if param == "user": # check for param type if param == "user": # check for param type
user = tag.as_vec()[2] #user = tag.as_vec()[2]
users.append(Tag.parse(["p", tag.as_vec()[2]]))
elif param == "users": # check for param type elif param == "users": # check for param type
users = json.loads(tag.as_vec()[2]) users = json.loads(tag.as_vec()[2])
elif param == "since": # check for param type elif param == "since": # check for param type
since_days = int(tag.as_vec()[2]) since_seconds = int(tag.as_vec()[2])
elif param == "until": # check for param type elif param == "until": # check for param type
until_days = int(tag.as_vec()[2]) until_seconds = min(int(tag.as_vec()[2]), until_seconds)
elif param == "max_results": # check for param type elif param == "max_results": # check for param type
max_results = int(tag.as_vec()[2]) max_results = int(tag.as_vec()[2])
options = { options = {
"search": search, "search": search,
"user": user,
"users": users, "users": users,
"since": since_days, "since": since_seconds,
"until": until_days, "until": until_seconds,
"max_results": max_results "max_results": max_results,
"relay": relay
} }
request_form['options'] = json.dumps(options) request_form['options'] = json.dumps(options)
return request_form return request_form
@@ -92,16 +96,18 @@ class AdvancedSearch(DVMTaskInterface):
signer = ClientSigner.keys(keys) signer = ClientSigner.keys(keys)
cli = Client.with_opts(signer, opts) cli = Client.with_opts(signer, opts)
cli.add_relay("wss://relay.nostr.band") cli.add_relay(options["relay"])
cli.connect() cli.connect()
search_since_seconds = int(options["since"]) * 24 * 60 * 60 #earch_since_seconds = int(options["since"]) * 24 * 60 * 60
dif = Timestamp.now().as_secs() - search_since_seconds #dif = Timestamp.now().as_secs() - search_since_seconds
search_since = Timestamp.from_secs(dif) #search_since = Timestamp.from_secs(dif)
search_since = Timestamp.from_secs(int(options["since"]))
search_until_seconds = int(options["until"]) * 24 * 60 * 60 #search_until_seconds = int(options["until"]) * 24 * 60 * 60
dif = Timestamp.now().as_secs() - search_until_seconds #dif = Timestamp.now().as_secs() - search_until_seconds
search_until = Timestamp.from_secs(dif) #search_until = Timestamp.from_secs(dif)
search_until = Timestamp.from_secs(int(options["until"]))
userkeys = [] userkeys = []
for user in options["users"]: for user in options["users"]:
user = user[1] user = user[1]
@@ -115,23 +121,14 @@ class AdvancedSearch(DVMTaskInterface):
userkeys.append(userkey) userkeys.append(userkey)
if not options["users"] and options["user"] == "": if not options["users"]:
notes_filter = Filter().kind(1).search(options["search"]).since(search_since).until(search_until).limit( notes_filter = Filter().kind(1).search(options["search"]).since(search_since).until(search_until).limit(options["max_results"])
options["max_results"])
elif options["search"] == "": elif options["search"] == "":
if options["users"]:
notes_filter = Filter().kind(1).authors(userkeys).since(search_since).until( notes_filter = Filter().kind(1).authors(userkeys).since(search_since).until(
search_until).limit(options["max_results"]) search_until).limit(options["max_results"])
else: else:
notes_filter = Filter().kind(1).authors([PublicKey.from_hex(options["user"])]).since(search_since).until(
search_until).limit(options["max_results"])
else:
if options["users"]:
notes_filter = Filter().kind(1).authors(userkeys).search(options["search"]).since( notes_filter = Filter().kind(1).authors(userkeys).search(options["search"]).since(
search_since).until(search_until).limit(options["max_results"]) search_since).until(search_until).limit(options["max_results"])
else:
notes_filter = Filter().kind(1).authors([PublicKey.from_hex(options["user"])]).search(options["search"]).since(
search_since).until(search_until).limit(options["max_results"])
events = cli.get_events_of([notes_filter], timedelta(seconds=5)) events = cli.get_events_of([notes_filter], timedelta(seconds=5))
@@ -163,29 +160,28 @@ class AdvancedSearch(DVMTaskInterface):
# playground or elsewhere # playground or elsewhere
def build_example(name, identifier, admin_config): def build_example(name, identifier, admin_config):
dvm_config = build_default_config(identifier) dvm_config = build_default_config(identifier)
admin_config.LUD16 = dvm_config.LN_ADDRESS
# Add NIP89 # Add NIP89
nip89info = { nip89info = {
"name": name, "name": name,
"image": "https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg", "image": "https://image.nostr.build/a99ab925084029d9468fef8330ff3d9be2cf67da473b024f2a6d48b5cd77197f.jpg",
"about": "I search notes", "about": "I search notes on Nostr.band.",
"encryptionSupported": True, "encryptionSupported": True,
"cashuAccepted": True, "cashuAccepted": True,
"nip90Params": { "nip90Params": {
"user": { "users": {
"required": False, "required": False,
"values": [], "values": [],
"description": "Do the task for another user" "description": "Search for content from specific users"
}, },
"since": { "since": {
"required": False, "required": False,
"values": [], "values": [],
"description": "The number of days in the past from now the search should include" "description": "A unix timestamp in the past from where the search should start"
}, },
"until": { "until": {
"required": False, "required": False,
"values": [], "values": [],
"description": "The number of days in the past from now the search should include up to" "description": "A unix timestamp that tells until the search should include results"
}, },
"max_results": { "max_results": {
"required": False, "required": False,
@@ -199,8 +195,10 @@ def build_example(name, identifier, admin_config):
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"]) nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"])
nip89config.CONTENT = json.dumps(nip89info) nip89config.CONTENT = json.dumps(nip89info)
options = {"relay": "wss://relay.nostr.band"}
return AdvancedSearch(name=name, dvm_config=dvm_config, nip89config=nip89config, return AdvancedSearch(name=name, dvm_config=dvm_config, nip89config=nip89config,
admin_config=admin_config) admin_config=admin_config, options=options)
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -50,8 +50,8 @@ class AdvancedSearchWine(DVMTaskInterface):
# default values # default values
user = "" user = ""
users = [] users = []
since_days = 800 # days ago since_seconds = Timestamp.now().as_secs() - (365 * 24 * 60 * 60)
until_days = 365 # days ago until_seconds = Timestamp.now().as_secs()
search = "" search = ""
max_results = 100 max_results = 100
@@ -68,9 +68,9 @@ class AdvancedSearchWine(DVMTaskInterface):
elif param == "users": # check for param type elif param == "users": # check for param type
users = json.loads(tag.as_vec()[2]) users = json.loads(tag.as_vec()[2])
elif param == "since": # check for param type elif param == "since": # check for param type
since_days = int(tag.as_vec()[2]) since_seconds = int(tag.as_vec()[2])
elif param == "until": # check for param type elif param == "until": # check for param type
until_days = int(tag.as_vec()[2]) until_seconds = int(tag.as_vec()[2])
elif param == "max_results": # check for param type elif param == "max_results": # check for param type
max_results = int(tag.as_vec()[2]) max_results = int(tag.as_vec()[2])
@@ -78,8 +78,8 @@ class AdvancedSearchWine(DVMTaskInterface):
"search": search, "search": search,
"user": user, "user": user,
"users": users, "users": users,
"since": since_days, "since": since_seconds,
"until": until_days, "until": until_seconds,
"max_results": max_results, "max_results": max_results,
} }
@@ -104,20 +104,18 @@ class AdvancedSearchWine(DVMTaskInterface):
print("Sending job to Server") print("Sending job to Server")
if options["users"]: if options["users"]:
url = ('https://api.nostr.wine/search?query=' + options["search"] + '&kind=1' + '&pubkey=' + options["users"][0] + "&limit=100" + "&sort=time") url = ('https://api.nostr.wine/search?query=' + options["search"] + '&kind=1' + '&pubkey=' + options["users"][0] + "&limit=100" + "&sort=time" + "&until=" + str(options["until"]) + "&since=" + str(options["since"]))
else: else:
url = ('https://api.nostr.wine/search?query=' + options["search"] + '&kind=1' + "&limit=100" + "&sort=time") url = ('https://api.nostr.wine/search?query=' + options["search"] + '&kind=1' + "&limit=100" + "&sort=time" + "&until=" + str(options["until"]) + "&since=" + str(options["since"]))
headers = {'Content-type': 'application/x-www-form-urlencoded'} headers = {'Content-type': 'application/x-www-form-urlencoded'}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
print(response.text) print(response.text)
ob = json.loads(response.text) ob = json.loads(response.text)
data = ob['data'] data = ob['data']
print(data)
result_list = [] result_list = []
for el in data: for el in data:
e_tag = Tag.parse(["e", el['id']]) e_tag = Tag.parse(["e", el['id']])
print(e_tag.as_vec())
result_list.append(e_tag.as_vec()) result_list.append(e_tag.as_vec())

View File

@@ -1,6 +1,6 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = '0.2.1' VERSION = '0.2.2'
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. ' LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. '
'This is an early stage release. Interfaces might change/brick') 'This is an early stage release. Interfaces might change/brick')