def get_smt_order(): print "**START**", get_now() # 获取所有速卖有效通账号 channel_accounts = get_accounts('Smt') for account in channel_accounts: print "\n *Account: %s Start" % account.name # 如果帐号尝试失败, 则继续下一个帐号 success_num = 0 for i in range(10): try: aliexpress = Aliexpress( app_key=account.app_key, app_pwd=account.app_pwd, refresh_token=account.refresh_token, ) data = aliexpress.list_order(page=1, page_size=1) totalItem = data.get('totalItem') or '0' break except Exception, e: print ee(e) print "Failure %s , try numbers: %s" % (account.name, i + 1) else: print "**Failure %s" % account.name continue print "Total: ", totalItem # 如果没有订单, 则换帐号 if int(totalItem) == 0: print account.name, "has no order." time.sleep(2) continue # 因为目的只是获取订单号, 所以page_size设置的较大 page_size = 30 order_info = [] # pages向上取整, 使用(A+B-1)/B, range最右再加一页 pages = int((int(totalItem) + page_size - 1) / page_size) + 1 for i in range(1, pages): for k in range(3): try: data = aliexpress.list_order(page=i, page_size=page_size) break except Exception, e: print 'Get page timeout.Numbers:%s Page:%s ' % (k, i) # 订单的备注信息只在这里有, 在订单的详细信息中没有, 但是有的订单会没有memo字段, 因此使用字典的get方法 order_info += [(str(j['orderId']), j.get('memo', '')) for j in data['orderList']] time.sleep(1)
def get_wish_order(): print "**START**", get_now() channel_accounts = get_accounts('wish') for account in channel_accounts: print "\n *Account: %s Start" % account.name try: wish = Wish(client_id=account.client_id, client_secret=account.client_secret, refresh_token=account.refresh_token ) except Exception, e: print "Failure, account: ", account.name, get_now(), e continue #取最近2天 next_page = "First" since = (get_now() - datetime.timedelta(days=2)).isoformat()[:-6] while next_page: try: if next_page == "First": datas = wish.list_order(since, limit=5) else: datas = wish.list_order_by_next_page(next_page) next_page = datas.get('paging', {}).get('next', '') except Exception, e: print ee(e), get_now() break order_list = datas.get('data', []) #如果最后一页只有一个订单,数据类型会变成字典,需转换成列表 if type(order_list) == dict: order_list = [order_list] # pp(order_list) order_dicts = data_to_order_dicts(order_list, account) for ordernum, order_dict in order_dicts.iteritems(): r = import_order(order_dict) #每导入一个订单, 打印出订单信息 print "Success" if r['success'] else 'Failure',\ ordernum, get_now(), account.name, r['msg'].strip()
def post_shopify_no(): print "**START**", get_now() channel_accounts = get_accounts('shopify') for account in channel_accounts: print "\n *Account: %s Start" % account.name # 选出合适的package packages = Package.objects.filter(order__channel_id=account.id)\ .filter(shipping_id__isnull=False)\ .filter(possback_status=0)\ .exclude(status__in=[0, 4])\ .exclude(tracking_no='') for package in packages: data = get_package_data(package) shopify = Shopify(account.name, account.api_key, account.password) try: r = shopify.upload_tracking(**data).json() except Exception, e: print 'Failure', package.order.ordernum, get_now( ), account.name, e error_info = r.get('errors', {}).get('order', '') if r.get('fulfillment', {}).get('status', '') == 'success': print 'Success', package.order.ordernum, get_now( ), account.name elif 'is already fulfilled' in error_info: print 'Already', package.order.ordernum, get_now( ), account.name elif error_info: print 'Failure', package.order.ordernum, get_now( ), account.name, error_info else: print 'Failure', package.order.ordernum, get_now( ), account.name, 'need IT' time.sleep(5)
def get_amazon_order(): print '***START work get amazon order %s ***' % get_now() channel_accounts = get_accounts('amazon') for account in channel_accounts: print "\n *Account: %s Start %s" % (account.name, get_now()) #去除这个用户所在大洲的开发人员账号信息 developer = Accounts.developers[account.continent.title()] amazon = Orders(access_key=developer['access_key'], secret_key=developer['secret_key'], account_id=account.account_id, auth_token=account.auth_token, ) #尝试, 当无法获取的时候, 进行 for i in range(10): if amazon.get_service_status().parsed.get('Status') == 'GREEN': print "GREEN" break else: print "Failure number: %s" % i+1 time.sleep(60) else: time.sleep(1) print "Account:%s Failure" % account.name continue # # test start # #商品数量为0, 两件产品, 单件有运费,对多件有优惠 # ordernums = ['113-6935756-6939409','115-5224300-1466659', '107-8893319-1637852', '111-5685070-5646636'] # order_info = amazon.get_order(ordernums).parsed # next_token = order_info.get('NextToken', '') # order_list = order_info.get('Orders').get('Order') # print 33333 # break # # test end # 给next_token赋初值 next_token = 'First' now_days = (datetime.datetime.now()- datetime.timedelta(days=1)).strftime('%Y-%m-%d') while next_token: #每个帐号第一次获取order, 后面使用next_token进行订单信息的获取 if next_token == 'First': order_info = amazon.list_orders(marketplaceids=account.marketplaceids.split('#'), lastupdatedafter=now_days, max_results="50", orderstatus=('Unshipped','PartiallyShipped'), fulfillment_channels=('MFN',) ).parsed else: order_info = amazon.list_orders_by_next_token(token=next_token).parsed next_token = order_info.get('NextToken', '') try: order_list = order_info.get('Orders').get('Order') except Exception, e: print e break #获取订单的时候, 如果最后一页只剩下一个订单, 那么那个订单的数据结构会是dict而不是list, 转化一下 if type(order_list) == dict: order_list = [order_list] for order_info in order_list: ordernum = order_info.get('AmazonOrderId', '') try: order = Order.objects.get(ordernum=ordernum) continue except Exception, e: pass order_dict = get_amazon_order_dict(order_info, account, amazon) r = import_order(order_dict) #每导入一个订单, 打印出订单信息 print "Success" if r['success'] else 'Failure',\ ordernum, get_now(), account.name, r['msg'].strip() connection.close() time.sleep(60)
def post_smt_no(): print "*****Aliexpress START*****" channel_accounts = get_accounts('Smt') # todo return 'Test Ending' failure_ordernum = [] for account in channel_accounts: print "Account: %s Start" % account.name, get_now() #实例化, 因为部分帐号不一定能一次实例化成功 for i in range(15): try: aliexpress = Aliexpress( app_key=account.access_key, app_pwd=account.secret_key, refresh_token=account.auth_token, ) break except Exception, e: print ee(e) print "Failure %s , try numbers: %s" % (account.name, i+1) else: print "**Failure %s" % account.name continue # 选择合适的package回传 # 当前channel, 有shipping, 回传状态为0, 有运单号, 不是取消和未处理 packages = Package.objects.filter(order__channel_id=account.id)\ .filter(shipping_id__isnull=False)\ .filter(possback_status=0)\ .filter(order__order_from=account.name)\ .exclude(status__in=[0, 4])\ .exclude(tracking_no='') for package in packages: ordernum = package.order.ordernum service_name = get_service_name(package.shipping.label) no = package.tracking_no # 如果是顺丰俄罗斯, 那么需要回传sf_numbers if package.shipping.label == 'SFRU': no = package.sf_numbers send_type = get_sendType(package) for i in range(5): try: result = aliexpress.shipment(ordernum, service_name=service_name, no=no, send_type=send_type) break except: print 'Possback numbers:%s ordernum:%s' % (i, ordernum) else: print 'Possback failure. ordernum: %s' % ordernum failure_ordernum.append(ordernum) continue #更新成功 if 'success' in result: if send_type == 'all': #如果回传的状态是all, 那么就将这个order下的所有package的状态全部更新成回传完成 #因为之前发的包裹是待更新, 这次完成后, 之前的也全部完成 Package.objects.filter(order_id=package.order_id).update(possback_status=2) else: Package.objects.filter(id=package.id).update(possback_status=1) print 'Success', package.id, send_type, get_now(), package.tracking_no #对于已经上传的数据进行更新, 运单号校验失败, 因为运单号已经被手工回传过了 elif result.get('error_code', '') == '15-2001': Package.objects.filter(id=package.id).update(possback_status=2) print 'Update', package.id, send_type, get_now(), package.tracking_no #更新失败 else: print 'Failure', package.id, send_type, get_now(), package.tracking_no, result.get('error_code', 'Need Check')