Esempio n. 1
0
def daily(hours):
    now = time.time()
    offset = 0
    post_ids_to_email = set()
    while True:
        index_posts = conn.query(
            "SELECT * FROM index_posts ORDER BY rank DESC LIMIT %s, 100",
            offset)

        if len(index_posts) == 0:
            break

        post_ids = [post["entity_id"] for post in index_posts]
        for post_id, post in nomagic._get_entities_by_ids(post_ids):
            period = (now - time.mktime(
                datetime.datetime.strptime(
                    post["datetime"],
                    "%Y-%m-%dT%H:%M:%S.%f").timetuple())) / 3600

            if period <= hours:
                post_ids_to_email.add(post_id)

        offset += 100

    posts_to_email = nomagic._get_entities_by_ids(post_ids_to_email)
    loader = tornado.template.Loader(
        os.path.dirname(os.path.abspath(__file__)) + "/../template/")

    locale = tornado.locale.get()
    msg = amazon_ses.EmailMessage()
    msg.subject = locale.translate('Pythonic Info Daily').encode("utf-8")
    msg.bodyHtml = loader.load("email_daily.html").generate(
        posts=posts_to_email, _=locale.translate)

    users = []
    users_not_to_send = []
    users_exists = conn.query("SELECT * FROM index_login")
    for user_id, user in nomagic._get_entities_by_ids(
        [user_exists["entity_id"] for user_exists in users_exists]):
        if user.get("receive_daily_email", True):
            users.append(user)
        else:
            users_not_to_send.append(user)

    users_invited = conn.query("SELECT * FROM invite")

    sender = amazon_ses.AmazonSES(settings["AmazonAccessKeyID"],
                                  settings["AmazonSecretAccessKey"])
    emails = set([user["login"] for user in users_exists] +
                 [user["email"] for user in users_invited]) - set(
                     [user["email"] for user in users_not_to_send])
    for email in emails:
        if "@" in email:
            print email
            sender.sendEmail(settings["email_sender"], email, msg)
Esempio n. 2
0
    def get(self):
        self.user_id = ""
        if self.current_user:
            self.user_id = self.current_user.get("user_id", u"").encode("utf8")

        self.from_id = self.get_argument("from", None)
        feeds = nomagic.feeds.get_public_feed(item_start_id = self.from_id)
        self.users = dict(nomagic._get_entities_by_ids(set([i["user_id"] for i in feeds])))

        self.more_id = None
        html_topics = []
        for topic in feeds:
            topic["like_count"] = len(topic.get("likes", []))
            topic["like"] = self.user_id in set(topic.get("likes", [])) if self.current_user else False
            topic["comment_count"] = 0
            topic["user"] = self.users[topic["user_id"]]
            topic["url"] = topic.get("url_cn") if topic.get("url_cn") else topic.get("url_en")
            topic["handler"] = self
            topic["_"] = self.locale.translate
            self.more_id = topic["id"]

            html_topics.append(self.topic_temp.generate(**topic))

        self.set_header("Cache-Control", "max-age=900")
        self.render("../template/html_feed.html", topics=html_topics)
Esempio n. 3
0
    def get(self):
        activity_id = self.get_argument("id")
        #user_id = self.current_user["user_id"].encode("utf8")
        news_feed, user_ids = nomagic.feeds.get_news_by_id(activity_id)
        users = dict(nomagic._get_entities_by_ids(user_ids))

        self.finish({"users": users, "news_feeds": [news_feed]})
Esempio n. 4
0
def ranking():
    now = time.time()
    offset = 0
    while True:
        index_posts = conn.query(
            "SELECT * FROM index_posts ORDER BY rank DESC LIMIT %s, 100",
            offset)

        if len(index_posts) == 0:
            break

        post_ids = [post["entity_id"] for post in index_posts]
        for post_id, post in nomagic._get_entities_by_ids(post_ids):
            period = (now - time.mktime(
                datetime.datetime.strptime(
                    post["datetime"],
                    "%Y-%m-%dT%H:%M:%S.%f").timetuple())) / 3600
            points = len(post["likes"])
            post_rank = rank(points, period)

            conn.execute(
                "UPDATE index_posts SET rank = %s WHERE entity_id = %s",
                post_rank, post_id)

        offset += 100
