コード例 #1
0
ファイル: run.py プロジェクト: wangyu190810/python-skill
def update(key):
    stmt = get_cache(key)
    if stmt is None:
        set_cache(key,10086)
        return "set success"
    else:
        return get_cache(key)
コード例 #2
0
ファイル: run.py プロジェクト: wangyu190810/python-skill
def index():
    sql = text("SELECT * FROM study")
    stmt = get_pg_index(sql)
    for key,values in stmt.iteritems():
        set_cache(key,values)
    time.sleep(10)
    return "key set"
コード例 #3
0
def others_menu(message):
    for p in product.get_others_titles():
        if message.text == p:
            bot.send_chat_action(message.chat.id, 'upload_photo')

            img_url = product.get_others_by_title(message.text)['picture']
            try:
                urllib2.urlretrieve(img_url, 'cache/other.jpg')
                img = open('cache/other.jpg', 'rb')
            except:
                urllib2.urlretrieve(img_url, 'cache/other_two.jpg')
                img = open('cache/other_two.jpg', 'rb')

            product_ = product.get_others_by_title(message.text)
            bot.send_message(message.chat.id,
                             'Ваш продукт: ',
                             reply_markup=keyboards.back_keyboard())
            bot.send_photo(message.chat.id,
                           img,
                           messages.product_data(product_),
                           parse_mode='HTML',
                           reply_markup=keyboards.add_to_basket())
            img.close()
            db.add_order(message.chat.id, message.text, product_['comp'],
                         product_['price'], product_['picture'])
            db.set_cache(message.chat.id, message.text)

    if message.text == '⬅ Назад':
        bot.send_message(message.chat.id,
                         'Выберите раздел, чтобы вывести список блюд 👇🏻',
                         reply_markup=keyboards.categories())
        db.delete_empty_orders(message.chat.id)
        states.set_state(message.chat.id, States.S_MENU.value)
    elif message.text == '🏠 Начало':
        bot.send_message(message.chat.id,
                         '🏠 Главное меню',
                         reply_markup=keyboards.main_menu())
        states.set_state(message.chat.id, States.S_MAIN_MENU.value)
        db.delete_empty_orders(message.chat.id)
    elif message.text == '📥 Корзина':
        bot.send_message(message.chat.id,
                         messages.basket(message.chat.id),
                         parse_mode='HTML',
                         reply_markup=keyboards.basket())
    elif not product.get_others_by_title(message.text)['title']:
        bot.send_message(
            message.chat.id,
            'Неизвесное название продукта попробуйте другое!\n'
            'Или попробуйте /start или /help')
コード例 #4
0
ファイル: util.py プロジェクト: ayanamist/TwiOtaku
 def make_namespace(self, single, no_duplicate=False):
     if single is None:
         return
     old_no_duplicate = self.no_duplicate
     self.no_duplicate = no_duplicate
     if isinstance(single, twitter.DirectMessage):
         single_type = db.TYPE_DM
     else:
         single_type = db.TYPE_STATUS
     short_id, short_id_alpha = self.generate_short_id(single['id_str'], single_type)
     db.set_cache(single['id_str'], single)
     t = time.mktime(email.utils.parsedate(single['created_at']))
     t += 28800 # GMT+8
     date_fmt = self._user['date_fmt'] if self._user['date_fmt'] else config.DEFAULT_DATE_FORMAT
     single['created_at_fmt'] = time.strftime(date_fmt.encode('UTF8'), time.localtime(t)).decode('UTF8')
     single_source = single.get('source')
     if single_source:
         gt_index = single_source.find('>')
         lt_index = single_source.rfind('<')
         if gt_index != -1 and lt_index != -1:
             single['source'] = single_source[gt_index + 1:lt_index]
     single['short_id_str_num'] = short_id
     single['short_id_str_alpha'] = short_id_alpha
     single['text'] = self.parse_text(single)
     retweeted_status = single.get('retweeted_status')
     if retweeted_status:
         single['retweeted_status'] = self.make_namespace(retweeted_status)
         retweet = single
         single = retweeted_status
         single['retweet'] = retweet
         del single['retweet']['retweeted_status']
     if single.get("in_reply_to_status", "") is None:
         try:
             single['in_reply_to_status'] = self._api.get_status(single['in_reply_to_status_id_str'])
         except twitter.Error:
             pass
     if single.get('in_reply_to_status') is not None:
         single['in_reply_to_status'] = self.make_namespace(single['in_reply_to_status'])
     self.no_duplicate = old_no_duplicate
     return single
