def send_today_in_history(user_id): u = User.get(user_id) if not u: return setting = u.get_profile_item("email_remind_today_in_history") if setting == 'N': print '---user %s does not like to receive remind mail' % u.id return email = u.get_email() if not email: print '---- user %s no email' % u.id return yesterday_ids = get_status_ids_yesterday(u.id, day=(datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")) status_of_yesterday = Status.gets(yesterday_ids) history_ids = get_status_ids_today_in_history(u.id, day=datetime.datetime.now().strftime("%Y-%m-%d")) status_of_today_in_history = Status.gets(history_ids) intros = [u.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) history_ids = get_status_ids_today_in_history(u.id, day=datetime.datetime.now().strftime("%Y-%m-%d")) d = {} for s in Status.gets(history_ids): t = s.create_time.strftime("%Y-%m-%d") if d.has_key(t): d[t].append(s) else: d[t] = [s] status_of_today_in_history = d from past.consts import YESTERDAY if not (status_of_yesterday or status_of_today_in_history): print '--- user %s has no status in history' % u.id return from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('past', 'templates')) env.filters['wrap_long_line'] = wrap_long_line env.filters['nl2br'] = filters.nl2br env.filters['clear_html_element'] = clear_html_element t = env.get_template('mail.html') m = t.module html = m.status_in_past(status_of_yesterday, status_of_today_in_history, YESTERDAY, config, intros) html = html.encode("utf8") subject = '''来自thepast.me的提醒 %s''' % datetime.datetime.now().strftime("%Y-%m-%d") text = '' print '--- send reminding to %s %s' %(user_id, email) send_mail(["%s" % email], "Today of The Past<*****@*****.**>", subject, text, html, files=[], server="localhost")
def user_past(uid): user = User.get(uid) if not user: abort(404, "no such user") r = check_access_user(user) if r: flash(r[1].decode("utf8"), "tip") return redirect("/") try: now = datetime.datetime.strptime(request.args.get("now"), "%Y-%m-%d") except: now = datetime.datetime.now() history_ids = get_status_ids_today_in_history(user.id, now) status_list = Status.gets(history_ids) if g.user and g.user.id == uid: pass elif g.user and g.user.id != uid: status_list = [ x for x in status_list if x.privacy() != consts.STATUS_PRIVACY_PRIVATE ] elif not g.user: status_list = [ x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC ] status_list = statuses_timelize(status_list) if g.user: sync_list = get_sync_list(g.user) else: sync_list = [] intros = [ user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values() ] intros = filter(None, intros) d = defaultdict(list) for x in status_list: t = x.create_time.strftime("%Y年%m月%d日") d[t].append(x) history_status = d return render_template("v2/user_past.html", **locals())
def past(): intros = [g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) yesterday_ids = get_status_ids_yesterday(g.user.id) status_of_yesterday = Status.gets(yesterday_ids) history_ids = get_status_ids_today_in_history(g.user.id) d = {} for s in Status.gets(history_ids): t = s.create_time.strftime("%Y-%m-%d") if d.has_key(t): d[t].append(s) else: d[t] = [s] status_of_today_in_history = d from past.consts import YESTERDAY return render_template("past.html", **locals())
def user_past(uid): user = User.get(uid) if not user: abort(404, "no such user") r = check_access_user(user) if r: flash(r[1].decode("utf8"), "tip") return redirect("/") try: now = datetime.datetime.strptime(request.args.get("now"), "%Y-%m-%d") except: now = datetime.datetime.now() history_ids = get_status_ids_today_in_history(user.id, now) status_list = Status.gets(history_ids) if g.user and g.user.id == uid: pass elif g.user and g.user.id != uid: status_list = [x for x in status_list if x.privacy() != consts.STATUS_PRIVACY_PRIVATE] elif not g.user: status_list = [x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC] status_list = statuses_timelize(status_list) if g.user: sync_list = get_sync_list(g.user) else: sync_list = [] intros = [user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) d = defaultdict(list) for x in status_list: t = x.create_time.strftime("%Y年%m月%d日") d[t].append(x) history_status = d return render_template("v2/user_past.html", **locals())
def past(): intros = [g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) try: now = datetime.datetime.strptime(request.args.get("now"), "%Y-%m-%d") except: now = datetime.datetime.now() history_ids = get_status_ids_today_in_history(g.user.id, now) status_list = Status.gets(history_ids) status_list = statuses_timelize(status_list) sync_list = get_sync_list(g.user) d = defaultdict(list) for x in status_list: t = x.create_time.strftime("%Y年%m月%d日") d[t].append(x) history_status = d return render_template("past.html", **locals())
def past(): intros = [ g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values() ] intros = filter(None, intros) now = datetime.datetime.now() yesterday_ids = get_status_ids_yesterday(g.user.id, now) status_of_yesterday = Status.gets(yesterday_ids) history_ids = get_status_ids_today_in_history(g.user.id, now) d = {} for s in Status.gets(history_ids): t = s.create_time.strftime("%Y-%m-%d") if d.has_key(t): d[t].append(s) else: d[t] = [s] status_of_today_in_history = d from past.consts import YESTERDAY return render_template("past.html", **locals())
def user_past(uid): user = User.get(uid) if not user: abort(404, "no such user") try: now = datetime.datetime.strptime(request.args.get("now"), "%Y-%m-%d") except: now = datetime.datetime.now() history_ids = get_status_ids_today_in_history(user.id, now) status_list = Status.gets(history_ids) status_list = statuses_timelize(status_list) if g.user: sync_list = get_sync_list(g.user) else: sync_list = [] d = defaultdict(list) for x in status_list: t = x.create_time.strftime("%Y年%m月%d日") d[t].append(x) history_status = d return render_template("v2/user_past.html", **locals())
def send_today_in_history(user_id, now=None, include_yestorday=False): if not now: now = datetime.datetime.now() u = User.get(user_id) if not u: return setting = u.get_profile_item("email_remind_today_in_history") if setting == 'N': print '---user %s does not like to receive remind mail' % u.id return email = u.get_email() if not email: print '---- user %s no email' % u.id return history_ids = get_status_ids_today_in_history(u.id, now) status_of_today_in_history = Status.gets(history_ids) if include_yestorday: yesterday_ids = get_status_ids_yesterday(u.id, now) status_of_yesterday = Status.gets(yesterday_ids) else: status_of_yesterday = None intros = [u.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) d = {} for s in Status.gets(history_ids): t = s.create_time.strftime("%Y-%m-%d") if d.has_key(t): d[t].append(s) else: d[t] = [s] status_of_today_in_history = d from past.consts import YESTERDAY if not (status_of_today_in_history or (include_yestorday and status_of_yesterday)): print '--- user %s has no status in history' % u.id return from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('past', 'templates')) env.filters['wrap_long_line'] = wrap_long_line env.filters['nl2br'] = filters.nl2br env.filters['clear_html_element'] = clear_html_element env.filters['isstr'] = lambda x: isinstance(x, basestring) t = env.get_template('mail.html') m = t.module if now: y = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d") else: y = YESTERDAY html = m.status_in_past(status_of_yesterday, status_of_today_in_history, y, config, intros) html = html.encode("utf8") subject = '''thepast.me|整理自己的故事 %s''' % now.strftime("%Y-%m-%d") text = '' print '--- send reminding to %s %s' %(user_id, email) send_mail(["%s" % email], "thepast<*****@*****.**>", subject, text, html)
def cmd_past(from_user, date_, msg_type="text"): thepast_id = UserWeixin.get_by_weixin(from_user).user_id now = datetime.datetime.now() date_ = date_ or now.strftime("%m-%d") date_ = "%s-%s" % (now.year + 1, date_) try: date_ = datetime.datetime.strptime(date_, "%Y-%m-%d") except: date_ = datetime.datetime(year=now.year + 1, month=now.month, day=now.day) history_ids = get_status_ids_today_in_history(thepast_id, date_) status_list = Status.gets(history_ids) status_list = [ x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC ] r = '' if msg_type == "text": for x in status_list: r += x.create_time.strftime( "%Y-%m-%d %H:%M") + "\n" + x.text + "\n~~~~~~~~~~\n" elif msg_type == "news": article_count = min(len(status_list) + 1, 9) r += "<ArticleCount>%s</ArticleCount>" % article_count r += "<Articles>" date_str = u"{m}月{d}日".format(m=date_.month, d=date_.day) title0 = u"{d},找到{l}条往事,点击看更多".format(d=date_str, l=len(status_list)) r += u''' <item> <Title><![CDATA[{title0}]]></Title> <Description><![CDATA[{desc0}]]></Description> <PicUrl><![CDATA[{picurl0}]]></PicUrl> <Url><![CDATA[{url0}]]></Url> </item> '''.format(title0=title0, desc0="", picurl0="", url0="http://thepast.me/laiwei") for i in range(1, article_count): item_xml = '<item>' s = status_list[i - 1] s_data = s.get_data() s_atts = s_data and s_data.get_attachments() or [] s_images = s_data and s_data.get_images() or [] s_re = s.get_retweeted_data() s_re_atts = s_re and s_re.get_attachments() or [] s_re_images = s_re and s_re.get_images() or [] s_re_user = s_re and s_re.get_user() or "" s_re_user_nickname = s_re_user if isinstance( s_re_user, basestring) else s_re_user.get_nickname() title = s.title desc = s.create_time.strftime("%Y-%m-%d %H:%M") desc += "\n" + s.summary for att in s_atts: desc += "\n" + att.get_href() desc += "\n" + att.get_description() if s_re_user_nickname and s_re.get_content(): desc += "\n//@" + s_re_user_nickname + ":" + s_re.get_content() for att in s_re_atts: desc += "\n" + att.get_href() desc += "\n" + att.get_description() s_images.extend(s_re_images) pic_url = "" if s_images: pic_url = s_images[0] s_from = s.get_origin_uri() url = s_from and s_from[1] or pic_url item_xml += "<Title><![CDATA[" + title + desc + "]]></Title>" item_xml += "<Description><![CDATA[" + title + desc + "]]></Description>" item_xml += "<PicUrl><![CDATA[" + pic_url + "]]></PicUrl>" item_xml += "<Url><![CDATA[" + url + "]]></Url>" item_xml += '</item>' r += item_xml r += "</Articles>" return r
def cmd_past(from_user, date_, msg_type="text"): thepast_id = UserWeixin.get_by_weixin(from_user).user_id now = datetime.datetime.now() date_ = date_ or now.strftime("%m-%d") date_ = "%s-%s" %(now.year + 1, date_) try: date_ = datetime.datetime.strptime(date_, "%Y-%m-%d") except: date_ = datetime.datetime(year=now.year+1, month=now.month, day=now.day) history_ids = get_status_ids_today_in_history(thepast_id, date_) status_list = Status.gets(history_ids) status_list = [x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC] r = '' if msg_type == "text": for x in status_list: r += x.create_time.strftime("%Y-%m-%d %H:%M") + "\n" + x.text + "\n~~~~~~~~~~\n" elif msg_type == "news": article_count = min(len(status_list)+1, 9) r += "<ArticleCount>%s</ArticleCount>" %article_count r += "<Articles>" date_str = u"{m}月{d}日".format(m=date_.month, d=date_.day) title0 = u"{d},找到{l}条往事,点击看更多".format(d=date_str, l=len(status_list)) r += u''' <item> <Title><![CDATA[{title0}]]></Title> <Description><![CDATA[{desc0}]]></Description> <PicUrl><![CDATA[{picurl0}]]></PicUrl> <Url><![CDATA[{url0}]]></Url> </item> '''.format(title0=title0, desc0="", picurl0="", url0="http://thepast.me/laiwei") for i in range(1, article_count): item_xml = '<item>' s = status_list[i-1] s_data = s.get_data() s_atts = s_data and s_data.get_attachments() or [] s_images = s_data and s_data.get_images() or [] s_re = s.get_retweeted_data() s_re_atts = s_re and s_re.get_attachments() or [] s_re_images = s_re and s_re.get_images() or [] s_re_user = s_re and s_re.get_user() or "" s_re_user_nickname = s_re_user if isinstance(s_re_user, basestring) else s_re_user.get_nickname() title = s.title desc = s.create_time.strftime("%Y-%m-%d %H:%M") desc += "\n" + s.summary for att in s_atts: desc += "\n" + att.get_href() desc += "\n" + att.get_description() if s_re_user_nickname and s_re.get_content(): desc += "\n//@" + s_re_user_nickname + ":" + s_re.get_content() for att in s_re_atts: desc += "\n" + att.get_href() desc += "\n" + att.get_description() s_images.extend(s_re_images) pic_url = "" if s_images: pic_url = s_images[0] s_from = s.get_origin_uri() url = s_from and s_from[1] or pic_url item_xml += "<Title><![CDATA[" +title+desc+ "]]></Title>" item_xml += "<Description><![CDATA["+ title + desc +"]]></Description>" item_xml += "<PicUrl><![CDATA[" +pic_url+ "]]></PicUrl>" item_xml += "<Url><![CDATA[" +url+ "]]></Url>" item_xml += '</item>' r += item_xml r += "</Articles>" return r
def send_today_in_history(user_id, now=None): if not now: now = datetime.datetime.now() u = User.get(user_id) if not u: return setting = u.get_profile_item("email_remind_today_in_history") if setting == 'N': print '---user %s does not like to receive remind mail' % u.id return email = u.get_email() if not email: print '---- user %s no email' % u.id return yesterday_ids = get_status_ids_yesterday(u.id, now) status_of_yesterday = Status.gets(yesterday_ids) history_ids = get_status_ids_today_in_history(u.id, now) status_of_today_in_history = Status.gets(history_ids) intros = [u.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) history_ids = get_status_ids_today_in_history(u.id, now) d = {} for s in Status.gets(history_ids): t = s.create_time.strftime("%Y-%m-%d") if d.has_key(t): d[t].append(s) else: d[t] = [s] status_of_today_in_history = d from past.consts import YESTERDAY if not (status_of_yesterday or status_of_today_in_history): print '--- user %s has no status in history' % u.id return from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('past', 'templates')) env.filters['wrap_long_line'] = wrap_long_line env.filters['nl2br'] = filters.nl2br env.filters['clear_html_element'] = clear_html_element t = env.get_template('mail.html') m = t.module if now: y = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d") else: y = YESTERDAY html = m.status_in_past(status_of_yesterday, status_of_today_in_history, y, config, intros) html = html.encode("utf8") subject = '''来自thepast.me的提醒 %s''' % now.strftime("%Y-%m-%d") text = '' print '--- send reminding to %s %s' %(user_id, email) send_mail(["%s" % email], "Today of The Past<*****@*****.**>", subject, text, html, files=[], server="localhost")