Esempio n. 5
0
    def get(self):
        self.user_id = ""
        if self.current_user:
            self.user_id = self.current_user.get("user_id", u"").encode("utf8")

        self.from_id = self.get_argument("from", None)
        feeds = nomagic.feeds.get_public_feed(item_start_id=self.from_id)
        self.users = dict(
            nomagic._get_entities_by_ids(set([i["user_id"] for i in feeds])))

        self.more_id = None
        html_topics = []
        for topic in feeds:
            topic["like_count"] = len(topic.get("likes", []))
            topic["like"] = self.user_id in set(topic.get(
                "likes", [])) if self.current_user else False
            topic["comment_count"] = 0
            topic["user"] = self.users[topic["user_id"]]
            topic["url"] = topic.get("url_cn") if topic.get(
                "url_cn") else topic.get("url_en")
            topic["handler"] = self
            topic["_"] = self.locale.translate
            self.more_id = topic["id"]

            html_topics.append(self.topic_temp.generate(**topic))

        self.set_header("Cache-Control", "max-age=900")
        self.render("../template/html_feed.html", topics=html_topics)
Esempio n. 6
0
    def get(self):
        activity_id = self.get_argument("id")
        #user_id = self.current_user["user_id"].encode("utf8")
        news_feed, user_ids = nomagic.feeds.get_news_by_id(activity_id)
        users = dict(nomagic._get_entities_by_ids(user_ids))

        self.finish({"users": users,
                     "news_feeds": [news_feed]})
Esempio n. 7
0
    def get(self):
        if not self.current_user:
            #raise tornado.web.HTTPError(401, "User not login")
            news_feeds = nomagic.feeds.get_public_news_feed()
            users = dict(nomagic._get_entities_by_ids(set([i["user_id"] for i in news_feeds])))

            self.finish({"users": users,
                        "news_feeds": news_feeds})

            return

        user_id = self.current_user["user_id"].encode("utf8")
        news_feeds = nomagic.feeds.get_news_feed_by_user_id(user_id)
        users = dict(nomagic._get_entities_by_ids(set([i["user_id"] for i in news_feeds]+[user_id])))

        self.finish({"user_id": user_id,
                     "users": users,
                     "news_feeds": news_feeds})
Esempio n. 8
0
def daily(hours):
    now = time.time()
    offset = 0
    post_ids_to_email = set()
    while True:
        index_posts = conn.query("SELECT * FROM index_posts ORDER BY rank DESC LIMIT %s, 100", offset)

        if len(index_posts) == 0:
            break

        post_ids = [post["entity_id"] for post in index_posts]
        for post_id, post in nomagic._get_entities_by_ids(post_ids):
            period = (now - time.mktime(datetime.datetime.strptime(post["datetime"], "%Y-%m-%dT%H:%M:%S.%f").timetuple())) / 3600

            if period <= hours:
                post_ids_to_email.add(post_id)

        offset += 100

    posts_to_email = nomagic._get_entities_by_ids(post_ids_to_email)
    loader = tornado.template.Loader(os.path.dirname(os.path.abspath(__file__)) + "/../template/")

    locale = tornado.locale.get()
    msg = amazon_ses.EmailMessage()
    msg.subject = locale.translate('Pythonic Info Daily').encode("utf-8")
    msg.bodyHtml = loader.load("email_daily.html").generate(posts=posts_to_email, _=locale.translate)

    users = []
    users_not_to_send = []
    users_exists = conn.query("SELECT * FROM index_login")
    for user_id, user in nomagic._get_entities_by_ids([user_exists["entity_id"] for user_exists in users_exists]):
        if user.get("receive_daily_email", True):
            users.append(user)
        else:
            users_not_to_send.append(user)

    users_invited = conn.query("SELECT * FROM invite")

    sender = amazon_ses.AmazonSES(settings["AmazonAccessKeyID"], settings["AmazonSecretAccessKey"])
    emails = set([user["login"] for user in users_exists] + [user["email"] for user in users_invited]) - set([user["email"] for user in users_not_to_send])
    for email in emails:
        if "@" in email:
            print email
            sender.sendEmail(settings["email_sender"], email, msg)
