def post(self): try: self._event('drawing') begin_date = DateUtil.str_2_date(self.params.get('begin'), '-') end_date = DateUtil.str_2_date(self.params.get('end'), '-') print begin_date, end_date if not (begin_date and end_date): self.write(json_encode({})) return param = self.get_param(begin_date, end_date) self.write(json_encode(param)) except Exception as e: logger.error('[message: %s]' % e.message)
def _cpsuc_effect(self, order_list): for order in order_list: # 获取校验参数 if type(order) is dict and order.has_key( 'merchant') and order.has_key('orderId') and order.has_key( 'productList') and order.has_key('merchant'): exist_order = MongodbUtil.find_one('api', 'cps', spec_or_id={ 'orderId': order.get('orderId'), 'merchant': order.get('merchant') }) if exist_order: exist_order['status'] = 'successful' exist_order['successfulTime'] = DateUtil.get_sys_time() MongodbUtil.update( 'api', 'cps', spec_or_id={'_id': exist_order.get('_id')}, document=exist_order) continue else: continue order = self._fit_params(order) # 插入cps cps_id = MongodbUtil.insert('api', 'cps', order) if cps_id: self._event('cps_insert:%s' % cps_id)
def get_product_dict(product, keyword_list, merchant_id, tag_dict): """拼接产品参数""" start_time = DateUtil.get_sys_date() category_mode = MongodbUtil.find_one('shopping', 'category') category_list, category, name, description = category_mode.get('category_dict'), product.get(tag_dict['category']), product.get(tag_dict['name']), product.get(tag_dict['description']) # 商品匹配定类 category_result = fit_category(category_list, category, name, description) # 填充类别关键词集合 fill_category_dict(category_list, category_result, category, name, description) product_dict = { 'productId': product.get(tag_dict['id']), 'category': category_result, 'keywordList': keyword_list, 'merchantId': merchant_id, 'title': str(product.get(tag_dict['name'])).replace('&', ' '), 'startTime': start_time, 'aliveTime': settings.alive_time, 'description': str(product.get(tag_dict['description'])).replace('&', ''), 'currency': product.get(tag_dict['currency']), 'price': float(product.get(tag_dict['price'])), 'url': product.get(tag_dict['url']), 'mpn': product.get(tag_dict.get('mpn', ''), ''), 'color': product.get(tag_dict.get('color', ''), ''), 'size': product.get(tag_dict.get('size', ''), ''), 'merchantCategory': product.get(tag_dict.get('merchantCategory', ''), ''), 'availability': product.get(tag_dict.get('availability', ''), ''), 'shippingWeight': product.get(tag_dict.get('shippingWeight', ''), ''), 'gender': product.get(tag_dict.get('gender', ''), ''), 'ageGroup': product.get(tag_dict.get('ageGroup', ''), ''), } return product_dict
def clear_update_product(): try: logger.info('[TIME: %s] METHOD: clear_update_product' % DateUtil.get_sys_time()) clear_product() update_priority() except Exception as e: logger.error('Clear and update product error: %s' % e.message)
def clear_product(): """清理过期产品到备份集合中""" try: product_list = MongodbUtil.find('shopping', 'product') overdue_product_list = list() today = DateUtil.get_sys_date() for product in product_list.__iter__(): start_date = product.get('startTime') alive_time = product.get('aliveTime') end_date = DateUtil.get_end_date(start_date, alive_time) if today > end_date: #添加到备份产品集合 overdue_product_list.append(product) #执行删除操作 delete_product(product) save_overdue_product(overdue_product_list) except Exception as e: logger.error(e.message)
def _fit_params(self, order): """获取符合格式的参数""" cpc_id = self.get_cookie('c_i') if self.get_cookie('c_i') else '' order['orderTime'] = DateUtil.get_sys_time() order['status'] = 'pendding' order_total_price = order.get('orderTotalPrice') if order.get( 'orderTotalPrice') else 0.0 if not order_total_price: for product in order.get('productList'): order_total_price += product.get('totalPrice') order['orderTotalPrice'] = order_total_price if cpc_id: order['cpcId'] = ObjectId(cpc_id) return order
def save_overdue_cpc(overdue_product_id, product, cpc_list): """定期备份cpc跟踪点击数到overdueCpc集合中""" for cpc in cpc_list: overdue_cpc = { 'cpcOverdueProductId': overdue_product_id, 'webmaster': cpc.get('webmaster'), 'merchant': cpc.get('merchant'), 'adPositionId': cpc.get('adPositionId'), 'clickTime': cpc.get('clickTime'), 'clearTime': DateUtil.get_sys_time(), 'takeEffect': cpc.get('takeEffect'), } overdue_cpc_id = MongodbUtil.insert('shopping', 'overdueCpc', overdue_cpc) if overdue_cpc_id: logger.info( 'Insert overdue cpc from:%s to:%s successfully!!!' % (overdue_cpc.get('webmaster'), overdue_cpc.get('merchant')))
def emit(self, record): for toaddr in self.toaddrs: msg = MIMEMultipart() msg['From'] = self.fromaddr msg['To'] = toaddr msg['Subject'] = self.subject #添加邮件内容 message = 'ERROR %s:[%s]|%s: %s' % (DateUtil.get_sys_time('%Y-%m-%d %H:%M:%S'), record.filename, record.lineno, record.message) txt = MIMEText(message) msg.attach(txt) #发送邮件 smtp = smtplib.SMTP() smtp.connect(self.mailhost) smtp.login(self.username, self.password) smtp.sendmail(self.fromaddr, toaddr, msg.as_string()) smtp.quit()
def insert_cpc(self, user_id, language, title, webmaster, merchant, category, url, meta_info, duration): """插入新增点击 takeEffect: 1(successful) 0(failed)""" cpc_document = { 'uid': user_id, 'merchant': merchant, 'clickTime': DateUtil.get_sys_time(), 'url': url, 'takeEffect': 1, 'webmaster': webmaster, 'category': category, 'language': language, 'title': title, 'metaInfo': meta_info, 'duration': duration, } obj_id = MongodbUtil.insert('api', 'cpc', cpc_document) return obj_id
def save_product_img(product, tag_dict, size): """下载产品图片并存入mongodb中""" file_name = product.get(tag_dict['image']) opener = urllib2.build_opener() response = opener.open(file_name) file_body = response.read() file_name = UploadUtil.upload_pic(file_name, file_body, size[0], size[1]) image = { 'productId': product.get('_id'), 'version': DateUtil.get_sys_time(), 'fileName': file_name, 'width': size[0], 'height': size[1], } image_id = MongodbUtil.insert('shopping', 'image', image) if image_id: logger.info('Save product\'s image: %s successfully!!!' % product.get(tag_dict['image'])) return image_id else: logger.info('Save product\'s image: %s failed!!!' % product.get(tag_dict['image'])) return None
def _cps_effect(self, merchant, order_list): """广告主订单成功,在联盟插入cps数据,确认cps生效""" cookie_value = '' for order in order_list: # 获取校验参数 if type(order) is dict and order.has_key( 'merchant') and order.has_key('orderId') and order.has_key( 'productList') and order.has_key('merchant') and ( merchant == order.get('merchant')): exist_order = MongodbUtil.find_one('api', 'cps', spec_or_id={ 'orderId': order.get('orderId'), 'merchant': order.get('merchant') }) if exist_order: exist_order['status'] = 'successful' exist_order['successfulTime'] = DateUtil.get_sys_time() MongodbUtil.update( 'api', 'cps', spec_or_id={'_id': exist_order.get('_id')}, document=exist_order) continue else: continue order = self._fit_params(order) # 插入cps cps_id = MongodbUtil.insert('api', 'cps', order) if cps_id: self._event('cps_insert:%s' % cps_id) cookie_value += '_%s' % cps_id.__str__() if cookie_value: self.set_cookie('c_i_l', cookie_value, expires_days=settings.cookie_alive_time)
def _update_cps_status(self): """更新订单状态为成功""" self._event('update_status') cps_id_str = self.get_cookie('c_i_l') cps_id_list = cps_id_str.split('_') if cps_id_str else list() for cps_id in cps_id_list: if cps_id: cps_document = MongodbUtil.find_one( 'api', 'cps', spec_or_id={'_id': ObjectId(cps_id)}) if cps_document: cps_document['status'] = 'successful' cps_document['successfulTime'] = DateUtil.get_sys_time() MongodbUtil.update( 'api', 'cps', spec_or_id={'_id': cps_document.get('_id')}, document=cps_document) self.set_cookie('c_i_l', '', expires_days=settings.cookie_alive_time) result_info = { 'status': '200', 'message': 'successful', } self.write(json_encode(result_info))
#! -*- coding:utf-8 -*- """ @author:Conner @version:1.0 @date:13-12-5 @description:日期测试 """ import sys reload(sys) sys.setdefaultencoding('utf-8') from affiliate.lib.util.date_util import DateUtil if __name__ == '__main__': print(DateUtil.get_sys_date())
def get_session_id(cls): current_time = DateUtil.get_sys_time(format_str='%Y-%m-%d %H:%M:%S') session_id = hashlib.md5(current_time).hexdigest() return session_id
(overdue_cpc.get('webmaster'), overdue_cpc.get('merchant'))) def clear_product(): """清理过期产品到备份集合中""" try: product_list = MongodbUtil.find('shopping', 'product') overdue_product_list = list() today = DateUtil.get_sys_date() for product in product_list.__iter__(): start_date = product.get('startTime') alive_time = product.get('aliveTime') end_date = DateUtil.get_end_date(start_date, alive_time) if today > end_date: #添加到备份产品集合 overdue_product_list.append(product) #执行删除操作 delete_product(product) save_overdue_product(overdue_product_list) except Exception as e: logger.error(e.message) if __name__ == '__main__': today = DateUtil.get_sys_date() start_date = '2014-02-18' alive_time = 30 end_date = DateUtil.get_end_date(start_date, alive_time) print end_date print today > end_date