def api_checkbenben(): uid = request.args.get('uid', -1, type=int) page = request.args.get('page', 1, type=int) headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" } try: benbens = requests.get( 'https://www.luogu.com.cn/api/feed/list?user={}&page={}'.format( uid, page), headers=headers).json() except: time.sleep(5) benbens = requests.get( 'https://www.luogu.com.cn/api/feed/list?user={}&page={}'.format( uid, page), headers=headers).json() benbens = benbens['feeds']['result'] cur = datetime.datetime.now() cnt = 0 for i in benbens[::-1]: text = markdown.markdown(i['content']) username = i['user']['name'] stime = datetime.datetime.fromtimestamp(i['time']) if BenBen.query.filter_by(uid=uid, time=stime).all(): continue abb = BenBen() abb.text = text.replace('<p>', "").replace('</p>', "") abb.username = username abb.uid = uid abb.time = stime abb.lid = i['id'] user = LuoguUser.query.filter_by(uid=uid).first() if user: user.benbens.append(abb) if user.username != username: user.username = username else: user = LuoguUser(username=username, uid=uid) db.session.add(user) user.benbens.append(abb) db.session.add(abb) db.session.commit() if stime.date() == cur.date(): cnt += 1 return str(cnt), 200, { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept", 'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS' }
def api_checkbenben(): uid = request.args.get('uid', -1, type=int) headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" } benbens = requests.get( 'https://www.luogu.com.cn/api/feed/list?user={}'.format(uid), headers=headers).json() benbens = benbens['feeds']['result'] cur = datetime.datetime.now() cnt = 0 for i in benbens[::-1]: text = markdown.markdown(i['content']) username = i['user']['name'] stime = datetime.datetime.fromtimestamp(i['time']) if BenBen.query.filter_by(uid=uid, time=stime).all(): continue abb = BenBen() abb.text = text.replace('<p>', "").replace('</p>', "") abb.username = username abb.uid = uid abb.time = stime user = LuoguUser.query.filter_by(uid=uid).first() if user: user.benbens.append(abb) if user.username != username: user.username = username else: user = LuoguUser(username=username, uid=uid) db.session.add(user) user.benbens.append(abb) db.session.add(abb) db.session.commit() if stime.date() == cur.date(): cnt += 1 return str(cnt)
def add_user(id): user = LuoguUser.query.filter(LuoguUser.uid == id).first() if user: return jsonify({"status": "fail", "message": "User exists"}) user = LuoguUser() headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" } data = requests.get( "https://www.luogu.com.cn/api/user/search?keyword={}".format(id), headers=headers).json() data = data['users'][0] user.uid = id user.username = data['name'] user.color = data['color'] user.ccf_level = data['ccfLevel'] db.session.add(user) db.session.commit() return jsonify({"status": "success"})