def online_help(): reply = "" reply = "My name is " + globals.bot_name + " and I am an instance of " + sys.argv[ 0] + ".\n" reply = reply + "I am an interface to the Searx meta-search engine with a limited conversational user interface.\n\n" if user_text: reply = reply + user_text + "\n\n" reply = reply + "At this time I can accept search requests and optionally e-mail the results to a destination address. To execute a search request, send me a message that looks like this:\n\n" reply = reply + globals.bot_name + ", (send/e-mail/email/mail) (me/<e-mail address>) top <number> hits for <search request...>\n\n" reply = reply + "By default, I will e-mail results to the address " + default_email + ".\n\n" reply = reply + "I can return search results directly to this instant messager session. Send me a request that looks like this:\n\n" reply = reply + globals.bot_name + ", (get) top <number> hits for <search request...>\n\n" reply = reply + "I can list the search engines I know about:\n\n" reply = reply + globals.bot_name + ", (list (search)) engines\n\n" reply = reply + "I can run searches using specific search engines:\n\n" reply = reply + globals.bot_name + ", search <search engine shortcode> for <search request...>\n\n" globals.send_message_to_user(globals.server, reply) # Now let's handle the "search categories" case. reply = "I can also run searches based upon different categories. The\n" reply = reply + "categories I support are:\n\n" reply = reply + ", ".join( category for category in globals.search_categories) + "\n\n" reply = reply + "To list the search categories:\n\n" reply = reply + globals.bot_name + ", list (search) categories\n\n" reply = reply + "To search in a particular category:\n\n" reply = reply + globals.bot_name + ", search <category> for top <n> hits for <search request...>\n\n" reply = reply + "" globals.send_message_to_user(globals.server, reply) return
def parse_search_request(search_request): logging.debug("Entered function parse_search_request().") number_of_search_results = 0 search_term = "" email_address = "" engine = "" # Clean up the search request. search_request = search_request.strip() search_request = search_request.strip(",") search_request = search_request.strip(".") search_request = search_request.strip("'") search_request = search_request.lower() # If the search request is empty (i.e., nothing in the queue) return 0 and # "". if "no commands" in search_request: logging.debug("Got empty search request.") return (number_of_search_results, search_term, email_address) # Attempt to parse a request for online help. (number_of_search_results, search_term, email_address) = parse_help(search_request) if number_of_search_results == "help": logging.info("The user is asking for online help.") return ("help", None, None) # Attempt to parse a "list search engines" request. (number_of_search_results, search_term, email_address) = parse_list(search_request) if number_of_search_results == "list": logging.info("The user has requested a list of search engines.") return ("list", None, None) # Attempt to parse a "get" request. (number_of_search_results, search_term, email_address) = parse_get_request(search_request) if number_of_search_results and (email_address == "XMPP"): logging.info("The user has sent the search term " + str(search_term)) return (number_of_search_results, search_term, "XMPP") # Attempt to parse an "email results" request. (number_of_search_results, search_term, email_address) = parse_and_email_results(search_request) if number_of_search_results: if ("@" in email_address) or (email_address == "default_email"): logging.info("The user has requested that search results for " + str(search_term) + " be e-mailed to " + email_address + ".") return (number_of_search_results, search_term, email_address) # Attempt to parse a search for a specific engine. If it works, prepend # the string "!<search shortcode>" so that Searx knows to search on one # search engine only. (number_of_search_results, search_term, email_address) = parse_specific_search(search_request) if number_of_search_results and (email_address == "XMPP"): logging.info("The user has requested a specific search: " + str(search_term)) return (number_of_search_results, search_term, "XMPP") # Fall-through - this should happen only if nothing at all matches. logging.info("Fell all the way through in parse_search_request(). Telling the user I didn't understand what they said.") globals.send_message_to_user(globals.server, "I didn't understand your command. Try again, please.") return (0, "", None)
def online_help(): reply = "" reply = "My name is " + globals.bot_name + " and I am an instance of " + sys.argv[0] + ".\n" reply = reply + "I am an interface to the Searx meta-search engine with a limited conversational user interface.\n\n" if user_text: reply = reply + user_text + "\n\n" reply = reply + "At this time I can accept search requests and optionally e-mail the results to a destination address. To execute a search request, send me a message that looks like this:\n\n" reply = reply + globals.bot_name + ", (send/e-mail/email/mail) (me/<e-mail address>) top <number> hits for <search request...>\n\n" reply = reply + "By default, I will e-mail results to the address " + default_email + ".\n\n" reply = reply + "I can return search results directly to this instant messager session. Send me a request that looks like this:\n\n" reply = reply + globals.bot_name + ", (get) top <number> hits for <search request...>\n\n" reply = reply + "I can list the search engines I know about:\n\n" reply = reply + globals.bot_name + ", (list (search)) engines\n\n" reply = reply + "I can run searches using specific search engines:\n\n" reply = reply + globals.bot_name + ", search !<search engine shortcode> for <search request...>\n" reply = reply + "Yes, an exclamation point goes in front of the shortcode.\n\n" globals.send_message_to_user(globals.server, reply) return
def get_search_results(search_term): logger.debug("Entered function get_search_results().") # URL which represents the search request. url = "" # This will hold the search results returned by Searx. search_results = {} # The array which holds the search result data we actually want. results = [] # Assemble the search request URL. url = searx + search_term logger.debug("Got a search request for " + str(url) + ".") # Make the search request and extract the JSON of the results. try: request = requests.get(url) search_results = json.loads(request.content) except: globals.send_message_to_user( globals.server, "Uh-oh. Searx returned HTTP status code %s." % (request.status_code)) return None # Extract only the stuff we want from the search results and pack it into # a separate hash table. for i in search_results['results']: temp = {} temp['title'] = i['title'] temp['url'] = i['url'] temp['score'] = i['score'] results.append(temp) # Truncate the master list of search results down to the number of # hits the user requested. if len(results) > number_of_results: results = results[:(number_of_results - 1)] logger.debug("Returning " + str(len(results)) + " search results.") # Return the list of search results. return results
str(polling_time)) logger.debug("SMTP server to send search results through: " + smtp_server) logger.debug("E-mail address that search results are sent from: " + origin_email_address) logger.debug("Number of search engines enabled: " + str(len(globals.search_engines))) if user_text: logger.debug("User-defined help text: " + user_text) if user_acknowledged: logger.debug("User-defined command acknowledgement text: " + user_acknowledged) # Try to contact the XMPP bridge. Keep trying until you reach it or the # system shuts down. logger.info("Trying to contact XMPP message bridge...") while True: try: globals.send_message_to_user(globals.server, globals.bot_name + " now online.") break except: logger.warning("Unable to reach message bus. Going to try again in %s seconds." % polling_time) time.sleep(float(polling_time)) # Query the Searx instance and get its list of enabled search engines. while True: try: temp_searx = "/".join(searx.split('/')[0:-1]) + "/config" request = requests.get(temp_searx) globals.search_engines = request.json()["engines"] break except: globals.send_message_to_user(globals.server, "Unable to contact Searx instance! Tried to contact configuration URL %s." % str(temp_searx)) time.sleep(float(polling_time))