Esempio n. 1
0
    def ajax_get_miner_data(self):
        user = App.get_user_info(self)
        if not user:
            return HttpResponse(json.dumps({
                'code': 0,
                'msg': '请登陆!'
            }),
                                content_type='application/json')
        tasks = Task.objects.filter(is_new=1,
                                    file_path='',
                                    user_id=user.user_id)
        res = []
        for val in tasks:
            data = []
            aid_li = []
            qa_is_done = 1
            reviews_is_done = 1
            task_id = val.id
            ob_time = val.created.strftime('%Y-%m-%d')
            a = datetime.datetime.now() - val.created.replace(tzinfo=None)
            if a.total_seconds() >= 7200:
                ob_time = datetime.datetime.now().strftime('%Y-%m-%d')
            for aid in eval(val.asins):
                aid = aid.replace('\n', '')
                aid_li.append(aid)

            if val.type == 1:
                qa_check = Questions.objects.filter(asin__in=aid_li, is_done=0)
                if qa_check:
                    qa_is_done = 0
                qa = Questions.objects.filter(asin__in=aid_li,
                                              created__icontains=ob_time)
                if qa and qa_is_done:
                    for q in qa:
                        answers = Answers.objects.filter(question_id=q.id)
                        if answers:
                            for v in answers:
                                re = {
                                    'question':
                                    v.question.question,
                                    'asin':
                                    v.question.asin,
                                    'asked':
                                    v.question.asked,
                                    'votes':
                                    v.question.votes,
                                    'answer':
                                    v.answer,
                                    'person':
                                    v.person,
                                    'created':
                                    v.created.strftime('%Y-%m-%d %H:%M:%S'),
                                }
                                data.append(re)
                        else:
                            re = {
                                'question': q.question,
                                'asin': q.asin,
                                'asked': q.asked,
                                'votes': q.votes,
                                'answer': '',
                                'person': '',
                                'created':
                                q.created.strftime('%Y-%m-%d %H:%M:%S'),
                            }
                            data.append(re)

                        fields = [
                            'Question', 'Asin', 'Asked', 'Votes', 'Answer',
                            'Person', 'Created'
                        ]

                        data_fields = [
                            'question', 'asin', 'asked', 'votes', 'answer',
                            'person', 'created'
                        ]
            else:
                reviews_check = AsinReviews.objects.filter(
                    aid__in=aid_li, is_done=0, created__icontains=ob_time)
                if reviews_check:
                    reviews_is_done = 0
                reviews_a = AsinReviews.objects.filter(
                    aid__in=aid_li, created__icontains=ob_time)
                reviews = Reviews.objects.filter(asin__in=aid_li,
                                                 created__icontains=ob_time)
                if reviews_a and reviews_is_done:
                    for v in reviews:
                        re = {
                            'title': v.title,
                            'name': v.name,
                            'asin': v.asin,
                            'variation': v.variation,
                            'content': v.content,
                            'review_link': v.review_link,
                            'score': v.score,
                            'is_vp': v.is_vp,
                            'review_date': v.review_date.strftime('%Y-%m-%d'),
                            'created': v.created.strftime('%Y-%m-%d'),
                        }
                        data.append(re)
                    fields = [
                        'Title', 'Name', 'Asin', 'Variation', 'Content',
                        'Review Link', 'Score', 'Vp', 'Review Date', 'Created'
                    ]

                    data_fields = [
                        'title', 'name', 'asin', 'variation', 'content',
                        'review_link', 'score', 'is_vp', 'review_date',
                        'created'
                    ]
            if data:
                file_path = get_excel_file1(self, data, fields, data_fields,
                                            task_id)
                f_time = datetime.datetime.now()
                Task.objects.filter(id=val.id).update(is_new=0,
                                                      file_path=file_path,
                                                      finish_time=f_time)
                res.append({
                    'id': task_id,
                    'file_path': file_path,
                    'f_time': f_time.strftime('%Y-%m-%d %H:%M:%S')
                })
                del (file_path)
            del (data)
            del (task_id)
        return HttpResponse(json.dumps({
            'code': 1,
            'data': res
        }),
                            content_type='application/json')
