Exemple #1
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    logging.info('----- express consumer starts -----')

    while common.running():
        #淘宝订单发货信息
        message_raw = redis.brpoplpush(options.queue_taobao_express,
                                       options.queue_taobao_express_processing)
        task = json.loads(message_raw, object_hook=json_hook)
        if task.tid:
            shop = db.get(
                'select distributor_shop_id from distributor_order where order_no = %s limit 1',
                task.tid)
            if shop and shop.distributor_shop_id == options.shop_id_taobao:
                #淘宝自动发货
                express_auto_push(db, redis, message_raw,
                                  options.shop_id_taobao)
            elif shop and shop.distributor_shop_id == options.shop_id_tmall:
                #天猫自动发货
                express_auto_push(db, redis, message_raw,
                                  options.shop_id_tmall)
            else:
                logging.info(
                    "can't find order in taobao and tmall. order_no = %s",
                    task.tid)

    logging.info('----- express consumer shut down -----')
Exemple #2
0
def main_loop():
    redis = common.redis_client()

    while common.running():
        email_raw = redis.brpoplpush(options.queue_email, options.queue_email_processing)
        email_json = json.loads(email_raw, object_hook=json_hook)

        smtp = smtplib.SMTP()
        smtp.connect(options.mail_smtp_host, 25)
        smtp.starttls()
        smtp.login(options.mail_smtp_user, options.mail_smtp_pwd)

        msg = MIMEMultipart('alternative')
        msg['From'] = options.mail_smtp_from
        msg['Subject'] = email_json.subject
        msg['To'] = email_json.to_list

        if 'text' in email_json:
            plain_part = MIMEText(email_json.text.encode('utf-8'), 'plain', 'utf-8')
            plain_part['Accept-Language'] = 'zh-CN'
            plain_part['Accept-Charset'] = 'ISO-8859-1,utf-8'
            msg.attach(plain_part)

        html_part = MIMEText(email_json.html.encode('utf-8'), 'html', 'utf-8')
        html_part['Accept-Language'] = 'zh-CN'
        html_part['Accept-Charset'] = 'ISO-8859-1,utf-8'
        msg.attach(html_part)

        receivers = email_json.to_list.split(',')
        smtp.sendmail(options.mail_smtp_from, receivers, msg.as_string())

        redis.lrem(options.queue_email_processing, 0, email_raw)
        logging.info(email_raw)

        smtp.close()
Exemple #3
0
def main_loop():

    redis = common.redis_client()
    logging.info('----- emay sms send consumer starts -----')

    while common.running():
        # 查余额
        emay = EMay('getBalance')
        response = emay.sync_fetch(serial_no=options.emay_serial_no,
                                   key=options.emay_key)
        emay.parse_response(response)
        if float(emay.message.findtext('.//return')) < 10.0:
            send_email(redis=redis,
                       subject='亿美余额不足10元提醒',
                       to_list='*****@*****.**',
                       html='亿美短信通道余额不足 10 元')

        message_raw = redis.brpoplpush(options.queue_coupon_send_emay,
                                       options.queue_coupon_send_processing)
        logging.info('get sms task: %s' % message_raw)
        task = json.loads(message_raw, object_hook=json_hook)
        mobile = task.mobile
        content = task.sms if isinstance(task.sms,
                                         unicode) else task.sms.decode('utf-8')
        content = content[:-5] + u',退订回复TD【一百券】'
        sms_id = task.sms_id if 'sms_id' in task else ''

        # 检查手机号码长度
        if len(mobile) != 11:
            logging.error(
                'coupon sms send consumer error: mobile number %s is not valid'
                % mobile)
            continue

        emay = EMay('sendSMS')
        response = emay.sync_fetch(
            serial_no=options.emay_serial_no,
            key=options.emay_key,
            send_time='',
            mobiles=mobile,
            sms_content=content.encode('utf-8'),
            add_serial='',
            src_charset='utf-8',
            sms_priority='5',
            sms_id=sms_id,
        )
        emay.parse_response(response)
        if emay.is_ok():
            redis.lrem(options.queue_coupon_send_processing, 0, message_raw)
            logging.info('re-send sms successfully: %s' % content)
        else:
            logging.error('re-send sms failed, emay response error: %s',
                          emay.message.findtext('.//return'))

    logging.info('----- coupon sms sender consumer shut down -----')