Esempio n. 9
0
def get_friends(user_ids):
    result = []
    for friend_id, friend in nomagic._get_entities_by_ids(user_ids):
        if "password" in friend:
            del friend["password"]
        if "salt" in friend:
            del friend["salt"]
        result.append(dict(friend, user_id=friend_id))

    return result
Esempio n. 10
0
def get_friends(user_ids):
    result = []
    for friend_id, friend in nomagic._get_entities_by_ids(user_ids):
        if "password" in friend:
            del friend["password"]
        if "salt" in friend:
            del friend["salt"]
        result.append(dict(friend, user_id=friend_id))

    return result
Esempio n. 11
0
    def get(self):
        self.set_header("Cache-Control", "max-age=0")
        if not self.current_user:
            raise tornado.web.HTTPError(401, "User not login")
            return

        from_id = self.get_argument("from", None)
        feeds = nomagic.feeds.get_public_feed(item_start_id = from_id)
        users = dict(nomagic._get_entities_by_ids(set([i["user_id"] for i in feeds])))

        self.finish({"users": users, "feeds": feeds})
Esempio n. 12
0
    def get(self):
        self.set_header("Cache-Control", "max-age=0")
        if not self.current_user:
            raise tornado.web.HTTPError(401, "User not login")
            return

        entity_id = self.get_argument("id")
        #user_id = self.current_user["user_id"].encode("utf8")
        item, user_ids = nomagic.feeds.get_item_by_id(entity_id)
        item["comments"] = self.get_comments(item["comments"])
        item["content"] = markdown2.markdown(item["content"], safe_mode=True)
        users = dict(nomagic._get_entities_by_ids(user_ids))

        self.finish({"users": users, "item": item})
Esempio n. 13
0
    def get(self):
        if not self.current_user:
            #raise tornado.web.HTTPError(401, "User not login")
            news_feeds = nomagic.feeds.get_public_news_feed()
            users = dict(
                nomagic._get_entities_by_ids(
                    set([i["user_id"] for i in news_feeds])))

            self.finish({"users": users, "news_feeds": news_feeds})

            return

        user_id = self.current_user["user_id"].encode("utf8")
        news_feeds = nomagic.feeds.get_news_feed_by_user_id(user_id)
        users = dict(
            nomagic._get_entities_by_ids(
                set([i["user_id"] for i in news_feeds] + [user_id])))

        self.finish({
            "user_id": user_id,
            "users": users,
            "news_feeds": news_feeds
        })
Esempio n. 14
0
def disconnect_entities(entity_id1, entity_connection_name1, entity_id2, entity_connection_name2):
    entity1, entity2 = nomagic._get_entities_by_ids([entity_id1, entity_id2])
    entity_data1, entity_data2 = entity1[1], entity2[1]

    entity_connection = set(entity_data1.get(entity_connection_name1, []))
    if entity_id2 in entity_connection:
        entity_connection.remove(entity_id2)
        entity_data1[entity_connection_name1] = list(entity_connection)
        nomagic._update_entity_by_id(entity_id1, entity_data1)

    entity_connection = set(entity_data2.get(entity_connection_name2, []))
    if entity_id1 in entity_connection:
        entity_connection.remove(entity_id1)
        entity_data2[entity_connection_name2] = list(entity_connection)
        nomagic._update_entity_by_id(entity_id2, entity_data2)
Esempio n. 15
0
def get_comments(entity, user_ids = set()):
    assert "comment_ids" in entity
    assert "comments" not in entity
    entity_comments = entity.get("comments", [])
    for comment_id, comment in nomagic._get_entities_by_ids(entity.get("comment_ids", [])):
        comment["id"] = comment_id
        comment["like_count"] = len(comment.get("likes", [])),
        comment["comment_count"] = 0, #len(activity.get("comment_ids", [])),
        user_ids.add(comment["user_id"])
        if comment.get("comment_ids"):
            get_comments(comment, user_ids)
        entity_comments.append(comment)

    entity["comments"] = entity_comments
    return entity_comments, user_ids
