def find_new_feed(): email_log.info("开始发送订阅结果邮件...") sended_num = 0 failed_num = 0 success_num = 0 no_pkg_nums = 0 freq_nums = 0 no_feed_nums = 0 try: send_deny_list = [] send_deny_set = set() toal_username_set = set() user_feed_map = {} usernames1 = set() usernames2 = set(UserFeed2.objects.filter(is_deleted=False).distinct('username')) charge_pkgs = UserChargePackage.objects.filter(feed_end_time__gte=datetime.datetime.now()) for charge_pkg in charge_pkgs: try: usernames1.add(charge_pkg.user.username) except: pass usernames3 = usernames1 & usernames2 usernames4 = list(usernames3) userFeeds = UserFeed2.objects.filter(is_deleted=False,username__in=usernames4) for userFeed in userFeeds: username = userFeed.username if isinstance(username, unicode): username = username.encode('utf-8') if username == '': continue if username in send_deny_list: continue if username in user_feed_map: user_feed_map[username].append(userFeed) continue toal_username_set.add(username) userSendInfos = EmailSendInfo.objects.filter(username=username) if len(userSendInfos) == 0: feed_list = [] feed_list.append(userFeed) user_feed_map[username] = feed_list else: userSendInfo = userSendInfos[0] if userSendInfo.sendFrequency == 0: no_feed_nums += 1 send_deny_set.add(username) send_deny_list.append(username) else: lastSendTime = userSendInfo.lastSendDate now = datetime.datetime.now() hours = ((now - lastSendTime).days * 24 * 3600 + (now - lastSendTime).seconds) / 3600 frequencyHours = (userSendInfo.sendFrequency) * 24 - 24 if hours >= frequencyHours : feed_list = [] feed_list.append(userFeed) user_feed_map[username] = feed_list else: freq_nums += 1 send_deny_set.add(username) send_deny_list.append(username) for username, feed_list in user_feed_map.iteritems(): if username == '': continue email_log.info("开始发送: " + username) sended_num = sended_num + 1 total_resume = 0 feed_info_list = [] for userFeed in feed_list: token = username + "&&&" + str(userFeed.feed.id) token = base64.b64encode(token) result_dict = get_feed_result(userFeed) total_resume += result_dict['feed_result_num'] result_dict['keywords'] = userFeed.feed.keywords result_dict['talent_level'] = userFeed.feed.talent_level result_dict['id'] = userFeed.feed.id result_dict['token'] = token if result_dict['feed_result_num'] > 0: feed_info_list.append(result_dict) try: if total_resume > 0: result, info = feed_find_email(username, feed_info_list, total_resume) time.sleep(15) # 限制发送频率防止发送过快被服务商屏蔽 success_num = success_num + 1 email_log.info("发送成功! ") emailSendInfos = EmailSendInfo.objects.filter(username=username) if len(emailSendInfos) == 0: emailSendInfo = EmailSendInfo(username=username, lastSendDate=datetime.datetime.now(), send_status=str(info)) else: emailSendInfo = emailSendInfos[0] emailSendInfo.send_status = str(info) emailSendInfo.lastSendDate = datetime.datetime.now() emailSendInfo.save() except Exception , IntegrityError: failed_num = failed_num + 1 email_log.info("发送失败! ") email_log.info(str(IntegrityError)) except Exception , IntegrityError: email_log.info(str(IntegrityError))
def find_new_feed(): email_log.info("开始发送订阅结果邮件...") sended_num = 0 failed_num = 0 success_num = 0 no_pkg_nums = 0 freq_nums = 0 no_feed_nums = 0 try: send_deny_list = [] send_deny_set = set() toal_username_set = set() user_feed_map = {} usernames1 = set() usernames2 = set( UserFeed2.objects.filter(is_deleted=False).distinct('username')) charge_pkgs = UserChargePackage.objects.filter( feed_end_time__gte=datetime.datetime.now()) for charge_pkg in charge_pkgs: try: usernames1.add(charge_pkg.user.username) except: pass usernames3 = usernames1 & usernames2 usernames4 = list(usernames3) userFeeds = UserFeed2.objects.filter(is_deleted=False, username__in=usernames4) for userFeed in userFeeds: username = userFeed.username if isinstance(username, unicode): username = username.encode('utf-8') if username == '': continue if username in send_deny_list: continue if username in user_feed_map: user_feed_map[username].append(userFeed) continue toal_username_set.add(username) userSendInfos = EmailSendInfo.objects.filter(username=username) if len(userSendInfos) == 0: feed_list = [] feed_list.append(userFeed) user_feed_map[username] = feed_list else: userSendInfo = userSendInfos[0] if userSendInfo.sendFrequency == 0: no_feed_nums += 1 send_deny_set.add(username) send_deny_list.append(username) else: lastSendTime = userSendInfo.lastSendDate now = datetime.datetime.now() hours = ((now - lastSendTime).days * 24 * 3600 + (now - lastSendTime).seconds) / 3600 frequencyHours = (userSendInfo.sendFrequency) * 24 - 24 if hours >= frequencyHours: feed_list = [] feed_list.append(userFeed) user_feed_map[username] = feed_list else: freq_nums += 1 send_deny_set.add(username) send_deny_list.append(username) for username, feed_list in user_feed_map.iteritems(): if username == '': continue email_log.info("开始发送: " + username) sended_num = sended_num + 1 total_resume = 0 feed_info_list = [] for userFeed in feed_list: token = username + "&&&" + str(userFeed.feed.id) token = base64.b64encode(token) result_dict = get_feed_result(userFeed) total_resume += result_dict['feed_result_num'] result_dict['keywords'] = userFeed.feed.keywords result_dict['talent_level'] = userFeed.feed.talent_level result_dict['id'] = userFeed.feed.id result_dict['token'] = token if result_dict['feed_result_num'] > 0: feed_info_list.append(result_dict) try: if total_resume > 0: result, info = feed_find_email(username, feed_info_list, total_resume) time.sleep(15) # 限制发送频率防止发送过快被服务商屏蔽 success_num = success_num + 1 email_log.info("发送成功! ") emailSendInfos = EmailSendInfo.objects.filter( username=username) if len(emailSendInfos) == 0: emailSendInfo = EmailSendInfo( username=username, lastSendDate=datetime.datetime.now(), send_status=str(info)) else: emailSendInfo = emailSendInfos[0] emailSendInfo.send_status = str(info) emailSendInfo.lastSendDate = datetime.datetime.now() emailSendInfo.save() except Exception, IntegrityError: failed_num = failed_num + 1 email_log.info("发送失败! ") email_log.info(str(IntegrityError)) except Exception, IntegrityError: email_log.info(str(IntegrityError))
def get_email_data(pub_feed): after_tomorrow = get_tomommow() + datetime.timedelta(days=1) username = pub_feed.email last_send_info = EmailSendInfo.objects(username=username).first() if last_send_info: send_frequency = last_send_info.sendFrequency last_send_date = last_send_info.lastSendDate need_send = ( send_frequency and last_send_date + datetime.timedelta(days=send_frequency) <= after_tomorrow ) else: user = User.objects.filter(username=username)[0] last_send_info = EmailSendInfo( user=user.id, username=username, ) need_send = True if not need_send: return False user_pub_feeds = EmailFeedData.objects( email=username, is_send=False, ).select_related() if not user_pub_feeds: return False feed_info_list = [] total_num = 0 keyword_list = [] for pub_feed in user_pub_feeds: feed_id = pub_feed.feed.id feed = Feed2.objects(id=feed_id, deleted=False).first() if not feed: continue keywords = feed.title if feed.title else feed.keywords keyword_list.append(keywords.replace(u',', ',')) resume_num = len(pub_feed.resumes) feed.feed_resumes = get_display_resumes(feed_id, pub_feed.resumes) feed.feed_result_num = resume_num feed.token = get_email_token(username, feed_id) total_num += resume_num feed_info_list.append(feed) keyword_list = ','.join(keyword_list).split(',') if len(keyword_list) > 5: keyword_list[4] = '...' keywords = ','.join(keyword_list[:5]) subject = u'聘宝为你推荐人才-%s' % keywords return { 'username': username, 'subject': subject, 'feed_info_list': feed_info_list, 'total_num': total_num, 'email_send_info': last_send_info, }