def on_convert(self, request): if should_exclude_visitor(request): return json_success({'excluded': 'true'}, request) experiment_name = request.args.get('experiment') client_id = request.args.get('client_id') kpi = request.args.get('kpi', None) if client_id is None or experiment_name is None: return json_error({'message': 'missing arguments'}, request, 400) dt = None if request.args.get("datetime"): dt = dateutil.parser.parse(request.args.get("datetime")) try: alt = convert(experiment_name, client_id, kpi=kpi, datetime=dt, redis=self.redis) except ValueError as e: return json_error({'message': str(e)}, request, 400) resp = { 'alternative': { 'name': alt.name }, 'experiment': { 'name': alt.experiment.name, }, 'conversion': { 'value': None, 'kpi': kpi }, 'client_id': client_id } return json_success(resp, request)
def on_client_experiments(self, request, client_id): api_key = request.args.get('api_key', None) exclude_archived = False if request.args.get( 'exclude_archived', '').lower() == 'false' else True exclude_paused = False if request.args.get( 'exclude_paused', '').lower() == 'false' else True if client_id is None: return json_error({'message': 'missing arguments'}, request, 400) try: experiment_alternatives = client_experiments( api_key, client_id, redis=self.redis, exclude_archived=exclude_archived, exclude_paused=exclude_paused) except (ValueError, APIError) as e: return json_error({'message': str(e)}, request, 500) if not experiment_alternatives: return json_success({'experiments': []}, request) experiment_info = [ { 'alternative': { 'name': alt.name }, 'experiment': { 'name': alt.experiment.name, 'is_paused': alt.experiment.is_paused(), 'is_archived': alt.experiment.is_archived() }, } for alt in experiment_alternatives ] resp = { 'experiments': experiment_info } return json_success(resp, request)
def on_traffic(): experiment_name = request.args.get('experiment') traffic_fraction = request.args.get('traffic_fraction') if traffic_fraction is None or experiment_name is None: return utils.json_error( {'message': 'set traffic fail because of missing arguments'}, request, 400) traffic_fraction = float(traffic_fraction) try: exp = Experiment.find(experiment_name, redis=db.REDIS) exp.set_traffic_fraction(traffic_fraction) exp.save() except ValueError as e: return utils.json_error({'message': str(e)}, request, 400) resp = { 'experiment': { 'name': experiment_name, }, 'traffic_fraction': traffic_fraction, 'status': 'ok' } return utils.json_success(resp, request)
def on_alt_fractions(self, request): experiment_name = request.args.get('experiment') if experiment_name is None: return json_error( { 'message': 'set alt_fractions fail because of missing experiment name' }, request, 400) try: exp = Experiment.find(experiment_name, redis=self.redis) alt_fractions = [] for alternabive in exp.alternatives: alt_fractions.append(request.args.get(alternabive.name)) exp.set_alt_fractions(alt_fractions) exp.save() except ValueError as e: return json_error({'message': str(e)}, request, 400) resp = { 'experiment': { 'name': experiment_name, }, 'alt_fraction': exp.alt_fractions, 'status': 'ok' } return json_success(resp, request)
def on_experiment_details(self, request, name): exp = Experiment.find(name, redis=self.redis) if exp is None: return json_error({'message': 'experiment not found'}, request, 404) return json_success(exp.objectify_by_period('day', True), request)
def post(self): novelid = self.get_argument("novelid", None) print novelid if not novelid: raise ValueError(401) md5novelid = hashlib.md5(novelid).hexdigest() self.write(json_success({"novelid": int(novelid), "novelsrc": BASEURL + "/static/novel/" + md5novelid + ".txt"}))
def on_participate(self, request): #获取分组列表 alts = request.args.getlist('alternatives') # alt_fractions = request.args.getlist('alt_fractions') experiment_name = request.args.get('experiment') #实验名称 force = request.args.get('force') #获取record_force参数,并进行to_bool判断若为[y,ture,yes],返回Ture,否则False record_force = to_bool(request.args.get('record_force', 'false')) client_id = request.args.get('client_id') #获取client_id traffic_fraction = request.args.get('traffic_fraction') #获取参与比例 if traffic_fraction is not None: traffic_fraction = float( traffic_fraction) #若traffic_fraction存在则转为float() prefetch = to_bool(request.args.get( 'prefetch', 'false')) #获取prefetch,并进行to_bool判断若为[y,ture,yes],返回Ture,否则Fals if client_id is None or experiment_name is None or alts is None: #三个必要参数,否则返回400 return json_error({'message': 'missing arguments'}, request, 400) dt = None if request.args.get( "datetime"): #获取datetime参数,转化为datetime.datetime()类型,没有则默认None dt = dateutil.parser.parse(request.args.get("datetime")) try: if should_exclude_visitor( request): #判断user_agent是爬虫或者IP为指定排除的ip,则拒绝参与 exp = Experiment.find(experiment_name, redis=self.redis) if exp.winner is not None: #判断是否已经胜出 alt = exp.winner else: alt = exp.control #没有胜出,返回分组中的第一个赋值给alt else: alt = participate( experiment_name, alts, client_id, force=force, record_force=record_force, traffic_fraction=traffic_fraction, # alt_fraction=alt_fractions, prefetch=prefetch, datetime=dt, redis=self.redis) except ValueError as e: return json_error({'message': str(e)}, request, 400) resp = { 'alternative': { 'name': alt.name }, 'experiment': { 'name': alt.experiment.name, }, 'client_id': client_id, 'status': 'ok' } return json_success(resp, request)
def post(self): novelid = self.get_argument("novelid", None) if not novelid: raise ValueError(401) else: chapter_list = self.novel.loadNovelChapter(int(novelid)) result = [{'subtitle': v['subtitle'], 'chapter': i, 'chapterid': v['id']} for i, v in enumerate(chapter_list, 1)] self.write(json_success(result))
def on_status(self, request): ''' 查看redis状态 :param request: :return: ''' self.redis.ping() # ping成功返回json_sucess,否则返回503 json_error return json_success({'version': __version__}, request)
def post(self): #如果username 和 password 合法,则访问数据库,如果appid下的username已经存在,则返回601 if self.fd.haveUserName(self.appid, self.username): self.write(utils.json_failed(601)) else: if not self.fd.saveUserInfo(self.appid, self.username, self.password): self.write(utils.json_failed(500)) else: self.write(utils.json_success("注册成功"))
def post(self): tag = self.get_argument("tag", None) tag = tag if tag in self.l else None page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NEWS_LIMIT if not limit else int(limit) pic_list = self.news.loadSinaGirlPic(tag, page, limit) result = [{'title': v['title'], 'tag': v['cl'], 'picon': json.loads(v['picmsg']), 'time': v['createtime']} for v in pic_list] self.write(utils.json_success(result))
def post(self): tag = self.get_argument('tag', None) tag = tag if tag in self.l else '__all__' page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NEWS_LIMIT if not limit else int(limit) news_list = self.news.loadNews(tag, page, limit) result = [{'title': v['title'], 'image': v['image'], 'desc': v['intro'], 'url': v['url'], 'create_time': v['createtime']} for v in news_list] self.write(utils.json_success(result))
def post(self): novelid = self.get_argument("novelid", None) novelid = int(novelid) if novelid else None if not novelid: raise ValueError(1) else: if self.novel.loadNovelIntroduction(novelid).__len__() != 1: raise ValueError(6) n = self.novel.addNovelPv(novelid)[0] result = {'novelid': n['id'], 'novelpv': n['novelpv']} self.write(json_success(result))
def post(self): chapterid = self.get_argument("chapterid", None) if not chapterid: raise ValueError(1) else: c = self.novel.loadNovelContent(int(chapterid)) if c.__len__() != 1: raise ValueError(500) else: result = {'title': c[0]['title'], 'subtitle': c[0]['subtitle'], 'novelid': c[0]['novelid'], 'content': c[0]['text']} self.write(json_success(result))
def post(self): novelid = self.get_argument("novelid", None) novelid = int(novelid) if novelid else None if not novelid: raise ValueError(401) else: if self.novel.loadNovelIntroduction(novelid).__len__() != 1: raise ValueError(406) n = self.novel.addNovelPv(novelid)[0] result = {'novelid': n['id'], 'novelpv': n['novelpv']} self.write(json_success(result))
def post(self): novelid = self.get_argument("novelid", None) if not novelid: raise ValueError(1) else: intro = self.novel.loadNovelIntroduction(int(novelid)) print intro.__len__() if intro.__len__() != 1: raise ValueError(500) else: result = {'title': intro[0]['title'], 'novelid': intro[0]['id'], 'author': intro[0]['author'], 'picture': "/static/spider/"+intro[0]['picture'], 'introduction': intro[0]['introduction']} self.write(json_success(result))
def post(self): novelid = self.get_argument("novelid", None) if not novelid: raise ValueError(1) else: chapter_list = self.novel.loadNovelChapter(int(novelid)) result = [{ 'subtitle': v['subtitle'], 'chapter': i, 'chapterid': v['id'] } for i, v in enumerate(chapter_list, 1)] self.write(json_success(result))
def on_convert(self, request): if should_exclude_visitor(request): return json_success({'excluded': 'true'}, request) experiment_name = request.args.get('experiment') client_id = request.args.get('client_id') kpi = request.args.get('kpi', None) if client_id is None or experiment_name is None: return json_error({'message': 'missing arguments'}, request, 400) client = Client(client_id, self.redis) try: experiment = Experiment.find(experiment_name, self.redis) if cfg.get('enabled', True): dt = None if request.args.get("datetime"): dt = dateutil.parser.parse(request.args.get("datetime")) alternative = experiment.convert(client, dt=dt, kpi=kpi) else: alternative = experiment.control.name except ValueError as e: return json_error({'message': str(e)}, request, 400) resp = { 'alternative': { 'name': alternative }, 'experiment': { 'name': experiment.name, }, 'conversion': { 'value': None, 'kpi': kpi }, 'client_id': client_id } return json_success(resp, request)
def post(self): wd = self.get_argument("wd", None) sign_method = self.get_argument("sign_method", None) if not wd: raise ValueError(401) #拼装body data = { "appid": self.appid, "sign_method": sign_method, "text": wd, "docids": "0-" + str(self.novel.getNovelDocMaxId(self.appid)), } sign = utils.md5sign(appsecret, data) data["sign"] = sign self.body = urllib.urlencode(data) resp = yield self.client() result = [] try: jsonret = json.loads(resp.body) if jsonret["code"] == 200: if len(jsonret["result"]["docs"]) != 0: novellist = self.novel.getNovelListById(jsonret["result"]["docs"]) for v in novellist: result.append({ 'id': v['id'], 'picture': "/static/spider/" + v['picture'], 'introduction': "".join(v['introduction'].split()), 'title': v['title'], 'first': v['first'], 'second': v['second'], 'novelv': v['novelpv'], 'author': v['author'], }) self.write(json_success(result)) else: self.write(json_success([])) else: self.write(json_failed(int(jsonret["code"]))) except Exception as e: self.write(json_failed(500))
def post(self): first = self.get_argument("first", None) second = self.get_argument("second", None) if first == None and second == None: tag_list = self.novel.loadAllTag() elif first != None and second == None: tag_list = self.novel.loadAllSecondTag(first) elif first == None and second != None: tag_list = self.novel.loadAllFirstTag(second) else: tag_list = self.novel.loadFirstSecondTag(first, second) result = [{'first': v['first'], 'second': v['id'], 'name': v['second']} for v in tag_list] self.write(json_success(result))
def post(self): first = self.get_argument("first", None) second = self.get_argument("second", None) page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NOVEL_LIMIT if not limit else int(limit) if not first or not second: raise ValueError(1) else: novel_list = self.novel.loadNovelList(first, second, page, limit) result = [{'novelid': v['id'], 'title': v['title'], 'novelpv': v['novelpv'], 'author': v['author'], 'introduction': v['introduction'], 'picture': "/static/spider/" + v['picture']} for v in novel_list] self.write(json_success(result))
def on_participate(self, request): alts = request.args.getlist('alternatives') experiment_name = request.args.get('experiment') bucket = request.args.get('bucket') force = request.args.get('force') client_id = request.args.get('client_id') traffic_fraction = request.args.get('traffic_fraction') if traffic_fraction is not None: traffic_fraction = float(traffic_fraction) prefetch = to_bool(request.args.get('prefetch', 'false')) if client_id is None or experiment_name is None or alts is None: return json_error({'message': 'missing arguments'}, request, 400) dt = None if request.args.get("datetime"): dt = dateutil.parser.parse(request.args.get("datetime")) if should_exclude_visitor(request): exp = Experiment.find(experiment_name, redis=self.redis) if exp.winner is not None: alt = exp.winner else: alt = exp.control else: try: alt = participate(experiment_name, alts, client_id, force=force, bucket=bucket, traffic_fraction=traffic_fraction, prefetch=prefetch, datetime=dt, redis=self.redis) except ValueError as e: return json_error({'message': str(e)}, request, 400) resp = { 'alternative': { 'name': alt.name }, 'experiment': { 'name': alt.experiment.name, }, 'client_id': client_id, 'status': 'ok' } return json_success(resp, request)
def post(self): chapterid = self.get_argument("chapterid", None) if not chapterid: raise ValueError(401) else: c = self.novel.loadNovelContent(int(chapterid)) if len(c) != 1: raise ValueError(500) else: result = {'title': c[0]['title'], 'subtitle': c[0]['subtitle'], 'novelid': c[0]['novelid'], 'content': c[0]['text'].encode("utf-8"), 'chapterid': c[0]['id'], 'prev': self.novel.loadPrevNext(int(c[0]['chapter']), int(c[0]['novelid']))[0], 'next': self.novel.loadPrevNext(int(c[0]['chapter']), int(c[0]['novelid']))[1]} #获取上一章节和下一章节 self.write(json_success(result))
def post(self): tag = self.get_argument("tag", None) tag = tag if tag in self.l else None page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NEWS_LIMIT if not limit else int(limit) pic_list = self.news.loadSinaGirlPic(tag, page, limit) result = [{ 'title': v['title'], 'tag': v['cl'], 'picon': json.loads(v['picmsg']), 'time': v['createtime'] } for v in pic_list] self.write(utils.json_success(result))
def on_participate(self, request): opts = {} alts = request.args.getlist('alternatives') experiment_name = request.args.get('experiment') force = request.args.get('force') client_id = request.args.get('client_id') distribution = request.args.get('traffic_dist') if client_id is None or experiment_name is None or alts is None: return json_error({'message': 'missing arguments'}, request, 400) if distribution: opts['distribution'] = distribution try: experiment = Experiment.find_or_create(experiment_name, alts, self.redis, opts) except ValueError as e: return json_error({'message': str(e)}, request, 400) alternative = None if force and force in alts: alternative = force elif not cfg.get('enabled', True): alternative = alts[0] elif experiment.winner is not None: alternative = experiment.winner elif should_exclude_visitor(request): alternative = alts[0] else: dt = None if request.args.get("datetime"): dt = dateutil.parser.parse(request.args.get("datetime")) client = Client(client_id, self.redis) alternative = experiment.get_alternative(client, dt=dt).name resp = { 'alternative': { 'name': alternative }, 'experiment': { 'name': experiment.name, }, 'client_id': client_id, 'status': 'ok' } return json_success(resp, request)
def post(self): page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NOVEL_LIMIT if not limit else int(limit) novel_list = self.novel.loadNovelRank(page, limit) result = [{ 'novelid': v['novelid'], 'title': v['title'], 'novelpv': v['novelpv'], 'author': v['author'], 'first': v['first'], 'second': v['second'], 'picture': v['picture'], 'rank': (page-1)*limit + i} for i, v in enumerate(novel_list, 1)] self.write(json_success(result))
def post(self): chapterid = self.get_argument("chapterid", None) if not chapterid: raise ValueError(1) else: c = self.novel.loadNovelContent(int(chapterid)) if c.__len__() != 1: raise ValueError(500) else: result = { 'title': c[0]['title'], 'subtitle': c[0]['subtitle'], 'novelid': c[0]['novelid'], 'content': c[0]['text'] } self.write(json_success(result))
def post(self): self.text = self.get_argument("text", None) self.docids = self.get_argument("docids", None) self.tags = self.get_argument("tags", None) self.timeout = self.get_argument("timeout", None) self.response = yield self.search() if self.response == 401: self.write(utils.json_failed(401)) else: tmp = json.loads(self.response.body)["result"] r = {} r["tokens"] = tmp["Tokens"] r["docs"] = [utils.decodeDocid(int(self.appid), int(docid['DocId'])) for docid in tmp["Docs"]] r["timeout"] = tmp["Timeout"] self.write(utils.json_success(r)) self.finish()
def post(self): page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NOVEL_LIMIT if not limit else int(limit) novel_list = self.novel.loadNovelRank(page, limit) result = [{ 'novelid': v['novelid'], 'title': v['title'], 'novelpv': v['novelpv'], 'author': v['author'], 'first': v['first'], 'second': v['second'], 'picture': v['picture'], 'rank': (page - 1) * limit + i } for i, v in enumerate(novel_list, 1)] self.write(json_success(result))
def post(self): first = self.get_argument("first", None) second = self.get_argument("second", None) if first == None and second == None: tag_list = self.novel.loadAllTag() elif first != None and second == None: tag_list = self.novel.loadAllSecondTag(first) elif first == None and second != None: tag_list = self.novel.loadAllFirstTag(second) else: tag_list = self.novel.loadFirstSecondTag(first, second) result = [{ 'first': v['first'], 'second': v['id'], 'name': v['second'] } for v in tag_list] self.write(json_success(result))
def on_user_experiment_alternatives(self, request, name): api_key = request.args.get('api_key', None) start = request.args.get('sequence_id_start') or 1 page_size = request.args.get('page_size') or 5000 end = int(start) + int(page_size) - 1 exp = Experiment.find(api_key, name, redis=self.redis) if exp is None: return json_error({'message': 'experiment not found'}, request, 404) user_alternatives = experiment_user_alternatives( api_key, exp, redis=self.redis, start=start, end=end) resp = { 'alternatives': [ {'user_id': user_id, 'alternative_name': alt} for user_id, alt in user_alternatives.iteritems() ] } return json_success(resp, request)
def on_participate(self, request): alts = request.args.getlist('alternatives') experiment_name = request.args.get('experiment') force = request.args.get('force') record_force = to_bool(request.args.get('record_force', 'false')) client_id = request.args.get('client_id') traffic_fraction = request.args.get('traffic_fraction') if traffic_fraction is not None: traffic_fraction = float(traffic_fraction) prefetch = to_bool(request.args.get('prefetch', 'false')) if client_id is None or experiment_name is None or alts is None: return json_error({'message': 'missing arguments'}, request, 400) dt = None if request.args.get("datetime"): dt = dateutil.parser.parse(request.args.get("datetime")) try: if should_exclude_visitor(request): exp = Experiment.find(experiment_name, redis=self.redis) if exp.winner is not None: alt = exp.winner else: alt = exp.control else: alt = participate(experiment_name, alts, client_id, force=force, record_force=record_force, traffic_fraction=traffic_fraction, prefetch=prefetch, datetime=dt, redis=self.redis) except ValueError as e: return json_error({'message': str(e)}, request, 400) resp = { 'alternative': { 'name': alt.name }, 'experiment': { 'name': alt.experiment.name, }, 'client_id': client_id, 'status': 'ok' } return json_success(resp, request)
def post(self): novelid = self.get_argument("novelid", None) if not novelid: raise ValueError(1) else: intro = self.novel.loadNovelIntroduction(int(novelid)) print intro.__len__() if intro.__len__() != 1: raise ValueError(500) else: result = { 'title': intro[0]['title'], 'novelid': intro[0]['id'], 'author': intro[0]['author'], 'picture': "/static/spider/" + intro[0]['picture'], 'introduction': intro[0]['introduction'] } self.write(json_success(result))
def post(self): tag = self.get_argument('tag', None) tag = tag if tag in self.l else '__all__' page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NEWS_LIMIT if not limit else int(limit) print page, limit, tag news_list = self.news.loadNews(tag, page, limit) print news_list result = [{ 'title': v['title'], 'image': v['image'], 'desc': v['intro'], 'url': v['url'], 'create_time': v['createtime'] } for v in news_list] self.write(utils.json_success(result))
def post(self): novelid = self.get_argument("novelid", None) if not novelid: raise ValueError(1) else: intro = self.novel.loadNovelIntroduction(int(novelid)) chapter_list = self.novel.loadNovelChapter(int(novelid)) if intro.__len__() != 1: raise ValueError(500) else: result = { 'title': intro[0]['title'], 'novelid': intro[0]['id'], 'author': intro[0]['author'], 'picture': "/static/spider/"+intro[0]['picture'], 'chapternum': len(chapter_list), 'introduction': "".join(intro[0]['introduction'].split()), } self.write(json_success(result))
def post(self): first = self.get_argument("first", None) second = self.get_argument("second", None) page = self.get_argument("page", None) page = 1 if not page else int(page) limit = self.get_argument("limit", None) limit = const.NOVEL_LIMIT if not limit else int(limit) if not first or not second: raise ValueError(1) else: novel_list = self.novel.loadNovelList(first, second, page, limit) result = [{ 'novelid': v['id'], 'title': v['title'], 'novelpv': v['novelpv'], 'author': v['author'], 'introduction': v['introduction'], 'picture': "/static/spider/" + v['picture'] } for v in novel_list] self.write(json_success(result))
def status(): db.REDIS.ping() return utils.json_success({'version': __version__}, request)
def on_status(self, request): self.redis.ping() return json_success({'version': __version__}, request)