Esempio n. 16
0
def get_public_news_feed(activity_offset=10, activity_start_id=None):
    if activity_start_id:
        pass
    else:
        posts = conn.query("SELECT * FROM index_posts ORDER BY id DESC LIMIT 0, %s", activity_offset)
    activity_ids = [i["entity_id"] for i in posts]

    if posts:
        activities = [dict(activity, id=activity_id)
                        for activity_id, activity in nomagic._get_entities_by_ids(activity_ids)]
        return [dict(activity,
                     like_count = len(activity.get("likes", [])),
                     comment_count = len(activity.get("comment_ids", [])),
                     ) for activity in activities]
    return []
Esempio n. 17
0
    def get_news_by_id(self, activity_id):
        activity = nomagic._get_entity_by_id(activity_id)
        activity["id"] = activity_id
        comments, user_ids = nomagic.feeds.get_comments(activity)
        self.users = dict(nomagic._get_entities_by_ids(user_ids))

        data = dict(activity,
                    id = activity_id,
                    like_count = len(activity.get("likes", [])),
                    like = False,
                    user = self.users[activity["user_id"]],
                    comment_count = 0, #len(activity.get("comment_ids", [])),
                    comments = self.get_comments(comments))

        return self.topic_temp.generate(**data)
Esempio n. 18
0
def get_comments(entity, user_ids=set()):
    assert "comment_ids" in entity
    assert "comments" not in entity
    entity_comments = entity.get("comments", [])
    for comment_id, comment in nomagic._get_entities_by_ids(
            entity.get("comment_ids", [])):
        comment["id"] = comment_id
        comment["like_count"] = len(comment.get("likes", [])),
        comment["comment_count"] = 0,  #len(activity.get("comment_ids", [])),
        user_ids.add(comment["user_id"])
        if comment.get("comment_ids"):
            get_comments(comment, user_ids)
        entity_comments.append(comment)

    entity["comments"] = entity_comments
    return entity_comments, user_ids
Esempio n. 19
0
def disconnect_entities(entity_id1, entity_connection_name1, entity_id2,
                        entity_connection_name2):
    entity1, entity2 = nomagic._get_entities_by_ids([entity_id1, entity_id2])
    entity_data1, entity_data2 = entity1[1], entity2[1]

    entity_connection = set(entity_data1.get(entity_connection_name1, []))
    if entity_id2 in entity_connection:
        entity_connection.remove(entity_id2)
        entity_data1[entity_connection_name1] = list(entity_connection)
        nomagic._update_entity_by_id(entity_id1, entity_data1)

    entity_connection = set(entity_data2.get(entity_connection_name2, []))
    if entity_id1 in entity_connection:
        entity_connection.remove(entity_id1)
        entity_data2[entity_connection_name2] = list(entity_connection)
        nomagic._update_entity_by_id(entity_id2, entity_data2)
Esempio n. 20
0
    def get_news_by_id(self, activity_id):
        activity = nomagic._get_entity_by_id(activity_id)
        activity["id"] = activity_id
        comments, user_ids = nomagic.feeds.get_comments(activity)
        self.users = dict(nomagic._get_entities_by_ids(user_ids))

        data = dict(
            activity,
            id=activity_id,
            like_count=len(activity.get("likes", [])),
            like=False,
            user=self.users[activity["user_id"]],
            comment_count=0,  #len(activity.get("comment_ids", [])),
            comments=self.get_comments(comments))

        return self.topic_temp.generate(**data)
Esempio n. 21
0
def ranking():
    now = time.time()
    offset = 0
    while True:
        index_posts = conn.query("SELECT * FROM index_posts ORDER BY rank DESC LIMIT %s, 100", offset)

        if len(index_posts) == 0:
            break

        post_ids = [post["entity_id"] for post in index_posts]
        for post_id, post in nomagic._get_entities_by_ids(post_ids):
            period = (now - time.mktime(datetime.datetime.strptime(post["datetime"], "%Y-%m-%dT%H:%M:%S.%f").timetuple())) / 3600
            points = len(post["likes"])
            post_rank = rank(points, period)

            conn.execute("UPDATE index_posts SET rank = %s WHERE entity_id = %s", post_rank, post_id)

        offset += 100
