Exemple #1
0
def video_play(video_dir):
    db = get_db()
    error = None

    if request.method == "POST":
        if g.user:
            body = request.form["body"]

            if not body:
                error = "请评论后再提交!"
            else:
                db = get_db()
                video = db.execute(
                    "SELECT * FROM video_info WHERE video_dir = ?",
                    (video_dir, )).fetchone()
                db.execute(
                    "INSERT INTO comment (body,author_id,video_id)"
                    " VALUES (?,?,?)", (body, g.user["id"], video["id"]))
                db.commit()
                return redirect(
                    url_for("models.video_play", video_dir=video_dir))
        else:
            error = "请先登录后再评论!"
        if error is not None:
            flash(error)

    comments = None
    video = db.execute(
        "SELECT id,video_name,video_type,video_dir FROM video_info"
        " WHERE video_dir = ?", (video_dir, )).fetchone()
    if db.execute("SELECT id FROM comment WHERE video_id=?",
                  (video["id"], )).fetchone is not None:
        comments = db.execute(
            """SELECT c.id,body,datetime(created,"localtime"),author_id,video_id,username,icon
              FROM comment c JOIN user u ON c.author_id = u.id
              WHERE video_id = ?
              ORDER BY created ASC""", (video["id"], )).fetchall()

    total = db.execute("SELECT COUNT(*) n FROM video_info").fetchone()
    random_int = random.sample(range(total["n"]), 3)

    recommend = []
    for i in random_int:
        random_video = db.execute("SELECT * FROM video_info WHERE id = ?",
                                  (i, )).fetchone()
        recommend.append(random_video)

    db.execute(
        "UPDATE video_info SET video_view_times = video_view_times + 1"
        " WHERE video_dir = ?", (video_dir, ))
    db.commit()
    return render_template("models/video_play.html",
                           video=video,
                           comments=comments,
                           recommend=recommend)
Exemple #2
0
def profile():
    if request.method == "POST":
        db = get_db()
        username = request.form["username"]
        password = request.form["password"]
        f = request.files["file"]
        icon = secure_filename(f.filename)
        suffix = (".jpg", ".bmp", ".jpeg", ".png", ".gif")
        error = None

        if not username:
            username = g.user["username"]
        if db.execute("SELECT id FROM user WHERE username = ?",
                      (username, )).fetchone() is not None:
            error = "用户名 {0} 已被注册!".format(username)
        if not password:
            password = g.user["password"]
        if not icon:
            icon = g.user["icon"]
        else:
            if not icon.endswith(suffix):
                error = "请上传指定格式的头像文件!"
            else:
                f.save(".\\static\\icon\\" + username + icon)
                icon = username + icon
        if error is None:
            db.execute(
                "INSERT INTO user (username,password,icon)"
                " VALUES (?,?,?)", (username, password, icon))
            db.commit()
        return redirect(url_for("auth.login"))

    return render_template("auth/profile.html")
Exemple #3
0
def load_logged_in_user():
    # 如果用户id在会话中,从数据库将用户对象导入
    # 每个请求之前都会运行此函数
    user_id = session.get("user_id")  # 会话跨请求存在

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute("SELECT * FROM user WHERE id = ?",
                                  (user_id, )).fetchone()
Exemple #4
0
def discover():
    db = get_db()

    total = db.execute("SELECT COUNT(*) n FROM video_info").fetchone()
    random_int = random.sample(range(total["n"]), 10)

    discover = []
    for i in random_int:
        random_video = db.execute("SELECT * FROM video_info WHERE id = ?",
                                  (i, )).fetchone()
        discover.append(random_video)

    return render_template("models/discover.html", discover=discover)
Exemple #5
0
def video_update(video_dirs=()):
    db = get_db()
    for video_dir in video_dirs:
        video_list = get_video_name(video_dir)
        subdir = os.listdir(video_dir)
        videos = list(zip(video_list, subdir))

        for video in videos:
            if db.execute("SELECT * FROM video_info WHERE video_dir = ?",
                          (video[1], )).fetchone() is None:
                video_type = re.search(r".*video\\(.*)", video_dir).group(1)

                db.execute(
                    "INSERT INTO video_info (video_name,video_dir,video_type)"
                    " VALUES (?,?,?)", (video[0], video[1], video_type))
                db.commit()
