def bulk_insert_N(session, cursor, count=1, step=10000, flag="a"): insert_sql = "INSERT INTO message (channel_id, origin_id, content, image_urls, receive_time, source)" \ "VALUES (%s, %s, %s, %s, %s, %s)" kw = { "shop_id": 1, "seller_id": "1", "seller_name": "1", "buyer_id": "1", "buyer_name": "1", "read_stat": 1, "deal_stat": 1, "open_date": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "close_date": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "closed_by": "1", "origin_id": "1", "relation_id": "1", "relation_type": "1", "last_msg_date": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "last_msg_id": "1", "last_msg_content": u"测试数据" } for i in range(1, count + 1): kw["seller_id"] = str(int(kw["seller_id"]) + i) channel = Channel.create(session, **kw) print "第 %d 个 %d 万" % (i, step / 10000) msgs = ((channel.id, str(1 + j) + flag, "asd", "test_data", datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "1") for j in xrange(1, step + 1)) cursor.executemany(insert_sql, msgs)
def bulk_insert(session, count=1, step=10000, flag="a"): kw = { "shop_id": 1, "seller_id": "1", "seller_name": "1", "buyer_id": "1", "buyer_name": "1", "read_stat": 1, "deal_stat": 1, "open_date": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "close_date": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "closed_by": "1", "origin_id": "1", "relation_id": "1", "relation_type": "1", "last_msg_date": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), "last_msg_id": "1", "last_msg_content": u"测试数据" } for i in range(1, count + 1): kw["seller_id"] = str(int(kw["seller_id"]) + i) channel = Channel.create(session, **kw) print "第 %d 个 %d 万" % (i, step / 10000) msgs = [ dict(channel_id=channel.id, origin_id=str(1 + j) + flag, content=u"测试数据", receive_time=datetime.datetime.now().strftime( "%Y-%m-%dT%H:%M:%S"), source="1") for j in xrange(1, step + 1) ] # session.bulk_insert_mappings(Message, msgs) # msgs = list() # for j in xrange(1, step+1): # m = Message() # m.channel_id = channel.id # m.origin_id=str(1+j) + flag # m.content=u"测试数据" # m.receive_time=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") # m.source="1" # msgs.append(m) db.execute(Message.__table__.insert(), msgs) # session.bulk_save_objects(msgs) session.commit()
def _get_messages_in_folder(self, folder_id, start, end, current_page=1, page_size=200): seller_id = self.shop.owner with sessionCM() as session: message_dict = dict() while True: logger.info("eBay-%d:正在同步第%d页的MessageList" % (self.shop.id, current_page)) print ("eBay-%d:正在同步第%d页的MessageList" % (self.shop.id, current_page)) result = self.msg_handler.get_my_messages( "ReturnHeaders", folder_id=folder_id, start_time=start, end_time=end, current_page=current_page, page_size=page_size) print result if not result["Messages"]: break message_list = result["Messages"]["Message"] if not isinstance(message_list, list): message_list = [message_list] for header in message_list: read_stat, deal_stat = True, True if header["Sender"] == seller_id: seller_name, buyer_id, buyer_name = seller_id, header["SendToName"], header["SendToName"] else: seller_name, buyer_id, buyer_name = seller_id, header["Sender"], header["Sender"] read_stat = header["Read"] == "true" # ?? deal_stat = header["Replied"] == "true" # ?? channel = Channel.find_by_channel_composition(session, seller_id, buyer_id, header.get("ItemID", 0)) # header中包含来自eBay的消息,该消息不一定拥有ItemID字段,取默认值0 receive_date = datetime.datetime.strptime( header["ReceiveDate"].rsplit(".", 1)[0], "%Y-%m-%dT%H:%M:%S") if channel: kwargs = dict() if receive_date > channel.last_msg_date: kwargs["last_msg_date"] = receive_date kwargs["last_msg_id"] = header["MessageID"] if receive_date < channel.open_date: kwargs["open_date"] = receive_date # 更新channel状态 # kwargs["read_stat"] = channel.read_stat and read_stat # kwargs["deal_stat"] = channel.deal_stat and deal_stat Channel.update(session, channel, **kwargs) else: channel = Channel.create( session, shop_id=self.shop.id, seller_id=seller_id, seller_name=seller_name, buyer_id=buyer_id, buyer_name=buyer_name, read_stat=read_stat, deal_stat=deal_stat, flag="rank0" if header["Flagged"] == "true" else "", msg_source="from ebay" if buyer_id == "eBay" else "from members", relation_id=header.get("ItemID", 0), relation_type="product", origin_id=0, open_date=receive_date, last_msg_date=receive_date, last_msg_id=header["MessageID"], last_msg_content="同步中..." ) product_url = "http://www.ebay.com/itm/%s" % header.get("ItemID", 0) # if not channel: # ??? Attachment.create( session, channel_id=channel.id, name=header.get("ItemTitle", ""), product_url=product_url, product_id=header.get("ItemID", "0"), image_url="", order_id="", order_url="" ) if not message_dict.get(channel.id): message_dict[channel.id] = list() message_dict[channel.id].append(header["MessageID"]) if len(message_list) < page_size: break current_page += 1 return message_dict