def main_loop():

    redis = common.redis_client()
    logging.info('----- emay sms send consumer starts -----')

    while common.running():
        # 查余额
        emay = EMay('getBalance')
        response = emay.sync_fetch(
            serial_no=options.emay_serial_no,
            key=options.emay_key
        )
        emay.parse_response(response)
        if float(emay.message.findtext('.//return')) < 10.0:
            send_email(redis=redis, subject='亿美余额不足10元提醒',
                       to_list='*****@*****.**', html='亿美短信通道余额不足 10 元')

        message_raw = redis.brpoplpush(options.queue_coupon_send_emay, options.queue_coupon_send_processing)
        logging.info('get sms task: %s' % message_raw)
        task = json.loads(message_raw, object_hook=json_hook)
        mobile = task.mobile
        content = task.sms if isinstance(task.sms, unicode) else task.sms.decode('utf-8')
        content = content[:-5] + u',退订回复TD【一百券】'
        sms_id = task.sms_id if 'sms_id' in task else ''

        # 检查手机号码长度
        if len(mobile) != 11:
            logging.error('coupon sms send consumer error: mobile number %s is not valid' % mobile)
            continue

        emay = EMay('sendSMS')
        response = emay.sync_fetch(
            serial_no=options.emay_serial_no,
            key=options.emay_key,
            send_time='',
            mobiles=mobile,
            sms_content=content.encode('utf-8'),
            add_serial='',
            src_charset='utf-8',
            sms_priority='5',
            sms_id=sms_id,
        )
        emay.parse_response(response)
        if emay.is_ok():
            redis.lrem(options.queue_coupon_send_processing, 0, message_raw)
            logging.info('re-send sms successfully: %s' % content)
        else:
            logging.error('re-send sms failed, emay response error: %s', emay.message.findtext('.//return'))

    logging.info('----- coupon sms sender consumer shut down -----')
Exemple #5
0
def main_loop():
    redis = common.redis_client()
    logging.info('----- coupon sms send consumer starts -----')

    while common.running():
        message_raw = redis.brpoplpush(options.queue_coupon_send,
                                       options.queue_coupon_send_processing)
        logging.info('get sms task: %s' % message_raw)
        task = json.loads(message_raw, object_hook=json_hook)
        mobile = task.mobile
        content = task.sms.encode('utf-8')

        # 检查手机号码长度
        if len(mobile) != 11:
            logging.error(
                'coupon sms send consumer error: mobile number %s is not valid'
                % mobile)
            continue

        # 发送请求
        zt = Zhutong()
        try:
            response = zt.sync_fetch(mobile=mobile, content=content, xh='')
            zt.parse_response(response)

            if zt.is_ok():
                redis.lrem(options.queue_coupon_send_processing, 0,
                           message_raw)
                logging.info('send sms successfully: %s' % content)
            elif int(zt.message) == 13:
                redis.lrem(options.queue_coupon_send_processing, 0,
                           message_raw)
                logging.info('send sms faied, 30分钟重复提交')
            else:
                redis.lpush(options.queue_coupon_send_emay, message_raw)
                logging.error('send sms failed, zhutong response error: %s',
                              response)
        except:
            redis.lpush(options.queue_coupon_send_emay, message_raw)
            logging.error('send sms failed, zhutong connection reset')

    logging.info('----- coupon sms sender consumer shut down -----')
Exemple #6
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    logging.info('----- distributor order consumer starts -----')

    while common.running():

        # 移动一个订单到处理队列
        message_raw = redis.brpoplpush(options.queue_distributor_order, options.queue_distributor_order_processing)
        task = json.loads(message_raw, object_hook=json_hook)
        logging.info('distributor order consumer pop %s distributor order, id=%s '
                     % (task.distributor, task.distributor_order_id))

        # 淘宝订单
        if task.distributor == 'TB':
            taobao_order(db, redis, task.distributor_order_id, message_raw)
        # 一号店订单
        elif task.distributor == 'YHD':
            yihaodian_order(db, redis, task.distributor_order_id, message_raw)

    logging.info('----- distributor order consumer shut down -----')
Exemple #7
0
def main_loop():
    redis = common.redis_client()

    while common.running():
        email_raw = redis.brpoplpush(options.queue_email,
                                     options.queue_email_processing)
        email_json = json.loads(email_raw, object_hook=json_hook)

        smtp = smtplib.SMTP()
        smtp.connect(options.mail_smtp_host, 25)
        smtp.starttls()
        smtp.login(options.mail_smtp_user, options.mail_smtp_pwd)

        msg = MIMEMultipart('alternative')
        msg['From'] = options.mail_smtp_from
        msg['Subject'] = email_json.subject
        msg['To'] = email_json.to_list

        if 'text' in email_json:
            plain_part = MIMEText(email_json.text.encode('utf-8'), 'plain',
                                  'utf-8')
            plain_part['Accept-Language'] = 'zh-CN'
            plain_part['Accept-Charset'] = 'ISO-8859-1,utf-8'
            msg.attach(plain_part)

        html_part = MIMEText(email_json.html.encode('utf-8'), 'html', 'utf-8')
        html_part['Accept-Language'] = 'zh-CN'
        html_part['Accept-Charset'] = 'ISO-8859-1,utf-8'
        msg.attach(html_part)

        receivers = email_json.to_list.split(',')
        smtp.sendmail(options.mail_smtp_from, receivers, msg.as_string())

        redis.lrem(options.queue_email_processing, 0, email_raw)
        logging.info(email_raw)

        smtp.close()