Esempio n. 22
0
    def get(self):
        if not self.current_user:
            news_feeds = nomagic.feeds.get_public_news_feed()
            self.users = dict(nomagic._get_entities_by_ids(set([i["user_id"] for i in news_feeds])))

            html_topics = []
            for topic in news_feeds:
                topic["like_count"] = len(topic.get("likes", []))
                topic["like"] = False
                topic["comment_count"] = 0
                topic["user"] = self.users[topic["user_id"]]

                html_topics.append(self.topic_temp.generate(**topic))

            self.render("../template/html_feed.html", topics=html_topics)
            return

        self.user_id = self.current_user.get("user_id", u"").encode("utf8")
        self.render("../template/feed.html")
Esempio n. 23
0
    def get(self):
        if not self.current_user:
            news_feeds = nomagic.feeds.get_public_news_feed()
            self.users = dict(
                nomagic._get_entities_by_ids(
                    set([i["user_id"] for i in news_feeds])))

            html_topics = []
            for topic in news_feeds:
                topic["like_count"] = len(topic.get("likes", []))
                topic["like"] = False
                topic["comment_count"] = 0
                topic["user"] = self.users[topic["user_id"]]

                html_topics.append(self.topic_temp.generate(**topic))

            self.render("../template/html_feed.html", topics=html_topics)
            return

        self.user_id = self.current_user.get("user_id", u"").encode("utf8")
        self.render("../template/feed.html")
Esempio n. 24
0
    def get_item_by_id(self, item_id):
        item = nomagic._get_entity_by_id(item_id)
        comments, user_ids = nomagic.feeds.get_comments(item)
        user_ids.add(item["user_id"])
        self.users = dict(nomagic._get_entities_by_ids(user_ids))
        url = item.get("url_cn") if item.get("url_cn") else item.get("url_en")
        #url = url if url else "/item?id=%s" % item.get("id")

        data = dict(item,
                    id = item_id,
                    like_count = len(item.get("likes", [])),
                    like = self.user_id in set(item.get("likes", [])) if self.current_user else False,
                    user = self.users[item["user_id"]],
                    comment_count = 0, #len(item.get("comment_ids", [])),
                    comments = self.get_comments(comments),
                    url = url,
                    content = markdown2.markdown(item["content"], safe_mode=True),
                    _ = self.locale.translate,
                    handler = self)

        return self.topic_temp.generate(**data)
Esempio n. 25
0
def get_public_news_feed(activity_offset=10, activity_start_id=None):
    if activity_start_id:
        pass
    else:
        posts = conn.query(
            "SELECT * FROM index_posts ORDER BY id DESC LIMIT 0, %s",
            activity_offset)
    activity_ids = [i["entity_id"] for i in posts]

    if posts:
        activities = [
            dict(activity, id=activity_id) for activity_id, activity in
            nomagic._get_entities_by_ids(activity_ids)
        ]
        return [
            dict(
                activity,
                like_count=len(activity.get("likes", [])),
                comment_count=len(activity.get("comment_ids", [])),
            ) for activity in activities
        ]
    return []
Esempio n. 26
0
    def get_item_by_id(self, item_id):
        item = nomagic._get_entity_by_id(item_id)
        comments, user_ids = nomagic.feeds.get_comments(item)
        user_ids.add(item["user_id"])
        self.users = dict(nomagic._get_entities_by_ids(user_ids))
        url = item.get("url_cn") if item.get("url_cn") else item.get("url_en")
        #url = url if url else "/item?id=%s" % item.get("id")

        data = dict(
            item,
            id=item_id,
            like_count=len(item.get("likes", [])),
            like=self.user_id in set(item.get("likes", []))
            if self.current_user else False,
            user=self.users[item["user_id"]],
            comment_count=0,  #len(item.get("comment_ids", [])),
            comments=self.get_comments(comments),
            url=url,
            content=markdown2.markdown(item["content"], safe_mode=True),
            _=self.locale.translate,
            handler=self)

        return self.topic_temp.generate(**data)