def stream(uuid, olderThan=None, newerThan=None, uid=None, list_id=None, type='normal'): ''' olderThan, newerThan: time since epoch (in milliseconds). uid: if exist, return uid's public messages. type: normal: the home timeline for uuid. mentions: messages mentioning uuid. unique: only one message specified by msg_uuid. user: messages published by uid. list: message published by users in list_id. ''' # setup basic query if type == 'unique': query = {'_id': ObjectId(msg_uuid)} elif type == 'normal': # uuid's home timeline u = get_user(uuid) following = u['following'] following_query = {'owner': {'$in': following + [u['_id']]}} mention_query = {'entities.mentions.mention': '@' + u['uid']} query = {'$or': [following_query, mention_query]} elif type == 'mentions': u = get_user(uuid) query = {'entities.mentions.mention': '@' + u['uid']} elif type == 'user': # get uid's public tweets target = find_user(uid) if target: query = {'owner': target['_id']} else: return error.user_not_found(raw=True) elif type == 'list': l = lists.find_one(ObjectId(list_id)) if l: query = {'owner': {'$in': l['people']}} else: return error.list_not_found(raw=True) else: return error.stream_type_not_supported(raw=True) # setup time constraints if olderThan or newerThan: query['timestamp'] = {} if olderThan: query['timestamp']['$lt'] = olderThan if newerThan: query['timestamp']['$gt'] = newerThan # then execute the query c = messages.find(query) \ .sort('timestamp', pymongo.DESCENDING) \ .batch_size(conf.stream_item_max) return _process_messages(uuid, c)
def get_list_users(uuid, list_id, skip): l = lists.find_one(ObjectId(list_id)) if not l: return error.list_not_found(raw=True) max_index = skip + conf.stream_item_max ret = {"has_more": max_index < len(l["people"]), "items": [get_user(uuid) for uuid in l["people"][skip:max_index]]} return ret
def get_list_info(uuid, list_id): ret = lists.find_one(ObjectId(list_id)) if ret: print ret u = get_user(ret['curator']) ret['id'] = ret['_id'] ret['curator'] = u and u['uid'] or '!invalid' return ret or error.list_not_found(raw=True)
def remove_from_list(uuid, list_id, uid): u = find_user(uid) if not u: return error.user_not_found(raw=True) if not lists.find_one(ObjectId(list_id)): return error.list_not_found(raw=True) lists.update({'_id': ObjectId(list_id)}, {'$pull': {'people': u['_id']}}) return {'success': 1}
def get_list_info(uuid, list_id): ret = lists.find_one(ObjectId(list_id)) if ret: print ret u = get_user(ret["curator"]) ret["id"] = ret["_id"] ret["curator"] = u and u["uid"] or "!invalid" return ret or error.list_not_found(raw=True)
def remove_from_list(uuid, list_id, uid): u = find_user(uid) if not u: return error.user_not_found(raw=True) if not lists.find_one(ObjectId(list_id)): return error.list_not_found(raw=True) lists.update({"_id": ObjectId(list_id)}, {"$pull": {"people": u["_id"]}}) return {"success": 1}
def get_list_users(uuid, list_id, skip): l = lists.find_one(ObjectId(list_id)) if not l: return error.list_not_found(raw=True) max_index = skip + conf.stream_item_max ret = { 'has_more': max_index < len(l['people']), 'items': [get_user(uuid) for uuid in l['people'][skip:max_index]] } return ret
def stream(uuid, olderThan=None, newerThan=None, uid=None, list_id=None, type="normal"): """ olderThan, newerThan: time since epoch (in milliseconds). uid: if exist, return uid's public messages. type: normal: the home timeline for uuid. mentions: messages mentioning uuid. unique: only one message specified by msg_uuid. user: messages published by uid. list: message published by users in list_id. """ # setup basic query if type == "unique": query = {"_id": ObjectId(msg_uuid)} elif type == "normal": # uuid's home timeline u = get_user(uuid) following = u["following"] following_query = {"owner": {"$in": following + [u["_id"]]}} mention_query = {"entities.mentions.mention": "@" + u["uid"]} query = {"$or": [following_query, mention_query]} elif type == "mentions": u = get_user(uuid) query = {"entities.mentions.mention": "@" + u["uid"]} elif type == "user": # get uid's public tweets target = find_user(uid) if target: query = {"owner": target["_id"]} else: return error.user_not_found(raw=True) elif type == "list": l = lists.find_one(ObjectId(list_id)) if l: query = {"owner": {"$in": l["people"]}} else: return error.list_not_found(raw=True) else: return error.stream_type_not_supported(raw=True) # setup time constraints if olderThan or newerThan: query["timestamp"] = {} if olderThan: query["timestamp"]["$lt"] = olderThan if newerThan: query["timestamp"]["$gt"] = newerThan # then execute the query c = messages.find(query).sort("timestamp", pymongo.DESCENDING).batch_size(conf.stream_item_max) return _process_messages(uuid, c)
def remove_list(uuid, list_id): l = lists.find_one({'curator': ObjectId(uuid), '_id': ObjectId(list_id)}) if not l: return error.list_not_found(raw=True) lists.remove(ObjectId(list_id)) return {'success': 1}
def remove_list(uuid, list_id): l = lists.find_one({"curator": ObjectId(uuid), "_id": ObjectId(list_id)}) if not l: return error.list_not_found(raw=True) lists.remove(ObjectId(list_id)) return {"success": 1}