class CategoryService(object): def __init__(self): self.__base = None def get_categorys(self): self.__base = BaseDao() op = Category(self.__base) rows = op.get_all_category() data = [] for row in rows: item = {} item['text'] = row.name item['type'] = row.c_type item['type_id'] = row.type_id item['url'] = read_icon_url_config(row.name) item['url_focus'] = read_icon_url_f_config(row.name) item['url_select'] = read_icon_url_s_config(row.name) data.append(item) result = {} result['navs'] = data return result def close(self): if (self.__base is not None): self.__base.close()
def history(): logger.info('rec a history request') res = {'code': 1, 'msg': 'success', 'content': {}} userid = request.args.get("userid") or '' if not userid: res['code'] = 0 res['msg'] = 'need userid' return jsonify(res) if validSqlInject(userid): return jsonify({'code': 1, 'msg': 'invalid param'}) if len(userid) > 32: logger.info('fail|user arg invalid, return') return jsonify({'code': -1, 'msg': 'userid invalid'}) content = {} itemList = [] content['itemList'] = itemList b = BaseDao() try: ub = UserBehavior(userid, b) for v_obj in ub.get_video_items(): #v_obj = assemble(item) itemList.append(v_obj) res['content'] = content logger.info('success|history request serve success') except: logger.error('fail|exception|history request serve error|%s' % traceback.format_exc()) finally: b.close() return jsonify(res)
class Recommender(object): def __init__(self, num=50): self.base = BaseDao(read_db_config()) self.num = num def recommend(self): uv = UserVideo(base=self.base) ur = UserRecommender(base=self.base) for user_id in uv.get_all_users(): video_logger.info('start rec video of user_id:%s' % user_id) items = [] for k, v in get_rec_list(self.num, user_id): items.append(k) if len(items): rec_list_str = ','.join(items) video_logger.info('add user:%s recommend:%s' % (user_id, rec_list_str)) ur.add_recommend(user_id, rec_list_str) video_logger.info('finish rec user_id:%s' % user_id) def music_recommend(self): music_logger.info('start music recommend') rec_music(self.base) music_logger.info('finish music recommend') def close(self): self.base.close()
def uid(): logger.info('rec a uid request') res = {'code': 1, 'msg': 'success', 'uid': 'unkown'} b = BaseDao() try: user_dao = User(base=b) macid = request.args.get("macid") or '' androidid = request.args.get("androidid") or '' if validSqlInject(macid) or validSqlInject(androidid): return jsonify({'code': 1, 'msg': 'invalid param'}) if macid or androidid: user = user_dao.get_user_info(macid, androidid) if not user: uid = get_user_id(macid, androidid) user_dao.add_user_id(uid, macid, androidid) else: uid = user.user_id res['uid'] = uid else: res['uid'] = get_user_id() logger.info('success|uid request serve success') except: logger.error('fail|exception|uid request serve error|%s' % traceback.format_exc()) finally: b.close() return jsonify(res)
def ub(): logger.info('rec a ub request') data = request.json uid = data.get('userid') vid = data.get('id') if validSqlInject(vid) or validSqlInject(uid): return jsonify({'code': 1, 'msg': 'invalid param'}) if not (uid and vid): logger.info('fail|userid or vid is null, return') return jsonify({'code': -1, 'msg': 'need userid and vid'}) if len(uid) > 32 or len(vid) > 32: logger.info('fail|userid or vid is invalid,userid:%s,vid:%s return' % (uid, vid)) return jsonify({'code': -1, 'msg': 'userid and vid invalid.'}) duration = data.get('duration') or 0 b = BaseDao() try: uv = UserVideo(base=b) v_dao = Video(base=b) uv.add_item(uid, vid, int(time.time()), duration) v_dao.add_play_count(vid) logger.info('success|ub request serve success') except: logger.error('fail|exception|ub request serve error|%s' % traceback.format_exc()) finally: b.close() return jsonify({'code': 0, 'msg': 'success'})
def del_all_history(): logger.info('rec a delallhistory request') res = {'code': 1, 'msg': 'success'} userid = request.args.get("userid") or '' if not userid: res['code'] = 0 res['msg'] = 'need userid' return jsonify(res) if len(userid) > 32: logger.info('fail|user arg invalid, return') return jsonify({'code': -1, 'msg': 'userid invalid'}) b = BaseDao() ub = UserBehavior(userid, b) try: ub.del_all_video_history() logger.info('success|delallhistory request serve success') except: res['code'] = 1 res['msg'] = '操作失败' logger.error('fail|exception|delallhistory request serve error|%s' % traceback.format_exc()) finally: b.close() return jsonify(res)
def get_fans(self, user_id=0, page_num=0, page_size=10): self.__base = BaseDao() op = UserProfile(self.__base) (total, rows) = op.get_fans_by_user(user_id, page_size, page_num) is_big_v = False result = [] for row in rows: u = row.toDict() u['is_big_v'] = is_big_v if (is_big_v): is_big_v = False else: is_big_v = True (play_count, icons) = op.get_user_content(u['userid']) u['total_play_count'] = play_count u['games'] = icons result.append(u) data = {} data['fans'] = result data['total'] = total return data
def get_video_comment(self, vid): self.__base = BaseDao() videoComment = VideoComment(self.__base) data = videoComment.get_video_comment(vid) if (data): return self._convert_to_xml(data) else: return ''
def watch(self, watch_id, watched_id, status): self.__base = BaseDao() op = UserProfile(self.__base) op.watch(watch_id, watched_id, status) data = {} data['op_result'] = '成功' return data
def get_by_id(id): dao = BaseDao() sql = 'select * from v_foods_cate where id=%s' data = dao.query(sql, id) # 将当前查看的foods_id加入人气排行中 redis_.add_popu_rank_foods(id) return jsonify(data)
def get_all_app(self): self.__base = BaseDao() app = Application(self.__base) rows = app.get_all_app() data = {} for row in rows: data[row.appid] = row.toDict() return data
def get_base_comment(self, video_type='all'): self.__base = BaseDao() baseComment = BaseComment(self.__base) data = [] result = baseComment.get_comment() for value in result: data.append(value.toDict()) return data
def test_conn(self): db = DB(db='dytt') with db: # 断言db.conn不是None # 如果断言失败,表示db.conn是None,数据库连接失败! self.assertIsNotNone(db.conn, '数据库连接失败!') dao = BaseDao(db) data = Dytt('a', 'b', 'c', 2014, 'ftp://www.aa.cn/aa.mp4') dao.save(data)
def top_rank_foods(): rank_list = redis_.top_popu_rank_foods(5) # 前5个人气排行 dao = BaseDao() sql = 'select * from v_foods_cate where id=%s' results = { score: dao.query(sql, foods_id)[0] for foods_id, score in rank_list } return jsonify(results)
class MediaService(object): def __init__(self): self.__base = None def get_music(self, user_id): self.__base = BaseDao() music = Media(self.__base) rows = music.get_all_music(user_id) arr = [] for row in rows: dic = row.toDict(tp=2) arr.append(dic) data = {} data['musics'] = arr data['total'] = len(arr) return data def get_video(self, user_id): self.__base = BaseDao() media = Media(self.__base) rows = media.get_all_video(user_id) arr = [] for row in rows: dic = row.toDict(tp=3) arr.append(dic) data = {} data['videos'] = arr data['total'] = len(arr) return data def get_picture(self, user_id): self.__base = BaseDao() media = Media(self.__base) rows = media.get_all_picture(user_id) arr = [] for row in rows: dic = row.toDict(tp=1) arr.append(dic) data = {} data['pictures'] = arr data['total'] = len(arr) return data def close(self): if (self.__base is not None): self.__base.close()
def get_picture(self, user_id): self.__base = BaseDao() media = Media(self.__base) rows = media.get_all_picture(user_id) arr = [] for row in rows: dic = row.toDict(tp=1) arr.append(dic) data = {} data['pictures'] = arr data['total'] = len(arr) return data
def get_music(self): self.__base = BaseDao() music = Music(self.__base) rows = music.get_all_music() arr = [] for row in rows: dic = row.toDict() arr.append(dic) data = {} data['musics'] = arr data['total'] = len(arr) return data
def get_watch_videos(self, user_id, page_num): self.__base = BaseDao() video = Video(self.__base) watch_users = self.get_watch_all(user_id=user_id) (total, rows) = video.get_video_by_users(page_num=page_num, page_size=30, user_ids=watch_users) data = {} videos = [] data['videos'] = videos data['total'] = len(rows) for row in rows: videos.append(row.toDict()) return data
def get_update(self, version_code): self.__base = BaseDao() version = Version(self.__base) versions = version.get_version(version_code) max_version_code = version_code download_url = '' force = 0 for ver in versions: if (ver.version_code > max_version_code): download_url = ver.version_url max_version_code = ver.version_code if (ver.version_code == version_code and ver.use_flag == 0): force = 1 return (download_url, force)
def get_watch(self, user_id=0, page_num=0, page_size=10): self.__base = BaseDao() op = UserProfile(self.__base) (total, rows) = op.get_watch_by_user(user_id, 30, page_num) is_big_v = False result = [] for row in rows: u = row.toDict() u['is_big_v'] = is_big_v result.append(u) data = {} data['users'] = result data['total'] = len(rows) return data
def get_user_profile_by_id(self, userid): self.__base = self.__base or BaseDao() op = UserProfile(self.__base) rows = op.get_user_profile_by_id(userid) if (len(rows) > 0): return rows[0].toDict()
def get_video_by_user(self, user_id=0, req_user_id=0, begin_sec=0, begin_usec=0, batch_num=30): data = {} data['videos'] = [] data['total'] = 0 data['user_info'] = [] data['is_watch'] = False self.__base = self.__base or BaseDao() try: req_user_info = { 'req_user_id': req_user_id, 'begin_sec': begin_sec, 'begin_usec': begin_usec } res_obj = self.get_video_by_users(user_id=user_id, req_user_ids=[req_user_info], batch_num=batch_num) data['videos'] = res_obj.get('videos') data['total'] = res_obj.get('total') data['user_info'] = res_obj.get('user_info') ps = ProfileService(base=self.__base) is_watch = ps.is_watch(user_id, req_user_id) data['is_watch'] = is_watch except: logger.error(traceback.format_exc()) finally: return data
class ApplicationService(object): def __init__(self): self.__base = None def get_all_app(self): self.__base = BaseDao() app = Application(self.__base) rows = app.get_all_app() data = {} for row in rows: data[row.appid] = row.toDict() return data def close(self): if (self.__base is not None): self.__base.close()
def billboard(): logger.info('rec a billboard request') res = {'code': 1, 'msg': 'success', 'qr': '', 'notice': ''} b = BaseDao() try: bb_dao = Billboard(base=b) bb = bb_dao.get_billboard_info() logger.info('success|billboard request serve success') except: logger.error('fail|exception|billboard request serve error|%s' % traceback.format_exc()) finally: b.close() res['qr'] = bb.qr_url res['notice'] = bb.notice return jsonify(res)
def query_category(): dao = BaseDao() sql = """ select * from v_foods_cate order by cate_name limit %s, 10 """ page_num = request.args.get('page') page_num = int(page_num) if page_num else 1 data = dao.query(sql, (page_num - 1) * 10) total = dao.total('t_foods') return jsonify({ 'data': data, 'total_rows': total, 'total_page': total // 10 + 1 if (total % 10) > 0 else 0 })
def get_watch_video(self, user_id, page_num): self.__base = self.__base or BaseDao() video = Video(self.__base) (total, rows) = video.get_watch_video(user_id, page_num, 30) data = {} data['masters'] = rows data['total'] = total return data
def update_user_profile(self, obj): self.__base = self.__base or BaseDao() import time ts = int(time.time()) sql = """ update t_open_user_profile set nickname=%s,user_icon=%s,sex=%s,update_time=%s where uid=%s and isDiy=0 """ sex = 1 if obj['sex']=='男' else 0 self.__base.exec_w(sql, obj['nickname'], obj['usericon'], sex, ts, obj['openid'])
def get_categorys(self): self.__base = BaseDao() op = Category(self.__base) rows = op.get_all_category() data = [] for row in rows: item = {} item['text'] = row.name item['type'] = row.c_type item['type_id'] = row.type_id item['url'] = read_icon_url_config(row.name) item['url_focus'] = read_icon_url_f_config(row.name) item['url_select'] = read_icon_url_s_config(row.name) data.append(item) result = {} result['navs'] = data return result
def get_watch_all(self, user_id=0): self.__base = self.__base or BaseDao() op = UserProfile(self.__base) (total, rows) = op.get_watch_by_user_all(user_id) result = [] for row in rows: result.append(row.no) return result
class CommentService(object): def __init__(self): self.__base = None def get_base_comment(self, video_type='all'): self.__base = BaseDao() baseComment = BaseComment(self.__base) data = [] result = baseComment.get_comment() for value in result: data.append(value.toDict()) return data def add_video_comment(self, vid, content, userid, comment_time, uname): self.__base = BaseDao() videoComment = VideoComment(self.__base) videoComment.add_video_comment(vid, userid, comment_time, content, uname) def get_video_comment(self, vid): self.__base = BaseDao() videoComment = VideoComment(self.__base) data = videoComment.get_video_comment(vid) if (data): return self._convert_to_xml(data) else: return '' def close(self): if (self.__base is not None): self.__base.close() def _convert_to_xml(self, comments): xml = "<?xml version='1.0' encoding='UTF-8'?><i>{0}</i>" ds = [] for comment in comments: ds.append(comment.toString()) tmp_str = ''.join(ds) return xml.format(tmp_str)