Esempio n. 2
0
    def add(self):
        user = App.get_user_info(self)
        if not user:
            return HttpResponse(json.dumps({
                'code': 0,
                'msg': '请登陆!'
            }),
                                content_type='application/json')

        type = int(self.POST.get('taskType', 1))
        name = self.POST.get('taskName', '')
        description = self.POST.get('taskDesc', '')
        asins = self.POST.get('taskASIN', '')

        if asins:
            data = []
            asins = asins.split('|')
            tasks = Task.objects.filter(
                type=type,
                created__icontains=datetime.datetime.now().strftime(
                    '%Y-%m-%d'))
            if tasks:
                file_path = ''
                for val in tasks:
                    if len(eval(val.asins)) == len(asins) and set(
                            eval(val.asins)).issubset(set(asins)):
                        file_path = val.file_path
                if file_path:
                    return HttpResponse(json.dumps({
                        'code': 1,
                        'data': {
                            'file_path': file_path,
                            'f_time': 'finish'
                        }
                    }),
                                        content_type='application/json')
            is_asin = 1
            for aid in asins:
                user_aid = UserAsins.objects.filter(aid=aid.replace('\n', ''))
                if not user_aid:
                    is_asin = 0
            if is_asin:
                if type == 0:
                    reviews = Reviews.objects.filter(
                        asin__in=asins,
                        created__icontains=Reviews.objects.aggregate(
                            Max('created'))['created__max']).all()
                    if reviews:
                        for v in reviews:
                            re = {
                                'title': v.title,
                                'name': v.name,
                                'asin': v.asin,
                                'variation': v.variation,
                                'content': v.content,
                                'review_link': v.review_link,
                                'score': v.score,
                                'is_vp': v.is_vp,
                                'review_date':
                                v.review_date.strftime('%Y-%m-%d'),
                                'created': v.created.strftime('%Y-%m-%d'),
                            }
                            data.append(re)
                        fields = [
                            'Title', 'Name', 'Asin', 'Variation', 'Content',
                            'Review Link', 'Score', 'Vp', 'Review Date',
                            'Created'
                        ]

                        data_fields = [
                            'title', 'name', 'asin', 'variation', 'content',
                            'review_link', 'score', 'is_vp', 'review_date',
                            'created'
                        ]
                else:
                    qa_max = Questions.objects.aggregate(Max('created'))
                    questions = Questions.objects.filter(
                        asin__in=asins,
                        created__icontains=qa_max['created__max'].strftime(
                            "%Y-%m-%d"))
                    for val in questions:
                        answers = Answers.objects.filter(question_id=val.id)
                        if answers:
                            for v in answers:
                                re = {
                                    'question':
                                    v.question.question,
                                    'asin':
                                    v.question.asin,
                                    'asked':
                                    v.question.asked,
                                    'votes':
                                    v.question.votes,
                                    'answer':
                                    v.answer,
                                    'person':
                                    v.person,
                                    'created':
                                    v.created.strftime('%Y-%m-%d %H:%M:%S'),
                                }
                                data.append(re)
                        else:
                            re = {
                                'question':
                                val.question,
                                'asin':
                                val.asin,
                                'asked':
                                val.asked,
                                'votes':
                                val.votes,
                                'answer':
                                '',
                                'person':
                                '',
                                'created':
                                val.created.strftime('%Y-%m-%d %H:%M:%S'),
                            }
                            data.append(re)

                    fields = [
                        'Question', 'Asin', 'Asked', 'Votes', 'Answer',
                        'Person', 'Created'
                    ]

                    data_fields = [
                        'question', 'asin', 'asked', 'votes', 'answer',
                        'person', 'created'
                    ]

            task = Task()
            task.id
            task.name = name
            task.user = user.user
            task.type = int(type)
            task.description = description
            task.asins = asins
            if not data:
                task.is_new = 1
            task.save()

            if data:
                file_path = get_excel_file1(self, data, fields, data_fields)
                id = task.id
                f_time = datetime.datetime.now()
                Task.objects.filter(id=id).update(file_path=file_path,
                                                  finish_time=f_time)
                return HttpResponse(json.dumps({'code': 1, 'data': {'id': id, 'file_path': file_path, 'f_time':\
                                                    f_time.strftime('%Y-%m-%d %H:%M:%S')}}),content_type='application/json')
            else:
                work_path = settings.SPIDER_URL
                os.chdir(work_path)
                os.system('scrapyd-deploy')
                for aid in asins:
                    if type == 0:
                        cmd_str = 'curl http://localhost:6800/schedule.json -d project=maxlead_scrapy -d spider=review_spider -d asin=%s' % aid
                    else:
                        cmd_str = 'curl http://localhost:6800/schedule.json -d project=maxlead_scrapy -d spider=qa_spider -d asin=%s' % aid
                    os.system(cmd_str)
                    os.chdir(settings.ROOT_PATH)

        else:
            return HttpResponse(json.dumps({
                'code': 0,
                'data': '任务添加失败!'
            }),
                                content_type='application/json')
