def post(self): # when this request is received, user id has been saved in login process # token validation self.ustc_id = self.get_argument('id') token = self.get_argument('token') time = parse.str_to_datetime(self.get_argument('time')) rows = self.get_argument('rows') mode = self.get_argument('mode') true_token = str(util.hash.generate_token(self.ustc_id)) if not true_token == token: raise web.HTTPError(500) # print "%s req Detail" % (self.ustc_id) resp_data = [] if mode == 'newer': operator = '$gt' else: operator = '$lt' cursor = db.record.find({ 'ustc_id': self.ustc_id, 'time': {operator: time} }) cursor.sort([('time', -1)]) for document in (yield cursor.to_list(length=int(rows))): resp_data.append(document) json_data = parse.to_json(resp_data, escape_time=True) self.write(json_data) self.finish()
def count_daily_sum(self): # get month sum via aggregate pipeline = [ {'$match': { '$and': [ {'ustc_id': self.ustc_id}, {'type': "消费"}, {'time': {'$gt': parse.get_days_before(60)}}, ]}}, {'$group': { '_id': {'$dayOfYear': "$time"}, 'amount': {'$sum': "$amount"}, 'time': {'$last': '$time'}, 'ustc_id': {'$last': '$ustc_id'} }} ] resp = [{'key': u'消费金额', 'color': "#54B59A", 'values':[]}] tmp = [] cursor = yield db.record.aggregate(pipeline, cursor={}) while (yield cursor.fetch_next): item = cursor.next_object() tmp.append([int(item['time'].strftime('%s')) * 1000, item['amount']]) tmp.sort(key=operator.itemgetter(0)) resp[0]['values'] = tmp self.write(parse.to_json(resp)) self.finish() raise gen.Return()
def post(self): # when this request is received, user id has been saved in login process # token validation self.ustc_id = self.get_argument('id') token = self.get_argument('token') time = parse.str_to_datetime(self.get_argument('time')) rows = self.get_argument('rows') mode = self.get_argument('mode') true_token = str(util.hash.generate_token(self.ustc_id)) if not true_token == token: raise web.HTTPError(500) # print "%s req Detail" % (self.ustc_id) resp_data = [] if mode == 'newer': operator = '$gt' else: operator = '$lt' cursor = db.record.find({ 'ustc_id': self.ustc_id, 'time': { operator: time } }) cursor.sort([('time', -1)]) for document in (yield cursor.to_list(length=int(rows))): resp_data.append(document) json_data = parse.to_json(resp_data, escape_time=True) self.write(json_data) self.finish()
def post(self): input_id = self.get_argument('inputId') input_password = self.get_argument('inputPassword') yield db.log.insert({ 'ustc_id': input_id, 'time': datetime.datetime.now() }) # visit index to get cookie visit_index = HTTPRequest(url="http://ecard.ustc.edu.cn", method='GET', headers={'User-Agent': 'Firefox'}) response = yield gen.Task(client.fetch, visit_index) # get captcha code with cookie cookie = response.headers['Set-Cookie'].split(';')[0] get_captcha = HTTPRequest( url="http://ecard.ustc.edu.cn/sisms/index.php/login/getimgcode", user_agent="Firefox", headers={'Cookie': cookie}) response = yield gen.Task(client.fetch, get_captcha) # generate captcha code and try login code = captcha.parse(response.body) login = HTTPRequest( url="http://ecard.ustc.edu.cn/sisms/index.php/login/dologin/", method='POST', headers={'Cookie': cookie}, body='username='******'&password='******'&usertype=1&schoolcode=001&imgcode=' + code) yield gen.Task(client.fetch, login) # test login status state_test = HTTPRequest( url="http://ecard.ustc.edu.cn/sisms/index.php/person/information", method='GET', headers={'Cookie': cookie}) response = yield gen.Task(client.fetch, state_test) # return token if success if response.body is not None and len(response.body) > 8000: name = parse.find_name(response.body) token = util.hash.generate_token(input_id) usr_tmp = yield db.tmp.find_one({'ustc_id': input_id}) # for new user, set flag to mark fetch status if usr_tmp is None: usr_tmp = {'ustc_id': input_id, 'fetching_for_new': True} # do not save password into temp collection usr_tmp['cookie'] = cookie usr_tmp['token'] = token # print "usr_tmp", usr_tmp yield db.tmp.save(usr_tmp) self.write(parse.to_json(dict(token=token, name=name))) self.finish() else: raise web.HTTPError(500)
def monthly_sum(self): cursor = db.monthly.find({ "ustc_id": self.ustc_id }).sort([('time', 1)]) resp = [{'key': u'消费金额', 'color': "#EA9F33", 'values':[]}] tmp = [] while (yield cursor.fetch_next): item = cursor.next_object() tmp.append({'x': int(item['time'].strftime('%s')) * 1000, 'y': item['total']}) resp[0]['values'] = tmp self.write(parse.to_json(resp)) self.finish() raise gen.Return()
def monthly_sum(self): cursor = db.monthly.find({"ustc_id": self.ustc_id}).sort([('time', 1)]) resp = [{'key': u'消费金额', 'color': "#EA9F33", 'values': []}] tmp = [] while (yield cursor.fetch_next): item = cursor.next_object() tmp.append({ 'x': int(item['time'].strftime('%s')) * 1000, 'y': item['total'] }) resp[0]['values'] = tmp self.write(parse.to_json(resp)) self.finish() raise gen.Return()
def count_daily_sum(self): # get month sum via aggregate pipeline = [{ '$match': { '$and': [ { 'ustc_id': self.ustc_id }, { 'type': "消费" }, { 'time': { '$gt': parse.get_days_before(60) } }, ] } }, { '$group': { '_id': { '$dayOfYear': "$time" }, 'amount': { '$sum': "$amount" }, 'time': { '$last': '$time' }, 'ustc_id': { '$last': '$ustc_id' } } }] resp = [{'key': u'消费金额', 'color': "#54B59A", 'values': []}] tmp = [] cursor = yield db.record.aggregate(pipeline, cursor={}) while (yield cursor.fetch_next): item = cursor.next_object() tmp.append( [int(item['time'].strftime('%s')) * 1000, item['amount']]) tmp.sort(key=operator.itemgetter(0)) resp[0]['values'] = tmp self.write(parse.to_json(resp)) self.finish() raise gen.Return()
def post(self): self.ustc_id = self.get_argument('id') self.token = self.get_argument('token') self.time = self.get_argument('time') self.usr = yield db.tmp.find_one({'ustc_id': self.ustc_id}) if self.usr is None: raise web.HTTPError(403) if str(self.usr['token']) != self.token: print 'not done' if self.usr['fetching_for_new'] is True: yield gen.Task(self.fetch_record, self.time) yield gen.Task(self.calculate) # for old user, the fetching_for_new flag is false else: yield gen.Task(self.update) yield gen.Task(self.calculate) self.write(parse.to_json({'end': True}))
def recent_data(self): cursor = db.record.find( spec={ 'ustc_id': self.ustc_id, 'type': u'消费', 'amount': {'$gt': 0}, 'time': {'$gt': parse.get_days_before(10)} }, fields={ 'time': True, 'amount': True, }) cursor.sort([('time', -1)]) resp = [] for document in (yield cursor.to_list(length=int(200))): resp.append(document) json_data = parse.to_json(resp, escape_time=True) self.write(json_data) self.finish() raise gen.Return()
def recent_data(self): cursor = db.record.find(spec={ 'ustc_id': self.ustc_id, 'type': u'消费', 'amount': { '$gt': 0 }, 'time': { '$gt': parse.get_days_before(10) } }, fields={ 'time': True, 'amount': True, }) cursor.sort([('time', -1)]) resp = [] for document in (yield cursor.to_list(length=int(200))): resp.append(document) json_data = parse.to_json(resp, escape_time=True) self.write(json_data) self.finish() raise gen.Return()
def post(self): ustc_id = self.get_argument('id') print 'ustc-id', ustc_id yield gen.Task(self.foo) self.write(parse.to_json(dict(time="2014-10-20", end=False))) self.finish()
def post(self): input_id = self.get_argument('inputId') input_password = self.get_argument('inputPassword') yield db.log.insert({ 'ustc_id': input_id, 'time': datetime.datetime.now() }) # visit index to get cookie visit_index = HTTPRequest( url="http://ecard.ustc.edu.cn", method='GET', headers={ 'User-Agent': 'Firefox' } ) response = yield gen.Task(client.fetch, visit_index) # get captcha code with cookie cookie = response.headers['Set-Cookie'].split(';')[0] get_captcha = HTTPRequest( url="http://ecard.ustc.edu.cn/sisms/index.php/login/getimgcode", user_agent="Firefox", headers={ 'Cookie': cookie } ) response = yield gen.Task(client.fetch, get_captcha) # generate captcha code and try login code = captcha.parse(response.body) login = HTTPRequest( url="http://ecard.ustc.edu.cn/sisms/index.php/login/dologin/", method='POST', headers={ 'Cookie': cookie }, body='username='******'&password='******'&usertype=1&schoolcode=001&imgcode=' + code ) yield gen.Task(client.fetch, login) # test login status state_test = HTTPRequest( url="http://ecard.ustc.edu.cn/sisms/index.php/person/information", method='GET', headers={ 'Cookie': cookie } ) response = yield gen.Task(client.fetch, state_test) # return token if success if response.body is not None and len(response.body) > 8000: name = parse.find_name(response.body) token = util.hash.generate_token(input_id) usr_tmp = yield db.tmp.find_one({ 'ustc_id': input_id }) # for new user, set flag to mark fetch status if usr_tmp is None: usr_tmp = { 'ustc_id': input_id, 'fetching_for_new': True } # do not save password into temp collection usr_tmp['cookie'] = cookie usr_tmp['token'] = token # print "usr_tmp", usr_tmp yield db.tmp.save(usr_tmp) self.write(parse.to_json(dict(token=token, name=name))) self.finish() else: raise web.HTTPError(500)
def post(self): ustc_id = self.get_argument('id') token = self.get_argument('token') true_token = str(util.hash.generate_token(ustc_id)) if not true_token == token: raise web.HTTPError(500) # print "%s req Brief" % (ustc_id) # find sum of this week weekly_sum = 0 start_of_this_week = parse.start_of_this_week() weekly_cursor = db.record.find( spec={ 'ustc_id': ustc_id, 'type': u'消费', 'amount': {'$gt': 0}, 'time': {'$gte': start_of_this_week} }, fields={ 'amount': True, } ).sort([('time', -1)]) while (yield weekly_cursor.fetch_next): weekly_sum += weekly_cursor.next_object()['amount'] # print "weekly sum", weekly_sum # find sum of last month last_month_cursor = yield db.monthly.find_one({ 'ustc_id': ustc_id, 'time': { '$lt': parse.start_of_this_month(), '$gte': parse.start_of_last_month() } }) if last_month_cursor is not None and last_month_cursor['total'] is not None: last_month_sum = last_month_cursor['total'] else: last_month_sum = 0 # print "last month sum", last_month_sum # find sum of this month this_month_sum = 0 this_month_cursor = yield db.monthly.find_one({ 'ustc_id': ustc_id, 'time': { '$gte': parse.start_of_this_month() } }) if this_month_cursor is not None: this_month_sum = this_month_cursor['total'] else: this_month_sum = 0 print "this month sum", this_month_sum # find rank by last month sum usr = yield db.tmp.find_one({'ustc_id': ustc_id}) yield db.tmp.save(usr) base_rank = yield db.monthly.find({ 'time': { '$lt': parse.start_of_this_month(), '$gte': parse.start_of_last_month() } }).count() print base_rank me_rank = yield db.monthly.find({ 'total': {'$gte': last_month_sum}, 'time': { '$lt': parse.start_of_this_month(), '$gte': parse.start_of_last_month() } }).count() # the richer, the less rate # rate = int((float(me_rank) / float(base_rank)) * 100) resp = { 'this_week': weekly_sum, 'this_month': this_month_sum, 'last_month': last_month_sum, 'rate': 100 } self.write(parse.to_json(resp)) self.finish()
def post(self): ustc_id = self.get_argument('id') token = self.get_argument('token') true_token = str(util.hash.generate_token(ustc_id)) if not true_token == token: raise web.HTTPError(500) # print "%s req Brief" % (ustc_id) # find sum of this week weekly_sum = 0 start_of_this_week = parse.start_of_this_week() weekly_cursor = db.record.find(spec={ 'ustc_id': ustc_id, 'type': u'消费', 'amount': { '$gt': 0 }, 'time': { '$gte': start_of_this_week } }, fields={ 'amount': True, }).sort([('time', -1)]) while (yield weekly_cursor.fetch_next): weekly_sum += weekly_cursor.next_object()['amount'] # print "weekly sum", weekly_sum # find sum of last month last_month_cursor = yield db.monthly.find_one({ 'ustc_id': ustc_id, 'time': { '$lt': parse.start_of_this_month(), '$gte': parse.start_of_last_month() } }) if last_month_cursor is not None and last_month_cursor[ 'total'] is not None: last_month_sum = last_month_cursor['total'] else: last_month_sum = 0 # print "last month sum", last_month_sum # find sum of this month this_month_sum = 0 this_month_cursor = yield db.monthly.find_one({ 'ustc_id': ustc_id, 'time': { '$gte': parse.start_of_this_month() } }) if this_month_cursor is not None: this_month_sum = this_month_cursor['total'] else: this_month_sum = 0 print "this month sum", this_month_sum # find rank by last month sum usr = yield db.tmp.find_one({'ustc_id': ustc_id}) yield db.tmp.save(usr) base_rank = yield db.monthly.find({ 'time': { '$lt': parse.start_of_this_month(), '$gte': parse.start_of_last_month() } }).count() print base_rank me_rank = yield db.monthly.find({ 'total': { '$gte': last_month_sum }, 'time': { '$lt': parse.start_of_this_month(), '$gte': parse.start_of_last_month() } }).count() # the richer, the less rate # rate = int((float(me_rank) / float(base_rank)) * 100) resp = { 'this_week': weekly_sum, 'this_month': this_month_sum, 'last_month': last_month_sum, 'rate': 100 } self.write(parse.to_json(resp)) self.finish()