def lightIntel(): # a misnomer now try: page1 = requests.get('https://washingtonpost.com') tree1 = html.fromstring(page1.content) page2 = requests.get('https://techcrunch.com') tree2 = html.fromstring(page2.content) excerpt = tree2.xpath('//a[@class="post-block__title__link"]/text()') H() print("\n\nTechCrunch:\n") articles = tree2.xpath('//a[@class="post-block__title__link"]/text()') for i in articles: print(i.strip()) time.sleep(0.03) headlinez = tree1.xpath( '//a[@data-pb-placeholder="Write headline here"]/text()') stutter("\nWashington Post:\n") time.sleep(1) for i in headlinez: print(i) time.sleep(0.03) except KeyboardInterrupt as e: H() sprint('Aborted.') except Exception as e: rollbar.report_exc_info() logging.debug(str(e)) H() sprint(random.choice(error_responses)) finally: return
def code_search(): """Searches files in specified folder for occurances substring. """ H() sprint('Enter phrase or code snippet') term = display_manager.input_prompt() H() sprint( "Please enter the path to the folder you'd like to begin the search from." ) root_dir = display_manager.input_prompt() try: H() sprint("Looking it up..") os.system("gnome-terminal -e \"grep -rnw '%s' -e '%s'\"" % (root_dir, term)) except KeyboardInterrupt as e: return except Exception as e: rollbar.report_exc_info() print(e) pass return
def directions(destination=None, origin=None): """Opens default browser and maps out possible routes between two geographic points. params: destination -> string; route destination origin -> string; route origin """ try: if not origin: origin = 'my+location' H() sprint('Charting a course to %s.' % destination.title()) else: H() sprint('Charting a course to %s from %s.' % (destination.title(), origin.title())) os.system( "gnome-terminal -e 'python -m webbrowser -t 'https://www.google.com/maps/dir/%s/%s''" % (origin, destination)) return except Exception as e: rollbar.report_exc_info() logging.error(e) H() sprint(random.choice(error_responses)) return
def find_related(key_word): """Finds articles in main database (intel, in this case) that contain keyword. """ H() sprint('This might take a while so please be patient.') try: keyword = key_word found = [] count = 0 cycle = 0 for file in intel.find(): if ' ' + keyword.lower() + ' ' in file['payload'][0].lower(): found.append([ file['payload'][0].lower().count(keyword.lower()), file['title'] ]) count += file['payload'][0].lower().count(keyword.lower()) cycle += 1 if cycle == 1 or cycle == 100000: logging.debug( '<<<%s>>>...>' % file['payload'][0].lower().count(keyword.lower())) cycle = 0 except KeyboardInterrupt: logging.warning('User aborted search for keyword "%s".' % key_word) H() sprint("I guess patience isn't your strong suit.") return if found: logging.debug('keyword "%s" appears %s times in current universe.' % (key_word, str(count))) try: found.sort() found.reverse() for title in found: print('%s (%s)' % (title[1].strip(), str(title[0]))) time.sleep(0.02) return except KeyboardInterrupt: return else: H() sprint( "No mentions of %s exist locally, shall I do an internet sweep for requested data?" % key_word) response = display_manager.input_prompt() if 'yes' in response: logging.debug('relation seeker sent "%s" to informant.' % key_word) H() sprint('Doing that.') informant(key_word, False, 0, False, *['relation seeker']) else: controlCentre(*[response]) return
def handle_persistence_response(result, void=False): logging.debug(result) if not void: if (result.startswith('Success')): H() sprint('Done.') else: H() sprint('Negative contact. Check the logs.') return
def start_over(): H() sprint('Would you like to start over?') directive = display_manager.input_prompt() if 'yes' in directive: laFibonacci() else: H() sprint("Cool. It was nice reminding you who's the boss around here.") return
def imageFinder(entity, num_instances): """Deprecated. Downloads images without displaying them. Stores them in ./LUNA/downloads. """ try: os.system('googleimagesdownload --keywords "%s" --limit %s' % (entity, num_instances)) H() sprint("You know where to find them.") except KeyboardInterrupt: H() sprint("You know where to find them.")
def human_lang_translator(text): """Translates text to english. """ try: H() sprint(trans_to_eng(text)) except Exception as e: rollbar.report_exc_info() logging.error(str(e)) H() sprint('Some of my agents seem to be inhibited. Check the logs.') init_translator(*['session'])
def init_translator(*session): """Initialises and terminates translator mode. """ if not session: H() sprint('translator activated.') raw_text = display_manager.input_prompt() if raw_text.lower() != 'exit': human_lang_translator(raw_text) else: H() sprint('translator terminated.')
def population_density(): """Measures the population density of a specified area, given the population. """ H() sprint("Please enter the number of people who live in this area.") population = display_manager.input_prompt().replace(',', '') H() sprint("Enter the size of the area in square kilometers.") area = display_manager.input_prompt().replace(',', '') H() sprint( "The population density of this area is %s people per square kilometer." % str(int(population) / int(area)))
def laFibonacci(): H() sprint( 'The first two numbers are 1 and 1. Continue the sequence. The last one standing wins (I always win).' ) straws = [1, 2] firstGo = random.choice(straws) if firstGo == 1: H() sprint("I'll go first.") lunasTurn(1, 1, 0) else: usersTurn()
def wolfram(init=True): if init: H() sprint('Game face activated') command = display_manager.input_prompt() if command.lower() != 'exit': search_wolfram(command) wolfram(False) else: H() sprint('Normal operations resumed') return
def lunasTurn(n1, n2, depth): nextInt = n1 + n2 n1 = n2 n2 = nextInt H() time.sleep(2) print(nextInt) usersTurn(n1, n2, depth)
def find_root(t): """Find the square root of a specified number. """ index = float(t[t.find('the') + 4:t.find('root') - 1]) radicand = float(t[t.find('of') + 3:]) root = radicand**(1 / index) H() sprint('The %s root of %s is %s' % (index, radicand, root)) return
def save_in_db(code_name, obj): entry = { '_id': str(uuid.uuid4()), 'code_name': code_name, 'date': str(datetime.datetime.now()), 'payload': [obj] } files.insert_one(entry) H() sprint("New file stored successfully.") return
def dictionaryHelper(dictionary): # todo: auto suggest from wordlist # print(dict_header, end='') # word = prompt('', history=FileHistory('wordlist.txt'), auto_suggest=AutoSuggestFromHistory()) word = display_manager.input_prompt() if word != 'exit': try: H(dict_header) os.system('dict -d %s %s' % (dictionary, word)) dictionaryHelper(dictionary) except Exception as e: rollbar.report_exc_info() H() sprint(e) return else: H() sprint('Dictionary closed.') return
def health_check(): try: battery = psutil.sensors_battery() if battery.percent <= 20 and battery.power_plugged == False: H() sprint(random.choice(low_battery_responses)) logging.info('Battery level is at %s percent. Charging: %s' % (str(battery.percent), str(battery.power_plugged))) except Exception as e: rollbar.report_exc_info() logging.error(e) pass
def media_player(artist): result = [ y for x in os.walk('%s/Downloads' % os.path.expanduser('~')) for y in glob(os.path.join(x[0], '*.mp3')) ] for file in result: if artist.lower() in file.lower(): os.system('xdg-open "%s"' % file) return H() sprint('Could not find any %s' % artist) return
def gridWeight(entity): """Unimplemented. Ignore for now. """ try: loc = layout.geocode(entity) H() sprint("%s has an importance weight of %s" % (entity.title(), loc.raw['importance'])) return except Exception as e: rollbar.report_exc_info() logging.error(e) gridWeight(entity)
def find_external_resource(): """Searches for links related to a specified subject. e.g., It can be used to search for pdf links that can then be curled through Luna's ghost terminal.""" H() sprint("What do you need?") res = display_manager.input_prompt() try: # for anonymity edit google library to run through proxychains urls = search(res, stop=20) H() sprint("Take your pick.") for url in urls: print(url) time.sleep(0.03) return except Exception as e: logging.debug(str(e)) H() sprint(random.choice(bad_connection_responses)) return
def dictionary(): try: H(dict_header) print("Choose a dictionary database (enter the associated number)\n\n", "1. The Collaborative International Dictionary of English\n", "2. Wordnet (2006)\n", "3. The Devil's Dictionary (1881-1906)\n", "4. The Moby Thesaurus II by Grady Ward") dic = display_manager.input_prompt() mapper = {'1': 'gcide', '2': 'wn', '3': 'devil', '4': 'moby-thesaurus'} mapper2 = { '1': 'The Collaborative International Dictionary of English', '2': 'Wordnet (2006)', '3': "The Devil's Dictionary (1881-1906)", '4': 'The Moby Thesaurus II by Grady Ward' } H(dict_header) sprint("You are now in %s" % mapper2[dic]) dictionaryHelper(mapper[dic]) except KeyboardInterrupt: H() sprint('Aborted') return
def ethan(): inp = display_manager.input_prompt() if inp.startswith('open'): target = inp[5:] try: x = hades.find_one({'title': target}) sprint(x['payload']) except Exception as e: rollbar.report_exc_info() H() sprint("No record found.") else: controlCentre(*[inp])
def find_location(location_name): """Plots the location of a specified place within google maps in default browser. params: location_name -> string; name of place whose location is sought. """ try: os.system( "gnome-terminal -e 'python -m webbrowser -t 'https://www.google.com/maps/place/%s''" % location_name) return except Exception as e: rollbar.report_exc_info() H() sprint(random.choice(error_responses)) return
def search_wolfram(command): res = client.query(command) if res['@success'] == 'false': H() sprint(kernel.respond(command)) result = persistence.insert_session_data(command) logging.info(result) else: result = '' pod0 = res['pod'][0] pod1 = res['pod'][1] if (('definition' in pod1['@title'].lower()) or ('result' in pod1['@title'].lower()) or (pod1.get('@primary', 'false') == 'true')): result = resolveListOrDict(pod1['subpod']) H() sprint(result.replace('\n', ' ')) return else: H() sprint(kernel.respond(command)) result = persistence.insert_session_data(command) logging.info(result) return
def usersTurn(n1=1, n2=1, depth=0): try: start = time.time() nextInt = n1 + n2 try: userNext = display_manager.input_prompt() test = int(userNext) except Exception as e: logging.error(e) return end = time.time() gameOver = False if int(end - start) > 10: H() sprint('You took too long. Your max depth is %s.' % depth) start_over() if int(userNext) != nextInt: H() sprint('Cute. Max depth is %s.' % depth) gameOver = True if gameOver: start_over() # todo: save high score to file. Sustain if depth is lower. else: n1 = n2 n2 = nextInt depth += 1 lunasTurn(n1, n2, depth) except KeyboardInterrupt as e: H() sprint("Sequence terminated.") return
def nearby(req): """Finds places related to specified subject near users location. """ try: mid = req.find('near') obj = req[:9 - 1] pos = req[9 + 5:] coords = find_lc(pos) H() sprint('Searching for %s near %s' % (obj, pos)) webbrowser.open('https://google.com/maps/search/%s/@%s,%s' % (obj, coords[0], coords[1])) return except Exception as e: rollbar.report_exc_info() logging.debug(str(e)) print("Couldn't comnply. Check the logs.") return
def porter(): """Sends an email to a specified email address. """ uheader = '[' + user[0].upper() + ']' H() sprint("Enter payload\n") lines = [] try: while True: lines.append(input(u"\u262F ")) except EOFError: pass except KeyboardInterrupt: H() sprint("Aborted.") payload = "\n".join(lines) print('\n') H() sprint("Label the package") subject = display_manager.input_prompt() H() sprint("Who's the mark?") target = display_manager.input_prompt() try: fa = os.getenv('LUNADDR') if not target: # Env vars ta = os.getenv('MYEMAIL') else: ta = target msg = MIMEMultipart() msg['From'] = fa msg['To'] = ta msg['Subject'] = subject body = payload msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fa, os.getenv('EMAILPWD')) text = msg.as_string() server.sendmail(fa, ta, text) server.quit() H() sprint("Package sent.") delete_latest() return except: rollbar.report_exc_info() H() sprint('Package sending failed.') return
def groundDistance(point_x, point_y): """Measures distance between one geographic location to another. Example input: 'what is the distance between New York and California'. """ try: x_raw = layout.geocode(point_x) y_raw = layout.geocode(point_y) x = (x_raw.latitude, x_raw.longitude) y = (y_raw.latitude, y_raw.longitude) xydistance = vincenty( x, y ).kilometers # other options: .nautical, .miles, .ft, ...etc. see docs H() sprint("The distance between %s and %s is %s kilometers" % (point_x.title(), point_y.title(), xydistance)) return except Exception as e: rollbar.report_exc_info() logging.error(e) groundDistance(point_x, point_y)
def command_handler(user_input): kernel.setPredicate("name", os.environ['LUNA_USER']) command = user_input.lower() try: if command == 'lets be serious': wolfram() if command == 'help': utils.help_center() elif (command.startswith('what is the distance between ') or command.startswith('distance between')): end = command.find(' and ') start = command.find(' between ') x = command[start + 9:end] y = command[end + 5:] utils.groundDistance(x, y) return elif 'resource' in command: utils.find_external_resource() return elif command == 'newscatcher' or command == 'nc': nc_handler.main() return elif command == 'search quotes': # display_manager.quote_search() return elif command.lower().strip().startswith('search '): listing_handler.list_search_results(command[7:]) elif command.lower() == 'insert quote': result = data_manager.insert_quote_from_user() handle_persistence_response(result, True) return elif command == 'reading list': listing_handler.list_table('random') return elif command == 'latency': render_latency_chart = StoppableThread(target=utils.latency) render_latency_chart.daemon = True render_latency_chart.start() return elif command == 'clear': os.system('clear') return elif command.startswith('extract '): extraction_list = command[8:] targets = extraction_list.split(',') formatted_parameters = '' for target in targets: formatted_parameters += f"'{target.strip()}' " os.system( f'gnome-terminal -e "python functions/extractor/extractor.py {formatted_parameters}"' ) return # todo: run without tagging. Also find a way to remove entries from text file after extraction. # elif command.lower() == 'run passive extraction': # targets = utils.fetch_passive_extraction_list() # os.system(f'gnome-terminal -e "python functions/extractor/extractor.py {targets}"') # return elif command.startswith('play me some '): utils.media_player(command[13:]) return elif command == 'data stats': H() sprint('Listing database item counts\n') sprint( '| ' + f'{ Style.BRIGHT + Fore.YELLOW + "INTEL" + Fore.RESET + Style.RESET_ALL + " (Total: " + str(persistence.get_db_count("intelligence")) + ")" }' ) tags = persistence.fetch_distinct_tags() space = len(sorted(tags, key=len)[-1]) + 10 for tag in sorted(tags): count = persistence.tag_count(tag)[0] if count > 1 and tag != 'intelligence': print(' - %s%s: %s' % (tag.title().replace('_', ' '), (' ' * (space - len(tag))), count)) sprint( '| ' + f'{ Style.BRIGHT + Fore.YELLOW + "FILES" + Fore.RESET + Style.RESET_ALL + " (Total: " + str(persistence.get_db_count("texts")) + ")" }' ) sprint( '| ' + f'{ Style.BRIGHT + Fore.YELLOW + "ARCHIVES" + Fore.RESET + Style.RESET_ALL + " (Total: " + str(persistence.get_db_count("archive")) + ")" }' ) return elif command.startswith('how do i pronounce'): try: transformed = num_word_transform.number_to_words(command[19:]) H() sprint(transformed) return except Exception as e: H() sprint(str(e)) return elif command == 'banner': banners = ['db_banner2.py', 'db_banner3.py'] # for future: add other banners here os.system('python3 ./resources/banners/%s' % random.choice(banners)) return elif command.startswith('find the') and 'root' in command: utils.find_root(command) return elif command.lower().startswith('list '): table = command[5:].strip(' ') listing_handler.list_table(table) return # todo: complete and refine implementation elif command == 'merge all relations': H() sprint( 'Merging all unprotected intel relations. This may take a while.' ) result = persistence.merge_all_relations_by_tag() logging.debug(result) handle_persistence_response(result) return elif (command.lower().startswith('merge ') and command.lower() != 'merge all relations'): tag = command[6:].replace(' ', '_') result = persistence.merge_relation_by_tag(tag) handle_persistence_response(result) return elif command.lower().startswith('pin'): table = 'intelligence' tag = 'PIN_TO_START' document_to_be_tagged = command[4:].strip(' ') result = persistence.update_doc_flags(table, document_to_be_tagged, tag) handle_persistence_response(result) return elif command.lower() == 'clear pins': result = persistence.clear_all_pins() handle_persistence_response(result) return elif 'population density' in command: utils.population_density() return elif 'terminal' in command: H() sprint('Terminal open.') display_manager.terminal_session() return elif command == 'htop': os.system('htop') return elif command == 'clean swap': logging.info('attempting to transfer swap memory to RAM') print('') os.system('sudo swapoff -a && sudo swapon -a') logging.info('Swap cleansed.') H() sprint('Swap cleansed.') return elif command == 'clean db': onboarding.clean_db() return elif 'network diagnostic' in command or command == 'netdog': print('') os.system('sudo nmcli radio wifi off') logging.debug('Turning wifi off.') os.system('nmcli radio wifi on') logging.debug('Turning wifi on.') os.system('sudo service network-manager restart') logging.debug('Restarting network manager.') H() sprint('Diagnosis complete. Counter-measures deployed.') return elif command == 'whats my ip': H() os.system('dig +short myip.opendns.com @resolver1.opendns.com') return elif 'all systems shutdown' in command: if os.environ['LUNA_USER'] == 'FRTNX': try: os.system('sudo shutdown now') except KeyboardInterrupt: return except Exception as e: H() print(e) return else: H() sprint(random.choice(DoA)) return elif command.startswith('convert'): utils.converter(command) return elif 'reboot all systems' in command: # refine condition to only execute when called by primary user if os.environ['LUNA_USER'] == 'FRTNX': try: os.system('sudo reboot now') except KeyboardInterrupt: return except Exception as e: H() print(e) return else: H() sprint(random.choice(DoA)) return elif command.startswith('show me all '): utils.nearby(command[12:]) return elif command.startswith('dict'): utils.dictionary() return elif 'fibonacci' in command: utils.laFibonacci() return elif 'koan' in command: utils.zen() return elif command == 'history': print('') os.system('sudo python3 herodotus.py') return elif 'exit' in command: H() sprint(random.choice(farewell_responses)) logging.warn('shutting down...') exit() else: H() sprint(kernel.respond(command)) result = persistence.insert_session_data(command) logging.info(result) return except Exception as e: rollbar.report_exc_info() H() print(e) return
def intent_and_entity_rerouter(text): logging.info('Intent classifier received: %s' % text) for ignorable in nlu_ignore: if (text.lower().startswith(ignorable)): command_handler(text) return True THRESHOLD = 0.75 try: nlu_response = func_timeout(1, nlu_parser, args=(text, )) except (FunctionTimedOut, Exception) as e: logging.error(f'Error getting NLU data: {e}') return False logging.debug('Intent classifier response: %s' % nlu_response) if nlu_response['intent']['confidence'] >= THRESHOLD: intent = nlu_response['intent']['name'] entities = nlu_response['entities'] has_entities = isinstance(entities, list) and len(entities) > 0 if intent == 'get_weather': logging.info( 'Weather request acknowledged. Sending through designated path.' ) if entities: weather.get_weather(False, False, *[entities[0]['value']]) else: weather.get_weather() return True elif intent == 'find_info' and has_entities: # TODO: consider how to make images and local lookup optional # possible intents: toggle_image_display (translated entities: on/off); toggle_air_gap (grants or removes Luna's access to the internet, # and, more importantly, the internets access to Luna) action = intel_handler.informant(entities[0]['value'].title(), True, 0, False) logging.info(f'Caller received action: {action}') if action: handle_user_input(action) return True elif intent == 'find_images': if utils.is_online(): entity = entities[0]['value'] try: image_urls = wikipedia.page(entity).images render_images = StoppableThread( target=display_manager.fetch_images, args=( entities[0]['value'], image_urls, )) render_images.daemon = True render_images.start() H() sprint(random.choice(pending_image_search_responses)) except Exception as e: logging.error(e) H() sprint('For some reason I could not comply.') else: H() sprint('I need an internet connection to comply.') return True elif intent == 'find_related_info' and has_entities: utils.find_related(entities[0]['value']) return True elif intent == 'directions' and has_entities: origin = None destination = None for entity in entities: if entity['entity'] == 'source': origin = entity['value'] elif entity['entity'] == 'destination': destination = entity['value'] logging.info( 'Parsing direction query with destination: %s and origin: %s' % (destination, origin)) if destination: utils.directions(destination, origin) else: H() sprint('No destination found.') return True elif intent == 'find_location' and has_entities: utils.find_location(entities[0]['value']) return True elif intent == 'find_more_info' and has_entities: action = intel_handler.informant(entities[0]['value'].title(), False, 0, True) logging.info(f'Caller received action: {action}') if action: handle_user_input(action) else: return True else: return False return False