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()
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 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 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 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()
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)
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)
class MusicService(object): def __init__(self): self.__base = None 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 close(self): if (self.__base is not None): self.__base.close()
class VersionService(object): def __init__(self): self.__base = None 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 close(self): if (self.__base is not None): self.__base.close()
class WatchService(object): def __init__(self, base=None): self.__base = base 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 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_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_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 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 is_watch(self, userid1, userid2): self.__base = BaseDao() op = UserProfile(self.__base) return op.is_watch(userid1, userid2) def user_watch_relation(self, userid1, userid2): data = {} data['is_watch'] = self.is_watch(userid1, userid2) return data def _get_message(self): dic = {} dic['praise'] = 3 dic['watch'] = 8 dic['cluster'] = 6 dic['system'] = 2 return dic def close(self): if (self.__base is not None): self.__base.close() self.__base = None