Esempio n. 3
0
def get_tracking_order_status():
    # billing_date = datetime.datetime.now().strftime("%b.%y")
    # lists = TrackingOrders.objects.filter(billing_date__contains=billing_date)
    t = threading.Timer(86400.0, get_tracking_order_status)
    datetime_now = datetime.datetime.now()
    re_date = datetime_now + datetime.timedelta(days=-30)
    lists = TrackingOrders.objects.filter(created__gt=re_date).exclude(
        Q(status='Delivered') | Q(tracking_num=''))
    if lists:
        data = []
        for val in lists:
            try:
                headers = {
                    'User-Agent':
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
                }
                url = 'http://shipit-api.herokuapp.com/api/carriers/%s/%s'
                if len(val.tracking_num) > 12:
                    carrier = 'ups'
                else:
                    carrier = 'fedex'
                url = url % (carrier, val.tracking_num)
                res = requests.get(url, headers=headers)
                res = json.loads(res.content.decode())
                eta = False
                if 'eta' in res:
                    eta = datetime.datetime.strptime(res['eta'][0:19],
                                                     '%Y-%m-%dT%H:%M:%S')
                if 'activities' in res:
                    activities = res['activities']
                    if activities:
                        status = activities[0]['details']
                        first_scan_time = ''
                        delivery_time = ''
                        shipment_late = ''
                        delivery_late = ''
                        arrived_date_list = []
                        for v in activities:
                            if v['details'] == 'Arrived at FedEx location':
                                arrived_date_list.append(
                                    datetime.datetime.strptime(
                                        v['datetime'][0:10], '%Y-%m-%d'))
                            if v['details'] == 'Picked up' and carrier == 'fedex':
                                first_scan_time = datetime.datetime.strptime(
                                    v['datetime'], '%Y-%m-%dT%H:%M:%S')
                                first_scan_time = first_scan_time + datetime.timedelta(
                                    hours=-7)
                            if v['details'] == 'Delivered':
                                if carrier == 'ups':
                                    delivery_time = datetime.datetime.strptime(
                                        v['timestamp'][0:19],
                                        '%Y-%m-%dT%H:%M:%S')
                                elif carrier == 'fedex':
                                    delivery_time = datetime.datetime.strptime(
                                        v['datetime'], '%Y-%m-%dT%H:%M:%S')
                                delivery_time = delivery_time + datetime.timedelta(
                                    hours=-7)
                            if v['details'] == 'Origin scan' and carrier == 'ups':
                                first_scan_time = datetime.datetime.strptime(
                                    v['timestamp'][0:19], '%Y-%m-%dT%H:%M:%S')
                                first_scan_time = first_scan_time + datetime.timedelta(
                                    hours=-7)
                            if first_scan_time:
                                if val.latest_ship_date and len(
                                        val.latest_ship_date) >= 20:
                                    first_scan_time_str = int(
                                        time.mktime(
                                            first_scan_time.timetuple()))
                                    latest_ship_date_str = int(
                                        time.mktime(
                                            time.strptime(
                                                val.latest_ship_date[0:19],
                                                "%Y-%m-%dT%H:%M:%S")))
                                    shipment_late_c = first_scan_time_str - latest_ship_date_str
                                    if shipment_late_c > 0:
                                        shipment_late = 'Y'
                            if delivery_time:
                                if val.latest_delivery_date and len(
                                        val.latest_delivery_date) >= 20:
                                    delivery_time_str = int(
                                        time.mktime(delivery_time.timetuple()))
                                    latest_delivery_date_str = int(
                                        time.mktime(
                                            time.strptime(
                                                val.latest_delivery_date[0:19],
                                                "%Y-%m-%dT%H:%M:%S")))
                                    delivery_late_c = delivery_time_str - latest_delivery_date_str
                                    if delivery_late_c > 0:
                                        delivery_late = 'Y'
                        val.status = status
                        if first_scan_time:
                            val.first_scan_time = first_scan_time
                        if delivery_time:
                            val.delivery_time = delivery_time
                        if shipment_late:
                            val.shipment_late = shipment_late
                        if delivery_late:
                            val.delivery_late = delivery_late
                        val.save()
                        update_check = 0
                        update_status = '正常'
                        delivered_status = '正常'
                        if arrived_date_list:
                            arrived_date_list.sort()
                            arrived_date = arrived_date_list[0]
                            billing_date = str(val.billing_date)
                            billing_date = datetime.datetime.strptime(
                                billing_date, '%Y-%m-%d')
                            billing_weekday = billing_date.weekday()
                            update_check = (arrived_date - billing_date).days
                            if billing_weekday == 4 and update_check > 4:
                                update_status = '异常'
                            if billing_weekday != 4 and update_check > 2:
                                update_status = '异常'
                        check_delivered = eta and eta < datetime_now and val.status != 'Delivered'
                        if check_delivered:
                            delivered_status = '异常'
                        if (val.status == 'Order Processed: Ready for UPS' or
                                val.status == 'Order processed: ready for ups'
                                or val.status
                                == 'Shipment information sent to FedEx'
                            ) or update_check > 2 or check_delivered:
                            data.append({
                                'order_num':
                                val.order_num,
                                'tracking_num':
                                val.tracking_num,
                                'warehouse':
                                val.warehouse,
                                'account_num':
                                val.account_num,
                                'description':
                                val.description,
                                'status':
                                val.status,
                                'update_status':
                                update_status,
                                'delivered_status':
                                delivered_status,
                                'shipment_late':
                                val.shipment_late,
                                'delivery_late':
                                val.delivery_late,
                                'billing_date':
                                val.billing_date.strftime('%b.%d'),
                                'latest_ship_date':
                                val.latest_ship_date,
                                'latest_delivery_date':
                                val.latest_delivery_date,
                                'first_scan_time':
                                val.first_scan_time,
                                'delivery_time':
                                val.delivery_time
                            })
            except:
                log_obj = StockLogs()
                log_obj.id
                log_obj.user_id = 1
                log_obj.fun = 'tracking 自动更新状态'
                log_obj.description = 'Tracking number %s' % (val.tracking_num)
                log_obj.save()
                continue
        if data:
            fields = [
                '发货单时间', '账号', 'OrderNumber', 'WarehouseName', 'Description',
                'Tracking Numbers', 'latest-ship-date', 'latest-delivery-date',
                'Status', '动态更新', '预计送达', 'First Scan time', 'Delivery time',
                'Shipment Late', 'Delivery Late'
            ]
            data_fields = [
                'billing_date', 'account_num', 'order_num', 'warehouse',
                'description', 'tracking_num', 'latest_ship_date',
                'latest_delivery_date', 'status', 'update_status',
                'delivered_status', 'first_scan_time', 'delivery_time',
                'shipment_late', 'delivery_late'
            ]
            try:
                file_path = get_excel_file1(1, data, fields, data_fields)
                file_path = os.path.join(settings.BASE_DIR, file_path)
                subject = '报告邮件'
                text_content = '未扫描状态和异常的订单汇总。'
                html_content = '<p>这是一封<strong>未扫描状态和异常的订单汇总</strong>。</p>'
                from_email = settings.DEFAULT_FROM_EMAIL
                msg = EmailMultiAlternatives(subject, text_content, from_email,
                                             [
                                                 '*****@*****.**',
                                                 '*****@*****.**'
                                             ])
                msg.attach_alternative(html_content, "text/html")
                # 发送附件
                # text = open(file_path, 'rb').read()
                file_name = os.path.basename(file_path)
                # 对文件进行编码处理
                # b = make_header([(file_name, 'utf-8')]).encode('utf-8')
                # msg.attach(b, text)
                msg.attach_file(file_path)
                msg.send()
                os.remove(file_path)
            except Exception as e:
                log_obj = StockLogs()
                log_obj.id
                log_obj.user_id = 1
                log_obj.fun = 'tracking 邮件发送Error'
                log_obj.description = e
                log_obj.save()
    t.start()
    pass