コード例 #5
0
def add_to_basket(call):
    state = states.get_current_state(call.message.chat.id)
    if call.data == 'add_to_basket':
        bot.edit_message_reply_markup(call.message.chat.id,
                                      call.message.message_id,
                                      reply_markup=keyboards.chose_amount())
        bot.answer_callback_query(call.id, 'Выберите количество')
        states.set_state(call.message.chat.id, States.S_CHOSE_AMOUNT.value)

    elif call.data == 'add_to_basket_pizza':
        bot.edit_message_reply_markup(
            call.message.chat.id,
            call.message.message_id,
            reply_markup=keyboards.chose_pizza_weight())
        bot.answer_callback_query(call.id, 'Выберите вес ⬇')
        weights = product.get_pizza_weight_by_title(
            db.get_cache(call.message.chat.id))
        bot.send_message(call.message.chat.id,
                         'Выберите вес ⬇',
                         reply_markup=keyboards.pizza_weights(weights))
        states.set_state(call.message.chat.id,
                         States.S_CHOSE_PIZZA_WEIGHT.value)

    elif call.data == 'chose_weight':
        bot.answer_callback_query(call.id, 'Выберите вес')

    elif call.data == 'back_to_menu':
        bot.answer_callback_query(call.id, '⬅ Назад')
        bot.send_message(call.message.chat.id,
                         'Выберите раздел, чтобы вывести список блюд 👇🏻',
                         reply_markup=keyboards.categories())
        states.set_state(call.message.chat.id, States.S_MENU.value)

    elif call.data == 'back':
        bot.edit_message_reply_markup(call.message.chat.id,
                                      call.message.message_id,
                                      reply_markup=keyboards.add_to_basket())
        bot.answer_callback_query(call.id, '⬅ Назад')
        bot.send_message(call.message.chat.id,
                         'Выберите раздел, чтобы вывести список блюд 👇🏻',
                         reply_markup=keyboards.categories())
        states.set_state(call.message.chat.id, States.S_MENU.value)
        db.delete_empty_orders(call.message.chat.id)
    elif call.data == 'chose_amount':
        bot.answer_callback_query(call.id, 'Выберите колличество')

    elif state == States.S_CHOSE_AMOUNT.value:
        title = db.get_cache(call.message.chat.id)
        if call.data == '1':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 1
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

        elif call.data == '2':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 2
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

        elif call.data == '3':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 3
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

        elif call.data == '4':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 4
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

        elif call.data == '5':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 5
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

        elif call.data == '6':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 6
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)
        elif call.data == '7':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 7
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

        elif call.data == '8':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 8
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)
        elif call.data == '9':
            sum = db.get_orders_by_chat_id_and_title(call.message.chat.id,
                                                     title)[0][2] + 9
            db.edit_order_amount(call.message.chat.id, title, sum)
            bot.edit_message_reply_markup(
                call.message.chat.id,
                call.message.message_id,
                reply_markup=keyboards.add_to_basket())
            bot.answer_callback_query(call.id, '✅ Успешно добавлено в корзину')
            bot.send_message(
                call.message.chat.id, '✅ Успешно добавлено в корзину\n\n'
                'Вы уже можете оформить ваш заказ, кликнув на кнопку "📥 Корзина"',
                reply_markup=keyboards.main_menu())
            states.set_state(call.message.chat.id, States.S_MAIN_MENU.value)

    elif call.data == 'clear_basket':
        db.clear_basket(call.message.chat.id)
        bot.answer_callback_query(call.id, '✅ Корзина очищена')
        bot.send_message(
            call.message.chat.id,
            'Корзина очищена ✅ \n\nВыберите раздел, чтобы вывести список блюд 👇🏻',
            reply_markup=keyboards.categories())
        states.set_state(call.message.chat.id, States.S_MENU.value)

    elif call.data == 'reg_order':
        try:
            orders = db.get_orders_by_chat_id(call.message.chat.id)
            sum = 0
            for o in orders:
                sum = sum + o[5] * o[2]
        except:
            sum = 0
        if sum <= 0:
            bot.answer_callback_query(call.id, '❌ Не доступно')
            bot.send_message(
                call.message.chat.id,
                'Минимальная сумма заказа должна быть больше чем 0 руб.')
        else:
            bot.answer_callback_query(call.id, '✅ Выберите вид доставки')
            bot.send_message(
                call.message.chat.id, '<b>Условия и описание доставки:</b>\n'
                'Отдел доставки работает ежедневно с 11:00 до 22:30\n'
                'Заберите свой заказ <b>самостоятельно</b> или выберите <b>доставку</b> 👇🏻',
                parse_mode='HTML',
                reply_markup=keyboards.check_delivery())
            states.set_state(call.message.chat.id, States.S_DELIVERY.value)
            orders = db.get_orders_by_chat_id(call.message.chat.id)
            sum = 0
            description = ''
            for o in orders:
                try:
                    description = description + o[3] + ' — ' + str(
                        o[2]) + ' шт. \n(' + o[7] + ') = ' + str(
                            o[5] * o[2]) + ' руб.;'
                except:
                    description = description + o[3] + ' — ' + str(
                        o[2]) + ' шт. = ' + str(o[5] * o[2]) + ' руб.;'
            for o in orders:
                sum = sum + o[5] * o[2]

            db.add_reg_order(call.message.chat.id, description, sum)
            db.set_cache(call.message.chat.id,
                         db.get_reg_orders(call.message.chat.id)[-1][0])