nostrdvm/tests/search.py

234 lines
8.4 KiB
Python
Raw Normal View History

2024-07-23 09:21:49 +02:00
import json
import os
from pathlib import Path
import dotenv
from nostr_sdk import Keys, LogLevel, init_logger
2025-01-02 09:14:21 +01:00
from nostr_dvm.framework import DVMFramework
2024-07-23 09:21:49 +02:00
from nostr_dvm.tasks.advanced_search import AdvancedSearch
from nostr_dvm.tasks.advanced_search_wine import AdvancedSearchWine
from nostr_dvm.tasks.search_users import SearchUser
from nostr_dvm.utils.admin_utils import AdminConfig
from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
2024-11-15 08:56:34 +01:00
from nostr_dvm.utils.outbox_utils import AVOID_OUTBOX_RELAY_LIST
2024-07-23 09:21:49 +02:00
from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys
rebroadcast_NIP89 = False # Announce NIP89 on startup Only do this if you know what you're doing.
rebroadcast_NIP65_Relay_List = False
update_profile = False
use_logger = True
2024-07-23 09:21:49 +02:00
log_level = LogLevel.ERROR
if use_logger:
init_logger(log_level)
2024-07-23 09:21:49 +02:00
2024-11-15 08:56:34 +01:00
RELAY_LIST = ["wss://nostr.mom",
#"wss://relay.primal.net",
"wss://nostr.oxtr.dev",
#"wss://relay.nostr.net"
2024-08-12 15:55:12 +02:00
]
2024-11-15 08:56:34 +01:00
SYNC_DB_RELAY_LIST = ["wss://relay.damus.io",
#"wss://relay.primal.net",
"wss://nostr.oxtr.dev"]
2024-07-23 09:21:49 +02:00
def build_advanced_search(name, identifier):
dvm_config = DVMConfig()
dvm_config.PRIVATE_KEY = check_and_set_private_key(identifier)
npub = Keys.parse(dvm_config.PRIVATE_KEY).public_key().to_bech32()
2024-08-19 13:25:03 +02:00
dvm_config = build_default_config(identifier)
2024-07-23 09:21:49 +02:00
# dvm_config.LNBITS_URL = os.getenv("LNBITS_HOST")
2024-11-15 08:56:34 +01:00
dvm_config.FIX_COST = 0
dvm_config.AVOID_OUTBOX_RELAY_LIST = AVOID_OUTBOX_RELAY_LIST
dvm_config.RELAY_LIST = RELAY_LIST
dvm_config.SYNC_DB_RELAY_LIST = SYNC_DB_RELAY_LIST
2024-08-19 13:25:03 +02:00
2024-07-23 09:21:49 +02:00
2024-07-23 09:21:49 +02:00
admin_config = AdminConfig()
admin_config.REBROADCAST_NIP89 = rebroadcast_NIP89
admin_config.REBROADCAST_NIP65_RELAY_LIST = rebroadcast_NIP65_Relay_List
admin_config.UPDATE_PROFILE = update_profile
2024-08-19 13:25:03 +02:00
admin_config.LUD16 = dvm_config.LN_ADDRESS
2024-07-23 09:21:49 +02:00
# Add NIP89
nip89info = {
"name": name,
"picture": "https://nostr.band/android-chrome-192x192.png",
2024-07-23 09:21:49 +02:00
"about": "I search notes on nostr.band",
"supportsEncryption": True,
"acceptsNutZaps": dvm_config.ENABLE_NUTZAP,
2024-07-23 09:21:49 +02:00
"nip90Params": {
"users": {
"required": False,
"values": [],
"description": "Search for specific authors"
},
"since": {
"required": False,
"values": [],
"description": "The number of days in the past from now the search should include"
},
"until": {
"required": False,
"values": [],
"description": "The number of days in the past from now the search should include up to"
},
"max_results": {
"required": False,
"values": [],
"description": "The number of maximum results to return (default currently 150)"
}
}
}
nip89config = NIP89Config()
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY,
nip89info["picture"])
2024-07-23 09:21:49 +02:00
nip89config.CONTENT = json.dumps(nip89info)
return AdvancedSearch(name=name, dvm_config=dvm_config, nip89config=nip89config,
admin_config=admin_config)
def build_advanced_search_wine(name, identifier):
dvm_config = DVMConfig()
dvm_config.PRIVATE_KEY = check_and_set_private_key(identifier)
npub = Keys.parse(dvm_config.PRIVATE_KEY).public_key().to_bech32()
2024-08-12 15:55:12 +02:00
dvm_config.RELAY_LIST = RELAY_LIST
2024-11-18 17:14:07 +01:00
invoice_key, admin_key, wallet_id, lnaddress = check_and_set_ln_bits_keys(identifier, npub)
2024-07-23 09:21:49 +02:00
dvm_config.LNBITS_INVOICE_KEY = invoice_key
dvm_config.LNBITS_ADMIN_KEY = admin_key # The dvm might pay failed jobs back
dvm_config.LNBITS_URL = os.getenv("LNBITS_HOST")
2024-11-15 08:56:34 +01:00
dvm_config.AVOID_OUTBOX_RELAY_LIST = AVOID_OUTBOX_RELAY_LIST
dvm_config.RELAY_LIST = RELAY_LIST
dvm_config.SYNC_DB_RELAY_LIST = SYNC_DB_RELAY_LIST
2024-07-23 09:21:49 +02:00
admin_config = AdminConfig()
admin_config.REBROADCAST_NIP89 = rebroadcast_NIP89
admin_config.REBROADCAST_NIP65_RELAY_LIST = rebroadcast_NIP65_Relay_List
admin_config.UPDATE_PROFILE = update_profile
admin_config.LUD16 = lnaddress
# Add NIP89
nip89info = {
"name": name,
"picture": "https://image.nostr.build/d844d6a963724b9f9deb6b3326984fd95352343336718812424d5e99d93a6f2d.jpg",
2024-07-23 09:21:49 +02:00
"about": "I search notes on nostr.wine using the nostr-wine API",
"supportsEncryption": True,
"acceptsNutZaps": dvm_config.ENABLE_NUTZAP,
2024-07-23 09:21:49 +02:00
"nip90Params": {
"users": {
"required": False,
"values": [],
"description": "Search for content from specific users"
},
"since": {
"required": False,
"values": [],
"description": "The number of days in the past from now the search should include"
},
"until": {
"required": False,
"values": [],
"description": "The number of days in the past from now the search should include up to"
},
"max_results": {
"required": False,
"values": [],
"description": "The number of maximum results to return (default currently 20)"
}
}
}
nip89config = NIP89Config()
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY,
nip89info["picture"])
2024-07-23 09:21:49 +02:00
nip89config.CONTENT = json.dumps(nip89info)
return AdvancedSearchWine(name=name, dvm_config=dvm_config, nip89config=nip89config,
admin_config=admin_config)
def build_user_search(name, identifier):
dvm_config = build_default_config(identifier)
2024-11-20 10:40:55 +01:00
dvm_config.SYNC_DB_RELAY_LIST = SYNC_DB_RELAY_LIST
2024-11-15 08:56:34 +01:00
dvm_config.AVOID_OUTBOX_RELAY_LIST = AVOID_OUTBOX_RELAY_LIST
dvm_config.RELAY_LIST = RELAY_LIST
2024-11-20 10:40:55 +01:00
dvm_config.WOT_FILTERING = True
2024-07-23 09:21:49 +02:00
npub = Keys.parse(dvm_config.PRIVATE_KEY).public_key().to_bech32()
2024-11-04 08:30:38 +01:00
invoice_key, admin_key, wallet_id, lnaddress = check_and_set_ln_bits_keys(identifier, npub)
2024-07-23 09:21:49 +02:00
admin_config = AdminConfig()
admin_config.REBROADCAST_NIP89 = rebroadcast_NIP89
admin_config.REBROADCAST_NIP65_RELAY_LIST = rebroadcast_NIP65_Relay_List
admin_config.UPDATE_PROFILE = update_profile
admin_config.LUD16 = lnaddress
# Add NIP89
nip89info = {
"name": name,
"picture": "https://image.nostr.build/bd0181a3089181f1d92a5da1ef85cffbe37ba80fbcc695b9d85648dc2fa92583.jpg",
2024-07-23 09:21:49 +02:00
"about": "I search users based on their profile info.",
"supportsEncryption": True,
"acceptsNutZaps": dvm_config.ENABLE_NUTZAP,
2024-07-23 09:21:49 +02:00
"nip90Params": {
"max_results": {
"required": False,
"values": [],
"description": "The number of maximum results to return (default currently 150)"
}
}
}
nip89config = NIP89Config()
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["picture"])
2024-07-23 09:21:49 +02:00
nip89config.CONTENT = json.dumps(nip89info)
return SearchUser(name=name, dvm_config=dvm_config, nip89config=nip89config,
2024-11-20 10:40:55 +01:00
admin_config=admin_config)
2024-07-23 09:21:49 +02:00
def playground():
2025-01-02 09:14:21 +01:00
framework = DVMFramework()
2024-07-23 09:21:49 +02:00
advanced_search = build_advanced_search("Nostr.band Search",
"discovery_content_search")
2025-01-02 09:14:21 +01:00
framework.add(advanced_search)
2024-07-23 09:21:49 +02:00
advanced_search_wine = build_advanced_search_wine("Nostr.wine Search", "discovery_content_searchwine")
2025-01-02 09:14:21 +01:00
framework.add(advanced_search_wine)
framework.run()
2024-07-23 09:21:49 +02:00
2024-11-27 19:26:15 +01:00
#profile_search = build_user_search("Profile Searcher", "profile_search")
2025-01-02 09:14:21 +01:00
#framework.add(profile_search)
framework.run()
2024-07-23 09:21:49 +02:00
if __name__ == '__main__':
env_path = Path('.env')
if not env_path.is_file():
with open('.env', 'w') as f:
print("Writing new .env file")
f.write('')
if env_path.is_file():
print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True)
else:
raise FileNotFoundError(f'.env file not found at {env_path} ')
playground()