Exemple #6
0
def search():
    db = get_db()
    content = (request.args.get("keyword")).upper()
    print(content)
    video_list = []
    for dir in [
            ".\\blue7accoon\\static\\video\\other",
            ".\\blue7accoon\\static\\video\\trailer",
            ".\\blue7accoon\\static\\video\\edm",
            ".\\blue7accoon\\static\\video\\new"
    ]:
        for item in get_video_name(dir):
            if item.upper().find(content) > -1:
                video_list.append(item)

    rst = []
    for item in video_list:
        video = db.execute("SELECT * FROM video_info WHERE video_name = ?",
                           (item, )).fetchone()
        rst.append(video)
    return render_template("models/search-result.html", rst=rst)
Exemple #7
0
def login(id=None):
    # 将用户id加入会话来进行登录
    if request.method == "POST":
        db = get_db()
        username = request.form["username"]
        password = request.form["password"]
        error = None
        user = db.execute("SELECT * FROM user WHERE username = ?",
                          (username, )).fetchone()
        if id is not None:
            if check_password_hash(id, username + "eu343hds"):
                db.execute(
                    "UPDATE user SET email_validation = 1"
                    " WHERE username = ?", (username, ))
                db.commit()
            else:
                return render_template("404.html")
        if not username:
            error = "请输入用户名!"
        elif not password:
            error = "请输入密码!"
        elif user is None:
            error = "用户名不存在!"
        elif not check_password_hash(user["password"],
                                     username + password + "eu72dj"):
            error = "密码错误!"
        elif user["email_validation"] < 1:
            error = "初次登录请先验证邮箱!"
        if error is None:
            # 使用用户id创建会话并返回首页
            session.clear()
            session["user_id"] = user["id"]
            return redirect(url_for("models.index"))

        flash(error)

    return render_template("auth/login.html")
Exemple #8
0
def register():
    # 注册新用户
    # 确保用户名唯一,存储密码的哈希值
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        rePassword = request.form["rePassword"]
        email = request.form["email"]

        db = get_db()
        error = None

        if not username:
            error = "未输入用户名!"
        elif not password:
            error = "未输入密码!"
        elif not rePassword:
            error = "请确认密码!"
        elif not email:
            error = "请输入电子邮箱地址!"
        elif len(username) < 2:
            error = "用户名不可少于两个字符!"
        elif db.execute("SELECT id FROM user WHERE username = ?",
                        (username, )).fetchone() is not None:
            error = "用户名 {0} 已被注册!".format(username)
        elif password != rePassword:
            error = "两次输入的密码必须一致!"
        elif db.execute("SELECT id FROM user WHERE email = ?",
                        (email, )).fetchone() is not None:
            error = "同一邮箱只可注册一个帐号!"
        if error is None:
            # 将用户名和密码哈希值存储在数据库中并发送验证邮件之后跳转到登录界面
            db.execute(
                "INSERT INTO user (username,password,email) VALUES (?,?,?)",
                (username,
                 generate_password_hash(username + password + "eu72dj"),
                 email))
            db.commit()

            msg = MIMEMultipart()
            msg["From"] = _format_addr("blue7accoon <*****@*****.**>")
            msg["To"] = _format_addr("<%s>" % email)
            msg["Subject"] = Header("欢迎您加入blue7accoon!请验证邮箱地址!",
                                    "utf-8").encode()

            # 邮件正文-html
            msg.attach(
                MIMEText(
                    """<html><body><h3>您好!{0}</h3>
                                <p>欢迎加入blue7accoon!</p>
                                <p>请点击下方链接完成邮箱验证:</p>
                                <p><a href='http://127.0.0.1:5000/auth/login/{1}'>点击此处链接完成邮箱验证<a></p>
                                <p>如果您没有注册blue7accoon帐号,请忽略这封邮件</p>
                                </body></html>""".format(
                        username,
                        generate_password_hash(username + "eu343hds")), "html",
                    "utf-8"))

            server = smtplib.SMTP(smtp_server, 587)
            server.set_debuglevel(1)
            server.login("*****@*****.**", email_password)
            server.sendmail("*****@*****.**", [email], msg.as_string())
            server.quit()
            return redirect(url_for("auth.login"))

        flash(error)

    return render_template("auth/register.html")
Exemple #9
0
def billboard():
    db = get_db()
    data = db.execute(
        "SELECT * FROM video_info ORDER BY video_view_times DESC").fetchall()
    return render_template("models/billboard.html", data=data)
Exemple #10
0
def trailer():
    db = get_db()
    video = db.execute(
        "SELECT * FROM video_info WHERE video_type = 'trailer'").fetchall()
    return render_template("models/trailer.html", video=video)
Exemple #11
0
def edm():
    db = get_db()
    video = db.execute(
        "SELECT * FROM video_info WHERE video_type = 'edm'").fetchall()
    return render_template("models/edm.html", video=video)