示例#1
0
def run():
    data = {}
    existing = set()
    for query in TERMS:
        for source, faucet in DRIPPING_FAUCETS.items():
            try:
                logger.warn(f"----- Crawling {source} -----")
                start = time.time()

                docs = faucet.find(query, existing)
                data.update(docs)

                delta = time.time() - start
                average = 0
                if len(docs):
                    average = delta / len(docs)

                logger.warn(
                    f"----- Got {len(docs)} in {round(delta, 2)} seconds ({round(average, 2)}s average) -----"
                )
            except Exception as e:
                logger.error(e)

    articles = list(map(translate, data.values()))
    # TODO (ethanzh) get maps API key
    #articles_with_location = location.add_location_data(articles)

    db.create(db.Article, articles)

    preload_filter_options()
    mongo_to_meili()
示例#2
0
 def create_like(key, uid):
     data = {
         'uid': uid
     }
     try:
         db.create(Like.entry + '/' + str(key) + '/' + uid, data)
         return True
     except NameError:
         print(NameError)
 def create_graffiti(description, url, user):
     new_key = keygen.gen_random_key()
     data = {
         'key': new_key,
         'description': str(description),
         'nlikes': 0,
         'url': url,
         'user': user
     }
     db.create(Graffiti.entry + '/' + new_key, data)
     return Graffiti(key=new_key,
                     description=description,
                     n_likes=0,
                     url=url,
                     user=User.get_by_uid(user))
示例#4
0
 def create_rating(grade, description, leisure, user):
     new_key = keygen.gen_random_key()
     data = {
         'key': new_key,
         'grade': grade,
         'description': str(description),
         'nlikes': 0,
         'leisure': leisure,
         'user': user
     }
     db.create(Rating.entry + '/' + new_key, data)
     return Rating(key=new_key,
                   grade=grade,
                   description=description,
                   n_likes=0,
                   leisure=leisure,
                   user=user)
示例#5
0
    def create_user(uid, email, display_name, profile_picture):
        exists = bool(db.get_dict(User.entry, order='uid', value=uid))

        if not exists:
            data = {
                'uid': uid,
                'email': email,
                'displayName': display_name,
                'profilePicture': profile_picture
            }
            db.create(User.entry + '/' + uid, data)
            return User(email=email,
                        uid=uid,
                        display_name=display_name,
                        profile_picture=profile_picture)
        else:
            return None
示例#6
0
def preload_filter_options():
    """
  Aggregate all Articles' existing values for given FILTER_OPTION_KEYS and save them to the FilterOption collection in Mongo, replacing those that already exist.
  """
    filter_options = {key: set() for key in FILTER_OPTION_KEYS}
    # keep track of set of casefolded values to preserve case of the common values
    existing_filter_options = {key: set() for key in FILTER_OPTION_KEYS}

    for article in db.Article.objects().only(*FILTER_OPTION_KEYS):
        for key, s in filter_options.items():
            value = str(eval(f"article.{key}") or "")
            if value and value.casefold() not in existing_filter_options[key]:
                s.add(value)
                existing_filter_options[key].add(value.casefold())

    filter_option_data = []
    for key, values in filter_options.items():
        for value in values:
            filter_option_data.append({"key": key, "value": value})
    # clear collection before adding new values
    db.FilterOption.objects.delete()
    db.create(db.FilterOption, filter_option_data)
 def create_user_leisure(name, description, address, url_photo, schedule,
                         coordinates, user):
     new_key = keygen.gen_random_key()
     data = {
         'key': new_key,
         'name': name,
         'description': str(description),
         'address': address,
         'url_photo': url_photo,
         'schedule': schedule,
         'coordinates': coordinates,
         'user': user,
         'nlikes': 0
     }
     db.create(UserLeisure.entry + '/' + new_key, data)
     return UserLeisure(key=new_key,
                        description=description,
                        name=name,
                        address=address,
                        url_photo=url_photo,
                        schedule=schedule,
                        coordinates=coordinates,
                        user=User.get_by_uid(user))
