diff --git a/.gitignore b/.gitignore index 2e6c75b..250ad7b 100644 --- a/.gitignore +++ b/.gitignore @@ -181,3 +181,5 @@ ui/noogle/node_modules ui/noogle/.idea/ ui/noogle/package-lock.json search.py +identifier.sqlite +.idea/dataSources.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index d8ecaba..2c2e403 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -68,5 +68,24 @@ + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/db/nostr_profiles.db + $ProjectFileDir$ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.43.0/org/xerial/sqlite-jdbc/3.43.0.0/sqlite-jdbc-3.43.0.0.jar + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:identifier.sqlite + $ProjectFileDir$ + \ No newline at end of file diff --git a/nostr_dvm/dvm.py b/nostr_dvm/dvm.py index fec61cc..94c972e 100644 --- a/nostr_dvm/dvm.py +++ b/nostr_dvm/dvm.py @@ -23,6 +23,7 @@ from nostr_dvm.utils.cashu_utils import redeem_cashu + class DVM: dvm_config: DVMConfig admin_config: AdminConfig @@ -43,8 +44,6 @@ class DVM: signer = NostrSigner.keys(self.keys) self.client = Client.with_opts(signer,opts) - if dvm_config.SHOWLOG: - init_logger(LogLevel.DEBUG) self.job_list = [] self.jobs_on_hold_list = [] diff --git a/nostr_dvm/tasks/search_users.py b/nostr_dvm/tasks/search_users.py new file mode 100644 index 0000000..3f97c49 --- /dev/null +++ b/nostr_dvm/tasks/search_users.py @@ -0,0 +1,208 @@ +import json +import os +from datetime import timedelta +from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKey, NostrSigner, NostrDatabase, \ + ClientBuilder, Filter, NegentropyOptions, NegentropyDirection, init_logger, LogLevel + +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.nip89_utils import NIP89Config, check_and_set_d_tag +from nostr_dvm.utils.output_utils import post_process_list_to_events, post_process_list_to_users + +""" +This File contains a Module to search for notes +Accepted Inputs: a search query +Outputs: A list of events +Params: None +""" + + +class SearchUser(DVMTaskInterface): + KIND: int = EventDefinitions.KIND_NIP90_USER_SEARCH + TASK: str = "search-user" + FIX_COST: float = 0 + dvm_config: DVMConfig + dependencies = [("nostr-dvm", "nostr-dvm")] + + def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, + admin_config: AdminConfig = None, options=None): + dvm_config.SCRIPT = os.path.abspath(__file__) + + use_logger = False + if use_logger: + init_logger(LogLevel.DEBUG) + + super().__init__(name, dvm_config, nip89config, admin_config, options) + + 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("db/nostr_profiles.db") + cli = ClientBuilder().signer(signer).database(database).opts(opts).build() + + cli.add_relay("wss://relay.damus.io") + #cli.add_relay("wss://atl.purplerelay.com") + cli.connect() + + filter1 = Filter().kind(0) + + # filter = Filter().author(keys.public_key()) + print("Syncing Profile Database.. this might take a while..") + dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) + cli.reconcile(filter1, dbopts) + print("Done Syncing Profile Database.") + + def is_input_supported(self, tags, client=None, dvm_config=None): + for tag in tags: + if tag.as_vec()[0] == 'i': + input_value = tag.as_vec()[1] + input_type = tag.as_vec()[2] + if input_type != "text": + return False + return True + + def create_request_from_nostr_event(self, event, client=None, dvm_config=None): + self.dvm_config = dvm_config + print(self.dvm_config.PRIVATE_KEY) + + request_form = {"jobID": event.id().to_hex()} + + # default values + search = "" + max_results = 100 + relay = "wss://relay.nostr.band" + if self.options["relay"]: + relay = self.options["relay"] + + for tag in event.tags(): + if tag.as_vec()[0] == 'i': + input_type = tag.as_vec()[2] + if input_type == "text": + search = tag.as_vec()[1] + elif tag.as_vec()[0] == 'param': + param = tag.as_vec()[1] + if param == "max_results": # check for param type + max_results = int(tag.as_vec()[2]) + + options = { + "search": search, + "max_results": max_results, + "relay": relay + } + request_form['options'] = json.dumps(options) + return request_form + + def process(self, request_form): + from nostr_sdk import Filter + 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("db/nostr_profiles.db") + cli = ClientBuilder().database(database).signer(signer).opts(opts).build() + + cli.add_relay("wss://relay.damus.io") + #cli.add_relay("wss://atl.purplerelay.com") + cli.connect() + + # Negentropy reconciliation + + + + # Query events from database + + filter1 = Filter().kind(0) + events = cli.database().query([filter1]) + #for event in events: + # print(event.as_json()) + + #events = cli.get_events_of([notes_filter], timedelta(seconds=5)) + + result_list = [] + print("Events: " + str(len(events))) + index = 0 + if len(events) > 0: + + for event in events: + if index < options["max_results"]: + try: + if options["search"].lower() in event.content().lower(): + p_tag = Tag.parse(["p", event.author().to_hex()]) + print(event.as_json()) + result_list.append(p_tag.as_vec()) + index += 1 + except Exception as exp: + print(str(exp) + " " + event.author().to_hex()) + else: + break + + return json.dumps(result_list) + + 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 + dvm_config.SHOWLOG = True + # Add NIP89 + nip89info = { + "name": name, + "image": "https://image.nostr.build/a99ab925084029d9468fef8330ff3d9be2cf67da473b024f2a6d48b5cd77197f.jpg", + "about": "I search users.", + "encryptionSupported": True, + "cashuAccepted": True, + "nip90Params": { + "users": { + "required": False, + "values": [], + "description": "Search for content from specific users" + }, + "since": { + "required": False, + "values": [], + "description": "A unix timestamp in the past from where the search should start" + }, + "until": { + "required": False, + "values": [], + "description": "A unix timestamp that tells until the search should include results" + }, + "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["image"]) + nip89config.CONTENT = json.dumps(nip89info) + + options = {"relay": "wss://relay.damus.io"} + + return SearchUser(name=name, dvm_config=dvm_config, nip89config=nip89config, + admin_config=admin_config, options=options) + + +if __name__ == '__main__': + process_venv(SearchUser) diff --git a/nostr_dvm/utils/dvmconfig.py b/nostr_dvm/utils/dvmconfig.py index 4cab4db..a80765b 100644 --- a/nostr_dvm/utils/dvmconfig.py +++ b/nostr_dvm/utils/dvmconfig.py @@ -14,7 +14,7 @@ class DVMConfig: PUBLIC_KEY: str = "" FIX_COST: float = None PER_UNIT_COST: float = None - SHOWLOG: bool = False # Shows Nostr logs from Rust-Library, default off, turn on in config if needed. + RELAY_LIST = ["wss://relay.damus.io", "wss://nos.lol", "wss://nostr.wine", "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg", diff --git a/nostr_dvm/utils/scrapper/media_scrapper.py b/nostr_dvm/utils/scrapper/media_scrapper.py index 16ba2e1..9b7cb03 100644 --- a/nostr_dvm/utils/scrapper/media_scrapper.py +++ b/nostr_dvm/utils/scrapper/media_scrapper.py @@ -165,7 +165,7 @@ def XitterDownload(source_url, target_location): return details def get_tweet_status_id(tweet_url): - sid_patern = r"https://x\.com/[^/]+/status/(\d+)" + sid_patern = r'https://(?:x\.com|twitter\.com)/[^/]+/status/(\d+)' if tweet_url[len(tweet_url) - 1] != "/": tweet_url = tweet_url + "/" diff --git a/tests/test_dvm_client.py b/tests/test_dvm_client.py index e4e1189..c1e8e4e 100644 --- a/tests/test_dvm_client.py +++ b/tests/test_dvm_client.py @@ -40,7 +40,31 @@ def nostr_client_test_translation(input, kind, lang, sats, satsmax): config = DVMConfig send_event(event, client=client, dvm_config=config) return event.as_json() +def nostr_client_test_search_profile(input): + keys = Keys.parse(check_and_set_private_key("test_client")) + iTag = Tag.parse(["i", input, "text"]) + + + + relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", + "wss://nostr-pub.wellorder.net"]) + alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to translate a given Input"]) + event = EventBuilder(EventDefinitions.KIND_NIP90_USER_SEARCH, str("Search for user"), + [iTag, relaysTag, alttag]).to_event(keys) + + relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", + "wss://nostr-pub.wellorder.net"] + + signer = NostrSigner.keys(keys) + client = Client(signer) + + for relay in relay_list: + client.add_relay(relay) + client.connect() + config = DVMConfig + send_event(event, client=client, dvm_config=config) + return event.as_json() def nostr_client_test_image(prompt): keys = Keys.parse(check_and_set_private_key("test_client")) @@ -158,8 +182,8 @@ def nostr_client(): # nostr_client_test_translation("This is the result of the DVM in spanish", "text", "es", 20, 20) # nostr_client_test_translation("note1p8cx2dz5ss5gnk7c59zjydcncx6a754c0hsyakjvnw8xwlm5hymsnc23rs", "event", "es", 20,20) # nostr_client_test_translation("44a0a8b395ade39d46b9d20038b3f0c8a11168e67c442e3ece95e4a1703e2beb", "event", "zh", 20, 20) - nostr_client_test_image("a beautiful purple ostrich watching the sunset") - + #nostr_client_test_image("a beautiful purple ostrich watching the sunset") + nostr_client_test_search_profile("dontbelievew") # nostr_client_test_tts("Hello, this is a test. Mic check one, two.") # cashutoken = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MSwiQyI6IjAyNWU3ODZhOGFkMmExYTg0N2YxMzNiNGRhM2VhMGIyYWRhZGFkOTRiYzA4M2E2NWJjYjFlOTgwYTE1NGIyMDA2NCIsInNlY3JldCI6InQ1WnphMTZKMGY4UElQZ2FKTEg4V3pPck5rUjhESWhGa291LzVzZFd4S0U9In0seyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6NCwiQyI6IjAyOTQxNmZmMTY2MzU5ZWY5ZDc3MDc2MGNjZmY0YzliNTMzMzVmZTA2ZGI5YjBiZDg2Njg5Y2ZiZTIzMjVhYWUwYiIsInNlY3JldCI6IlRPNHB5WE43WlZqaFRQbnBkQ1BldWhncm44UHdUdE5WRUNYWk9MTzZtQXM9In0seyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MTYsIkMiOiIwMmRiZTA3ZjgwYmMzNzE0N2YyMDJkNTZiMGI3ZTIzZTdiNWNkYTBhNmI3Yjg3NDExZWYyOGRiZDg2NjAzNzBlMWIiLCJzZWNyZXQiOiJHYUNIdHhzeG9HM3J2WWNCc0N3V0YxbU1NVXczK0dDN1RKRnVwOHg1cURzPSJ9XSwibWludCI6Imh0dHBzOi8vbG5iaXRzLmJpdGNvaW5maXhlc3RoaXMub3JnL2Nhc2h1L2FwaS92MS9ScDlXZGdKZjlxck51a3M1eVQ2SG5rIn1dfQ=="