Exemple #8
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    logging.info('----- distributor order consumer starts -----')

    while common.running():

        # 移动一个订单到处理队列
        message_raw = redis.brpoplpush(
            options.queue_distributor_order,
            options.queue_distributor_order_processing)
        task = json.loads(message_raw, object_hook=json_hook)
        logging.info(
            'distributor order consumer pop %s distributor order, id=%s ' %
            (task.distributor, task.distributor_order_id))

        # 淘宝订单
        if task.distributor == 'TB':
            taobao_order(db, redis, task.distributor_order_id, message_raw)
        # 一号店订单
        elif task.distributor == 'YHD':
            yihaodian_order(db, redis, task.distributor_order_id, message_raw)

    logging.info('----- distributor order consumer shut down -----')
Exemple #9
0
import common
import logging
import json
from datetime import datetime, timedelta
from autumn.utils import json_hook
from tornado.options import options
from autumn.coupon import local_verify
"""美团、点评、糯米的券在一百券上验证处理"""

common.set_up()

db = common.db_client()
redis = common.redis_client()

while common.running():
    coupon = redis.brpoplpush(options.queue_coupon_local_verify,
                              options.queue_coupon_local_verify_processing)
    task = json.loads(coupon, object_hook=json_hook)
    logging.info('local coupon verify pop %s ,shop_id:%s ' %
                 (task.coupon, task.shop_id))

    #开始对外部券进行本地验证
    verify_result = local_verify(db,
                                 task.coupon,
                                 task.shop_id,
                                 '系统',
                                 verified_at=task.used_at)

    ok, msg = (verify_result.ok, verify_result.msg)
    if ok:
Exemple #10
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    args = {'db': db, 'body': ''}

    while common.running():
        sp_id = redis.brpop('q:wx:mem:update')[1]

        args['sp_id'] = sp_id
        app_id = db.get(
            'select * from supplier_property where name="app_id" and sp_id = %s',
            sp_id)['value']
        app_secret = db.get(
            'select * from supplier_property where name="app_secret" and sp_id = %s',
            sp_id)['value']

        wx = Weixin(method='user/get', **args)
        wx.set_app_info(app_id, app_secret)

        info = Weixin(method='user/info', **args)
        info.set_app_info(app_id, app_secret)

        next_openid = ''
        while True:
            result = yield wx(next_openid=next_openid)
            wx.parse_response(result.body)

            if wx.is_ok() and 'data' in wx.message.keys():
                count = wx.message.count
                total = wx.message.total
                customs = wx.message.data.openid
                for custom in customs:
                    response = yield info(openid=custom, lang='zh_CN')
                    info.parse_response(response.body)
                    if info.is_ok():
                        nickname = info.message.nickname

                        # 暂时数据库未升级至可以存储emoji字符,故使用以下代码,升级数据库后可删除
                        import re
                        try:
                            # UCS-4
                            highpoints = re.compile(u'[\U00010000-\U0010ffff]')
                        except re.error:
                            # UCS-2
                            highpoints = re.compile(
                                u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
                        nickname = highpoints.sub(u'', nickname)
                        # todo =========================未升级前的替代方案===================

                        sex = {0: '未知', 1: '男', 2: '女'}.get(info.message.sex)
                        follow_at = datetime.fromtimestamp(
                            info.message.subscribe_time)
                        openid = info.message.openid
                        head_img = info.message.headimgurl

                        mid = db.query(
                            'select * from member where wx_id = %s and sp_id = %s',
                            custom, sp_id)

                        if mid:
                            db.execute(
                                'update member set wx_name = %s, gender = %s,'
                                'head_img = %s, wx_follow_at = %s, country = %s, province = %s, city = %s '
                                'where wx_id = %s and sp_id = %s', nickname,
                                sex, head_img, follow_at, info.message.country,
                                info.message.province, info.message.city,
                                custom, sp_id)
                        else:
                            cid = db.query(
                                'select * from member_category where sp_id = %s and name = "未分组" ',
                                sp_id)[0].id
                            db.execute(
                                'insert into member set sp_id = %s, wx_id = %s, wx_name = %s, gender = %s,'
                                'head_img = %s, wx_follow_at = %s, created_at = NOW(),'
                                'category_id = %s, country = %s, province = %s, city = %s, '
                                'source = "微信导入"', sp_id, openid, nickname,
                                sex, head_img, follow_at, cid,
                                info.message.country, info.message.province,
                                info.message.city)
                        logging.info('会员 %s 更新信息成功', custom.encode('utf-8'))
                    else:
                        logging.error('获取 %s 信息失败。失败原因 %s',
                                      custom.encode('utf-8'),
                                      error_list.get(info.error_code, ''))

                if count < 10000 or count == total:
                    break
                else:
                    next_openid = wx.message.next_openid
            else:
                logging.error('获取关注者失败.商户 %s, 错误原因 %s', sp_id,
                              error_list.get(wx.error_code, ''))
                break
Exemple #11
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    args = {
        'db': db,
        'body': ''
    }

    while common.running():
        sp_id = redis.brpop('q:wx:mem:update')[1]

        args['sp_id'] = sp_id
        app_id = db.get('select * from supplier_property where name="app_id" and sp_id = %s', sp_id)['value']
        app_secret = db.get('select * from supplier_property where name="app_secret" and sp_id = %s', sp_id)['value']

        wx = Weixin(method='user/get', **args)
        wx.set_app_info(app_id, app_secret)

        info = Weixin(method='user/info', **args)
        info.set_app_info(app_id, app_secret)

        next_openid = ''
        while True:
            result = yield wx(next_openid=next_openid)
            wx.parse_response(result.body)

            if wx.is_ok() and 'data' in wx.message.keys():
                count = wx.message.count
                total = wx.message.total
                customs = wx.message.data.openid
                for custom in customs:
                    response = yield info(openid=custom, lang='zh_CN')
                    info.parse_response(response.body)
                    if info.is_ok():
                        nickname = info.message.nickname

                        # 暂时数据库未升级至可以存储emoji字符,故使用以下代码,升级数据库后可删除
                        import re
                        try:
                            # UCS-4
                            highpoints = re.compile(u'[\U00010000-\U0010ffff]')
                        except re.error:
                            # UCS-2
                            highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
                        nickname = highpoints.sub(u'', nickname)
                        # todo =========================未升级前的替代方案===================

                        sex = {0: '未知', 1: '男', 2: '女'}.get(info.message.sex)
                        follow_at = datetime.fromtimestamp(info.message.subscribe_time)
                        openid = info.message.openid
                        head_img = info.message.headimgurl

                        mid = db.query('select * from member where wx_id = %s and sp_id = %s',
                                       custom, sp_id)

                        if mid:
                            db.execute('update member set wx_name = %s, gender = %s,'
                                       'head_img = %s, wx_follow_at = %s, country = %s, province = %s, city = %s '
                                       'where wx_id = %s and sp_id = %s',
                                       nickname, sex, head_img, follow_at, info.message.country,
                                       info.message.province, info.message.city,
                                       custom, sp_id)
                        else:
                            cid = db.query('select * from member_category where sp_id = %s and name = "未分组" ',
                                           sp_id)[0].id
                            db.execute('insert into member set sp_id = %s, wx_id = %s, wx_name = %s, gender = %s,'
                                       'head_img = %s, wx_follow_at = %s, created_at = NOW(),'
                                       'category_id = %s, country = %s, province = %s, city = %s, '
                                       'source = "微信导入"',
                                       sp_id, openid, nickname, sex, head_img, follow_at, cid, info.message.country,
                                       info.message.province, info.message.city)
                        logging.info('会员 %s 更新信息成功', custom.encode('utf-8'))
                    else:
                        logging.error('获取 %s 信息失败。失败原因 %s', custom.encode('utf-8'), error_list.get(info.error_code, ''))

                if count < 10000 or count == total:
                    break
                else:
                    next_openid = wx.message.next_openid
            else:
                logging.error('获取关注者失败.商户 %s, 错误原因 %s', sp_id, error_list.get(wx.error_code, ''))
                break
import common
import logging
import json
from datetime import datetime, timedelta
from autumn.utils import json_hook
from tornado.options import options
from autumn.coupon import local_verify

"""美团、点评、糯米的券在一百券上验证处理"""

common.set_up()

db = common.db_client()
redis = common.redis_client()

while common.running():
    coupon = redis.brpoplpush(options.queue_coupon_local_verify, options.queue_coupon_local_verify_processing)
    task = json.loads(coupon, object_hook=json_hook)
    logging.info('local coupon verify pop %s ,shop_id:%s ' % (task.coupon, task.shop_id))

    #开始对外部券进行本地验证
    verify_result = local_verify(db, task.coupon, task.shop_id, '系统', verified_at=task.used_at)

    ok, msg = (verify_result.ok, verify_result.msg)
    if ok:
        # 外部券验证处理完成,从处理队列移除
        redis.lrem(options.queue_coupon_local_verify_processing, 0, coupon)
        logging.info('local coupon verify finish,coupon_sn:%s' % task.coupon)
    else:
        logging.info('local coupon verify failed,coupon_sn:%s' % task.coupon)