示例#8
0
def webhook():
    #db.clear()
    db.create()
    #log( db.showAll())

    USER = ""
    QUESTION = ""

    # endpoint for processing incoming messaging events
    data = request.get_json()
    #log(data)  # you may not want to log every incoming message in production, but it's good for testing

    if data["object"] == "page":

        for entry in data["entry"]:
            for messaging_event in entry["messaging"]:

                log("---------- INCOMING MESSAGE: -----------")
                log(messaging_event)

                if messaging_event.get("message"):  # someone sent us a message

                    sender_id = messaging_event["sender"][
                        "id"]  # the facebook ID of the person sending you the message
                    message_text = messaging_event["message"][
                        "text"]  # the message's text
                    recipient_id = messaging_event["recipient"][
                        "id"]  # the recipient's ID, which should be your page's facebook ID
                    USER = db.identifyUser(sender_id)
                    QUESTION = db.questionUser(sender_id)
                    if (message_text == "RESET" or message_text == "START"
                            or message_text == "STOP"):
                        if message_text == "STOP":
                            pair_id = db.findMatchingId(sender_id)
                            if message_text == "STOP" and USER == "CLIENT" and pair_id:
                                send_message(
                                    pair_id,
                                    "The conversation has been ended. Thank you for your help.\nYou are on standby until next client contacts for help."
                                )
                                send_message(
                                    sender_id,
                                    "The conversation has been ended. Thank you for using Trevor. Type RESET to ask another question."
                                )
                                send_rating(sender_id)
                                db.unpair(sender_id, pair_id)
                                db.updateLawyerQuestion("WAITITNG")
                                pair_id = db.findMatchingId(sender_id)
                                while pair_id == None and db.questionUser(
                                        pair_id) != "DONE":
                                    pair_id = db.findMatchingId(sender_id)
                                if pair_id != None and db.questionUser(
                                        pair_id) == "DONE":
                                    log(pair_id)
                                    db.updateLawyerQuestion("DONE")
                                    send_message(
                                        pair_id,
                                        "You have been connected to a legal advisor. \n\nThis is the information he or she has provided:\nName: %s \nState: %s\n Below is his or her answer."
                                        % (db.getLawyerName(sender_id),
                                           db.getLawyerState(sender_id)))
                                    send_message(
                                        sender_id,
                                        "You have been connected to a client. \n\nThis is the information your client has provided:\nAge: %d \nState: %s \nTopic: %s \nBelow is his or her inquiry"
                                        % (db.getClientAge(pair_id),
                                           db.getClientState(pair_id),
                                           db.getClientTopic(pair_id)))
                            elif message_text == "STOP" and USER == "LAWYER" and pair_id:
                                send_message(
                                    sender_id,
                                    "The conversation has been ended. Thank you for your help.\nYou are on standby until next client contacts for help."
                                )
                                send_message(
                                    pair_id,
                                    "The conversation has been ended. Thank you for using Trevor."
                                )
                                send_rating(pair_id)
                                db.unpair(pair_id, sender_id)
                                db.updateLawyerQuestion("WAITITNG")
                                pair_id = db.findMatchingId(sender_id)
                                while pair_id == None and db.questionUser(
                                        pair_id) != "DONE":
                                    pair_id = db.findMatchingId(sender_id)
                                if pair_id != None and db.questionUser(
                                        pair_id) == "DONE":
                                    log(pair_id)
                                    db.updateLawyerQuestion("DONE")
                                    send_message(
                                        pair_id,
                                        "You have been connected to a legal advisor. \n\nThis is the information he or she has provided:\nName: %s \nState: %s\n Below is his or her answer."
                                        % (db.getLawyerName(sender_id),
                                           db.getLawyerState(sender_id)))
                                    send_message(
                                        sender_id,
                                        "You have been connected to a client. \n\nThis is the information your client has provided:\nAge: %d \nState: %s \nTopic: %s \nBelow is his or her inquiry"
                                        % (db.getClientAge(pair_id),
                                           db.getClientState(pair_id),
                                           db.getClientTopic(pair_id)))
                        else:
                            USER = ""
                            QUESTION = ""
                            db.removeId(sender_id)
                            send_start(sender_id)  # VOLUNTEER OR CLIENT?

                    elif message_text == "HELP":
                        send_message(sender_id,
                                     "Type STOP to end conversation.")
                        send_message(sender_id, "Type RESET to restart.")

                    log("-------USER: "******"-------QUESTION: " + QUESTION)

                    if USER == "CLIENT":

                        if QUESTION == "AGE":
                            send_message(sender_id, "received " + message_text)
                            if message_text != "SKIP":
                                db.updateClientAge(sender_id,
                                                   int(message_text))
                            db.updateClientQuestion(sender_id, "STATE")
                            send_message(
                                sender_id,
                                "(OPTIONAL - for your legal advisor to better understand your case) \nEnter in your state (eg. NY) or enter SKIP:"
                            )  #send_message("Enter in the initials of your state (eg: NY or PA) OR enter SKIP:")
                            # save message_text as STATE
                            QUESTION = "STATE"

                        elif QUESTION == "STATE":
                            send_message(sender_id, "received " + message_text)
                            if message_text != "SKIP":
                                db.updateClientState(sender_id, message_text)
                            db.updateClientQuestion(sender_id, "DONE")
                            send_message(
                                sender_id,
                                "We will connect you to your volunteer legal advisor shortly."
                            )
                            pair_id = db.findMatchingId(sender_id)
                            while pair_id == None and db.questionUser(
                                    pair_id) != "DONE":
                                pair_id = db.findMatchingId(sender_id)
                            if pair_id != None and db.questionUser(
                                    pair_id) == "DONE":
                                log(pair_id)
                                #send_message( pair_id, "Legal adivisor and client connection established. \n\nThis is the information he or she has provided:\nName: %s \nState: %s\n Below is his or her answer."%( db.getLawyerName( pair_id ), db.getLawyerState( pair_id ) ) )
                                send_message(
                                    sender_id,
                                    "You have been connected to a legal advisor. \n\nThis is the information he or she has provided:\nName: %s \nState: %s\n Below is his or her answer."
                                    % (db.getLawyerName(pair_id),
                                       db.getLawyerState(pair_id)))
                                send_message(
                                    pair_id,
                                    "You have been connected to a client. \n\nThis is the information your client has provided:\nAge: %d \nState: %s \nTopic: %s \nBelow is his or her inquiry"
                                    % (db.getClientAge(sender_id),
                                       db.getClientState(sender_id),
                                       db.getClientTopic(sender_id)))

                        elif QUESTION == "DONE":
                            #send_message( sender_id, "handshake betch")
                            pair_id = db.findMatchingId(sender_id)
                            send_message(pair_id, message_text)
                            # save message_text as STATE

                    elif USER == "VOLUNTEER":

                        if QUESTION == "NAME":
                            send_message(sender_id, "received " + message_text)
                            if message_text != "SKIP":
                                db.updateLawyerName(sender_id, message_text)
                            db.updateLawyerQuestion(sender_id, "STATE")
                            send_message(
                                sender_id,
                                "(Enter in your state (eg. NY) or enter SKIP:"
                            )  #send_message("Enter in the initials of your state (eg: NY or PA) OR enter SKIP:")
                            # save message_text as STATE
                            QUESTION = "STATE"

                        elif QUESTION == "STATE":
                            send_message(sender_id, "received " + message_text)
                            if message_text != "SKIP":
                                db.updateLawyerState(sender_id, message_text)
                            db.updateLawyerQuestion(sender_id, "DONE")
                            send_message(
                                sender_id,
                                "You will be contacted by a client shortly.")
                            pair_id = db.findMatchingId(sender_id)
                            while pair_id == None and db.questionUser(
                                    pair_id) != "DONE":
                                pair_id = db.findMatchingId(sender_id)
                            if pair_id != None and db.questionUser(
                                    pair_id) == "DONE":
                                log(pair_id)
                                send_message(
                                    pair_id,
                                    "You have been connected to a legal advisor. \n\nThis is the information he or she has provided:\nName: %s \nState: %s\n Below is his or her answer."
                                    % (db.getLawyerName(sender_id),
                                       db.getLawyerState(sender_id)))
                                send_message(
                                    sender_id,
                                    "You have been connected to a client. \n\nThis is the information your client has provided:\nAge: %d \nState: %s \nTopic: %s \nBelow is his or her inquiry"
                                    % (db.getClientAge(pair_id),
                                       db.getClientState(pair_id),
                                       db.getClientTopic(pair_id)))
                        elif QUESTION == "DONE":
                            #send_message( sender_id, "second handshake betch")
                            pair_id = db.findMatchingId(sender_id)
                            send_message(pair_id, message_text)

                    else:
                        log("----------- MESSAGE NOT CAUGHT -----------")
                    '''

                    if( db.findMatchingId( sender_id ) != None ):
                        new_recipient_id = findMatchingId( sender_id )
                        send_message( new_recipient_id, message_text )
                    else:
                        send_message( sender_id, "Are you a lawyer or client?")

                    '''
                    # respond

                if messaging_event.get("delivery"):  # delivery confirmation
                    pass

                if messaging_event.get("optin"):  # optin confirmation
                    pass

                if messaging_event.get(
                        "postback"
                ):  # user clicked/tapped "postback" button in earlier message
                    sender_id = messaging_event["sender"]["id"]

                    action = messaging_event["postback"]["payload"]
                    log("------- BUTTON PRESSED: " + action)

                    if action == "VOLUNTEER":
                        USER = "******"
                        #send_message(sender_id, "You are a volunteer")
                        tempDict = {}
                        tempDict['id'] = sender_id
                        tempDict['name'] = 'N/A'
                        tempDict['currState'] = 'N/A'
                        db.addLawyer(tempDict)
                        db.updateLawyerQuestion(sender_id, "NAME")
                        send_message(sender_id, "Enter in your name:")
                    elif action == "CLIENT":
                        USER = "******"
                        #send_message(sender_id, "You are a client")
                        tempDict = {}
                        tempDict['id'] = sender_id
                        tempDict['focus'] = 'N/A'
                        tempDict['currState'] = 'N/A'
                        tempDict['age'] = 0
                        #db.addClient( {'id':sender_id, 'age' : 0, 'focus' : 'N/A', 'currState' : 'N/A'} )
                        db.addClient(tempDict)
                        send_categories(sender_id)
                    elif action == "IMMIGRATION_LAW" or action == "CITIZENSHIP" or action == "VISA":
                        db.updateClientFocus(sender_id, action)
                        log(db.questionUser(sender_id))
                        db.updateClientQuestion(sender_id, "AGE")
                        send_message(
                            sender_id,
                            "(OPTIONAL - for your legal advisor to better understand your case) \nEnter in your age OR enter SKIP:"
                        )

    return "ok", 200