def get_last_bookmark_archive_date(): """ do an Elasticsearch query for the most recent bookmark archive date return it in the same format as get_last_bookmark_update_date for comparison purposes """ s = Search('{}_bookmarks'.format(settings.PINBOARD_USERNAME), { "from": 0, "size": 1, "query": { "match_all": {} }, "fields": [ "time" ], "sort": { "time": { "order": "desc" } } }, mappers={ 'time': ElasticFieldMapper.date }) ero = s.search() first = next(ero.hits, None) return first.time if first is not None else first
def search(): """@params: q: query statement fl: filter by column count: the number of ci ret_key: id, name, alias facet: statistic wt: result format """ page = get_page(request.values.get("page", 1)) count = get_per_page(request.values.get("count")) query = request.values.get('q', "") fl = request.values.get('fl', "").split(",") ret_key = request.values.get('ret_key', "name") if ret_key not in ('name', 'alias', 'id'): ret_key = 'name' facet = request.values.get("facet", "").split(",") wt = request.values.get('wt', 'json') fl = filter(lambda x: x != "", fl) facet = filter(lambda x: x != "", facet) sort = request.values.get("sort") start = time.time() s = Search(query, fl, facet, page, ret_key, count, sort) try: response, counter, total, page, numfound, facet = s.search() except SearchError, e: return abort(400, str(e))
def driver_init(request): logging.info("Initializing the web driver") config = load_config() chrome_driver = webdriver.Chrome(config["driver"]["chrome"]) # Credentials can be handled better to avoid security issues request.cls.username = config["credentials"]["username"] request.cls.email = config["credentials"]["email"] request.cls.password = config["credentials"]["password"] request.cls.config = config request.cls.driver = chrome_driver request.cls.home = Home(request.cls.driver) request.cls.search = Search(request.cls.driver) request.cls.category = Category(request.cls.driver) request.cls.cart = Cart(request.cls.driver) yield chrome_driver.quit() logging.info("closing the web driver")
def __init__(self,driver): self.driver = driver self._search = Search(self.driver)
class Cart: def __init__(self,driver): self.driver = driver self._search = Search(self.driver) def navigate_to_cart(self): try: self.driver.find_element_by_id("nav-cart").click() return True except NoSuchElementException as e: logging.error("Navigate to cart Failed.Exception {}".format(str(e))) return False def add_product_to_cart(self,count=1,search_input='mobile'): """ :param count: int number of product to add into cart.Max allowed count is 20. :param search_input: :return: """ try: product_title_added_to_cart = [] if count>20: logging.warning("Maximum 20 products only allowed.Setting counter to 20...") count=20 self._search.search_by_keyword(search_input) product_object_list = self.driver.find_elements_by_xpath('//span[@class="a-size-medium a-color-base a-text-normal"]') logging.info("PRODUCT LENGTH IS : {}".format(len(product_object_list))) for product_index in range(0,count): element = product_object_list[product_index] title = element.text parent_window = self.driver.window_handles[0] self.driver.execute_script("window.scrollTo(0,"+str(element.location['y']-150)+")") element.click() sleep(3) new_window = self.driver.window_handles[1] self.driver.switch_to.window(new_window) self.driver.find_element_by_id("add-to-cart-button").click() logging.info("{} is added to the cart successfully...".format(title)) product_title_added_to_cart.append(title) sleep(3) self.driver.close() self.driver.switch_to.window(parent_window) return product_title_added_to_cart except NoSuchElementException as e: logging.error("Failed to add product to cart. {}".format(str(e))) def remove_first_product_from_cart(self): try: self.driver.find_element_by_xpath('.//input[@value="Delete"]').click() sleep(5) # Hardcoded sleep is not recommended.This can be better optimized return True except NoSuchElementException as e: logging.error("No item in cart to delete") return False def clear_cart(self): """ Usually there will be a backend call to clear cart and entitlement :return: """ try: if not self.navigate_to_cart(): return False while True: cart_header = self.driver.find_element_by_xpath('.//div[@id="sc-active-cart"]') if len(cart_header.find_elements_by_xpath(".//h2"))!=0: header_text = cart_header.find_element_by_xpath(".//h2").text else: header_text = cart_header.find_element_by_xpath(".//h1").text if "Your Shopping Cart is empty" in header_text: logging.info("All product removed from cart.Cart is empty now") return True else: logging.info("Removing item from cart") self.remove_first_product_from_cart() except Exception as e: logging.error("Exception ourred in clear cart. {}".format(str(e))) return False
def get_bookmark_by_hash(bookmark_hash): s = Search('{}_bookmarks'.format( settings.PINBOARD_USERNAME )) return s.get(bookmark_hash, 'bookmark', ['href'])
def run(self, dispatcher, tracker, domain): lemma = nltk.wordnet.WordNetLemmatizer() Operations = [] # get what you need from slots course = tracker.get_slot("course") knowledge = tracker.get_slot("knowledges") time = tracker.get_slot("time") # for debugging print("action_name:", self.name()) latest_message = ("example//latest_message:", tracker.latest_message) print(latest_message) # get what you need from user_input if not exist in slot # if slot exists in the latest msg, it will be autofilled in slotList. # returnType: tracker.get_latest_entity_values() -> Object<generator> : next( ) or list( ) course = list(tracker.get_latest_entity_values( "course")) if not course else course knowledge = list(tracker.get_latest_entity_values( "knowledge")) if not knowledge else knowledge time = list( tracker.get_latest_entity_values("time")) if not time else time # preprocess if course: course = str(course).lower() if knowledge: knowledge = [lemma.lemmatize(str.lower(key)) for key in knowledge] if 'exercise' in knowledge: knowledge.remove("exercise") if time: time = str(time).lower() time = time.replace(' ', '0') time = time.strip('[').strip(']') print('time: ', time) print('knowledge: ', knowledge) # If all required info has been stored in slot_set -> respond to question # otherwise -> send message for requesting what missed. if not (course and (knowledge or time)): # sending msg message = "Can you provide the course code of that knowledge?(e.g compXXXX)" if not course else "About what or time of lecture(e.g week 2)?" dispatcher.utter_message(message) # setting slot if course: Operations.append(SlotSet('course', course)) if knowledge: Operations.append(SlotSet('knowledges', knowledge)) if course and (knowledge or time): #retrieval info in DB search = Search() if course in search.db_list: # search method need imporvement for this: searhing exercises by knowledge or by time. print("time:", time) if knowledge: data = search._search_by_key(db_name=course, key=knowledge, intent='example', time=time) else: data = search._search_by_time(db_name=course, intent='example', time=time) if not len(data): dispatcher.utter_message("Nothing matched :(") dispatcher.utter_message("Is that what you want? (Yes/No)") Operations.append(SlotSet('knowledges', None)) Operations.append(SlotSet('time', None)) else: sim, index = search.tf_idf(knowledge, data) if not knowledge: ans_from = f'Here is all the examples in {time}:' dispatcher.utter_message(ans_from) title = None time = None num_key = 1 for i in index: if ((not title) or title != data[i][2]) and ( (not time) or time != data[i][1]): ans_from = f'I got you examples in {data[i][0]} about {data[i][2]}:' title = data[i][2] time = data[i][1] dispatcher.utter_message(str(num_key)) dispatcher.utter_message(ans_from) num_key += 1 dispatcher.utter_message(data[i][1]) dispatcher.utter_message("Is that what you want? (Yes/No)") Operations.append(SlotSet('knowledges', None)) Operations.append(SlotSet('time', None)) if course not in search.db_list: dispatcher.utter_message("Sorry.. I can't find course ") dispatcher.utter_message("Is that what you want? (Yes/No)") Operations.append(SlotSet('course', None)) return Operations
def run(self, dispatcher, tracker, domain): lemma = nltk.wordnet.WordNetLemmatizer() Operations = [] # for debugging print("action_name:", self.name()) latest_message = ("description//latest_message:", tracker.latest_message) print(latest_message) print(tracker.current_state) # get what you need from slots course = tracker.get_slot("course") knowledge = tracker.get_slot("knowledges") # get what you need from user_input if not exist in slot # if slot exists in the latest msg, it will be autofilled in slotList. # returnType: tracker.get_latest_entity_values() -> Object<generator> : next( ) or list( ) course = list(tracker.get_latest_entity_values( "course")) if not course else course knowledge = list(tracker.get_latest_entity_values( "knowledge")) if not knowledge else knowledge # preprocess input data from users if course: course = str(course).lower() if knowledge: knowledge = [lemma.lemmatize(str.lower(key)) for key in knowledge] # If all required info has been stored in slot_set -> respond to question # otherwise -> send message for requesting what missed. if not (course and knowledge): # sending msg message = "Can you provide the course code of that knowledge?(e.g compXXXX)" if not course else "About what(description)?" dispatcher.utter_message(message) # setting slot if course: Operations.append(SlotSet('course', course)) if knowledge: Operations.append(SlotSet('knowledges', knowledge)) if course and knowledge: #search infomation in db search = Search() if course in search.db_list: # search by keywords extracted by NLU data = search._search_by_key(db_name=course, key=knowledge) # if data matched is not exist, return message 'Nothing matched' # then reset solt if not len(data): dispatcher.utter_message("Nothing matched :(") dispatcher.utter_message("Is that what you want? (Yes/No)") Operations.append(SlotSet('knowledges', None)) Operations.append(SlotSet('time', None)) # matched data found -> excuting searching function # -> compute idf # -> ranking outcome # -> return outcome else: sim, index = search.tf_idf(knowledge, data) title = None time = None num_key = 1 for i in index: if ((not title) or title != data[i][2]) and ( (not time) or time != data[i][0]): ans_from = f'I got you something in {data[i][0]} about {data[i][2]}:' title = data[i][2] time = data[i][0] dispatcher.utter_message(str(num_key)) dispatcher.utter_message(ans_from) num_key += 1 dispatcher.utter_message(data[i][1]) print(i, data[i]) dispatcher.utter_message("Is that what you want? (Yes/No)") Operations.append(SlotSet('knowledges', None)) Operations.append(SlotSet('time', None)) if course not in search.db_list: dispatcher.utter_message("Sorry.. I can't find course ") dispatcher.utter_message("Is that what you want? (Yes/No)") Operations.append(SlotSet('course', None)) return Operations