def main(): connection = None try: log('cron1', 'Starting') connection, db = connect() subscriptions = Subscriptions.find(db, {'active': True, 'first_tweet': False}) for subscription_raw in subscriptions: created_message = Queue.insert( 'payments', { 'subscription_id': str(subscription_raw['_id']), 'account_id': str(subscription_raw['account_id']), 'parrot_id': str(subscription_raw['parrot_id']) } ) log('cron1', 'Payment queued %s' % created_message.id, subscription_raw['_id']) db.subscriptions.update({'_id': subscription_raw['_id']}, {'$set': {'first_tweet': True}}) log('cron1', 'Subscription updated', subscription_raw['_id']) finally: if connection: connection.close() log('cron1', 'Finishing')
def get_parrots(account_id, db, secure = True): querystring = request.query; from_ = querystring.get("from") to_ = querystring.get("to") skip = int(querystring.get('skip', 0)) limit = int(querystring.get('limit', 0)) query_subscriptions = {'account_id': ObjectId(account_id), 'active': True} if from_ or to_: query_subscriptions['created_at'] = {} if from_: query_subscriptions['created_at']['$gte'] = datetime.strptime(from_, '%Y-%m-%d') if to_: query_subscriptions['created_at']['$lte'] = datetime.strptime(to_, '%Y-%m-%d') if querystring.screen_name: screen_name_regex = re.compile(querystring.screen_name,re.IGNORECASE) query_subscriptions['twitter_screen_name'] = screen_name_regex parrots_from_subscriptions = Subscriptions.find(db, query_subscriptions, {'parrot_id': True, '_id': False}).skip(skip).limit(limit).sort([('_id', -1)]) parrots_id = map(lambda x: x.get('parrot_id'), parrots_from_subscriptions) parrots = list(Parrots.find(db, {'_id': {'$in': parrots_id}})) for parrot in parrots: parrot['payments'] = filter(lambda x: x.get('account_id') == account_id, parrot.get('payments')) # TODO: fix this response.headers['Content-type'] = 'application/json' return json.dumps(map(lambda x: Parrots.toJSON(x), parrots))