Beispiel #1
0
def crawl(phantom_port=9802):
    """
    使用PhantomJS抓取天气网页
    :param phantom_port:
    :return: 无返回
    """
    driver = None

    try:
        driver = init_phantom_js(phantom_port, DesiredCapabilities.CHROME)
        redis = redis_lib.StrictRedis(host='127.0.0.1', port=6379, db=0)

        city_id = crawl_city_data()

        for key, value in city_id.iteritems():

            print ' '.join(['Current', str(key), str(value)])

            driver.get(''.join(['http://www.weather.com.cn/weather1d/', key, '.shtml']))

            '''
            try:
                # Or EC.invisibility_of_element_located
                WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="today"]/div[1]/div/div[4]/span')))
            except Exception, e:
                print str(e)
            '''

            # 限行
            find, data = is_not_blank(xian_xing(driver))
            if find:
                set_string(redis, WEATHER_KEY_XX_PREFIX, key, value, data)

            # 气温
            find, data = is_not_blank(qi_wen(driver))
            if find:
                set_num(redis, WEATHER_KEY_PREFIX, key, value, data)

            # 更新时间
            find, data = is_not_blank(fa_bu_shi_jian(driver))

            if find and data is not None:
                data_ = data.encode('utf8')
                if data_.index('实况') != -1:
                    data = data_.replace('实况', '')
                    set_string(redis, WEATHER_KEY_UD_PREFIX, key, value, data)
    except Exception, e:
        print str(e)
Beispiel #2
0
def __process_telegram_command(user: User, command, user_input):
    if is_not_blank(command):
        command_argument = get_command_argument(user_input, command)
        response = COMMAND_HANDLERS.get(
            command, handle_invalid_command)(command_argument)

        send_message(user, command, response)
Beispiel #3
0
def __process_individual_telegram_command_with_parameter(
        command, user_input, user):
    if is_not_blank(command):
        return COMMAND_HANDLERS.get(command,
                                    handle_invalid_command)(user_input, user)
    else:
        return ''
Beispiel #4
0
def __process_dialogflow_input(user: User, session: Session, user_input):
    intent_result = detect_intent_via_text(session.id, user_input)

    intent_action = default_if_blank(intent_result.action, '')

    if is_not_blank(intent_action):
        INTENT_HANDLERS.get(intent_action,
                            handle_invalid_intent)(user, intent_result,
                                                   session.id)
Beispiel #5
0
def get_current_session(user: User):
    session_key = __session_key(user)
    session_id = cache.get(session_key)

    if is_not_blank(session_id):
        return Session(session_id, False)
    else:
        new_session_id = uuid.uuid4().hex
        cache.set(session_key, new_session_id)
        return Session(new_session_id, True)
def __show_menu_options(user: User, intent_result, session_id):
    response = intent_result.fulfillment_text
    if is_not_blank(response):
        return send_message_with_options(user,
                                         intent_result.intent.display_name,
                                         session_id, response,
                                         *list(MENU_CODES_TO_OPTIONS.values()))
    else:
        return send_message(user, intent_result.intent.display_name,
                            session_id, DEFAULT_ERROR_MESSAGE)
Beispiel #7
0
def crawl(phantom_port=9801):
    """
    使用PhantomJS抓取天气网页
    :param phantom_port:
    :return:
    """
    driver = None

    try:
        driver = driver = init_phantom_js(phantom_port, DesiredCapabilities.FIREFOX)
        redis = redis_lib.StrictRedis(host='127.0.0.1', port=6379, db=0)

        city_id = crawl_city_data()

        for key, value in city_id.iteritems():
            driver.get(''.join(['http://www.weather.com.cn/air/?city=', key]))

            '''
            try:
                # Or EC.invisibility_of_element_located
                WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.aqi')))
            except Exception, e:
                print str(e)
            '''

            # pm25
            find, data = is_not_blank(pm25(driver))
            if find:
                set_num(redis, WEATHER_KEY_PM25_PREFIX, key, value, data)

            # 污染程度
            find, data = is_not_blank(wrcd(driver))
            if find:
                set_string(redis, WEATHER_KEY_WRCD_PREFIX, key, value, data)

            # pm10
            find, data = is_not_blank(pm10(driver))
            if find:
                set_num(redis, WEATHER_KEY_PM10_PREFIX, key, value, data)
    except Exception, e:
        print str(e)
Beispiel #8
0
def webhook():
    req_body = request.get_json()

    if req_body is None:
        return "ERROR: No request body", 400

    user = get_user_from_request(req_body)
    session = get_current_session(user)
    user_input = get_user_input_from_request(req_body)
    commands = get_user_command_from_request(req_body)

    if is_not_blank(user.id, user_input):
        __process_request(user, session, user_input, commands)

    return ''
Beispiel #9
0
def webhook():
    req_body = request.get_json()

    if req_body is None:
        return 'ERROR: No request body', 400

    pprint(req_body)

    user = get_user_from_request(req_body)
    command = get_user_command_from_request(req_body)
    user_input = get_user_input_from_request(req_body)

    if is_not_blank(user.id, command, user_input):
        __process_telegram_command(user, command, user_input)

    return ''
Beispiel #10
0
def webhook():
    req_body = request.get_json()
    user = get_user_from_request(req_body)
    user_input = get_user_input_from_request(req_body)
    commands = get_user_command_from_request(req_body)

    if user == '':
        return ''

    elif is_not_blank(user.id, user_input):
        session = get_current_session(user)
        if 'message' in req_body:
            __process_request(user, session, user_input, commands)

    else:
        pass

    return ''
Beispiel #11
0
def __process_individual_telegram_command(command):
    if is_not_blank(command):
        return COMMAND_HANDLERS.get(command, handle_invalid_command)()
    else:
        return ''