示例#1
0
def torrent(**kwargs):
    magneticod_db = get_magneticod_db()
    context = {}

    try:
        info_hash = bytes.fromhex(kwargs["info_hash"])
        assert len(info_hash) == 20
    except (AssertionError, ValueError
            ):  # In case info_hash variable is not a proper hex-encoded bytes
        return flask.abort(400)

    with magneticod_db:
        cur = magneticod_db.execute(
            "SELECT name, discovered_on FROM torrents WHERE info_hash=? LIMIT 1;",
            (info_hash, ))
        try:
            name, discovered_on = cur.fetchone()
        except TypeError:  # In case no results returned, TypeError will be raised when we try to subscript None object
            return flask.abort(404)

        cur = magneticod_db.execute(
            "SELECT path, size FROM files "
            "WHERE torrent_id=(SELECT id FROM torrents WHERE info_hash=? LIMIT 1);",
            (info_hash, ))
        raw_files = cur.fetchall()
        size = sum(f[1] for f in raw_files)
        files = [File(f[0], utils.to_human_size(f[1])) for f in raw_files]

    context["torrent"] = Torrent(
        info_hash.hex(), name, utils.to_human_size(size),
        datetime.fromtimestamp(discovered_on).strftime("%d/%m/%Y"), files)

    return flask.render_template("torrent.html", **context)
示例#2
0
def newest_torrents():
    magneticod_db = get_magneticod_db()

    page = int(flask.request.args.get("page", 0))

    context = {"page": page}

    with magneticod_db:
        cur = magneticod_db.execute(
            "SELECT "
            "  info_hash, "
            "  name, "
            "  total_size, "
            "  discovered_on "
            "FROM torrents "
            "ORDER BY discovered_on DESC LIMIT 20 OFFSET ?", (20 * page, ))
        context["torrents"] = [
            Torrent(t[0].hex(), t[1], utils.to_human_size(t[2]),
                    datetime.fromtimestamp(t[3]).strftime("%d/%m/%Y"), [])
            for t in cur.fetchall()
        ]

    # noinspection PyTypeChecker
    if len(context["torrents"]) < 20:
        context["next_page_exists"] = False
    else:
        context["next_page_exists"] = True

    return flask.render_template("torrents.html", **context)
示例#3
0
def search_torrents():
    magneticod_db = get_magneticod_db()

    search = flask.request.args["search"]
    page = int(flask.request.args.get("page", 0))

    context = {"search": search, "page": page}

    with magneticod_db:
        cur = magneticod_db.execute(
            "SELECT"
            "    info_hash, "
            "    name, "
            "    total_size, "
            "    discovered_on "
            "FROM torrents "
            "INNER JOIN ("
            "    SELECT docid AS id, rank(matchinfo(fts_torrents, 'pcnxal')) AS rank "
            "    FROM fts_torrents "
            "    WHERE name MATCH ? "
            "    ORDER BY rank ASC"
            "    LIMIT 20 OFFSET ?"
            ") AS ranktable USING(id);", (search, 20 * page))
        context["torrents"] = [
            Torrent(t[0].hex(), t[1], utils.to_human_size(t[2]),
                    datetime.fromtimestamp(t[3]).strftime("%d/%m/%Y"), [])
            for t in cur.fetchall()
        ]

    if len(context["torrents"]) < 20:
        context["next_page_exists"] = False
    else:
        context["next_page_exists"] = True

    return flask.render_template("torrents.html", **context)
示例#4
0
def newest_torrents():
    page = int(flask.request.args.get("page", 0))

    context = {
        "page": page
    }

    with magneticod_db:
        cur = magneticod_db.execute(
            "SELECT "
            "  info_hash, "
            "  name, "
            "  total_size, "
            "  discovered_on "
            "FROM torrents "
            "ORDER BY id DESC LIMIT 20 OFFSET ?",
            (20 * page,)
        )
        context["torrents"] = [Torrent(t[0].hex(), t[1], utils.to_human_size(t[2]), datetime.fromtimestamp(t[3]).strftime("%d/%m/%Y"), [])
                               for t in cur.fetchall()]

    # noinspection PyTypeChecker
    if len(context["torrents"]) < 20:
        context["next_page_exists"] = False
    else:
        context["next_page_exists"] = True

    username, password = flask.request.authorization.username, flask.request.authorization.password
    context["subscription_url"] = "/feed?filter=&hash=%s" % (generate_feed_hash(username, password, ""),)

    return flask.render_template("torrents.html", **context)
示例#5
0
def search_torrents():
    magneticod_db = get_magneticod_db()

    search = flask.request.args["search"]
    page = int(flask.request.args.get("page", 0))

    context = {"search": search, "page": page}
    #searchkey = '%'+search+'%'
    total_page = 20 * page
    with magneticod_db.cursor() as cur:
        sql = "SELECT info_hash,name,total_size,discovered_on FROM torrents where MATCH(name) AGAINST ( '%s' IN BOOLEAN MODE) ORDER BY id DESC LIMIT 20 OFFSET %d;" % (
            search, total_page)
        #cur.execute(
        #    "SELECT"
        #    "    info_hash, "
        #    "    name, "
        #    "    total_size, "
        #    "    discovered_on "
        #    "FROM torrents where name like '%s'"
        #    "ORDER BY discovered_on DESC LIMIT 20 OFFSET '%d';" % \
        #    #"INNER JOIN ("
        #    #"    SELECT torrent_id, rank(matchinfo(fts_torrents, 'pcnxal')) AS rank "
        #    #"    FROM fts_torrents "
        #    #"    WHERE name MATCH ? "
        #    #"    ORDER BY rank ASC"
        #    #"    LIMIT 20 OFFSET ?"
        #    #") AS ranktable ON torrents.id=ranktable.torrent_id;",
        #    (searchkey, total_page)
        #)
        print(sql)
        cur.execute(sql)
        context["torrents"] = [
            Torrent(t[0], t[1], utils.to_human_size(t[2]),
                    datetime.fromtimestamp(t[3]).strftime("%d/%m/%Y"), [])
            for t in cur.fetchall()
        ]

    if len(context["torrents"]) < 20:
        context["next_page_exists"] = False
    else:
        context["next_page_exists"] = True

    return flask.render_template("torrents.html", **context)
示例#6
0
def torrents():
    search = flask.request.args.get("search")
    page = int(flask.request.args.get("page", 0))

    context = {"search": search, "page": page}

    SQL_query = """
        SELECT
            info_hash,
            name,
            total_size,
            discovered_on
        FROM torrents
    """
    if search:
        SQL_query += """
            INNER JOIN (
                SELECT docid AS id, rank(matchinfo(fts_torrents, 'pcnxal')) AS rank
                FROM fts_torrents
                WHERE name MATCH ?
            ) AS ranktable USING(id)
        """
    SQL_query += """
        ORDER BY {}
        LIMIT 20 OFFSET ?
    """

    sort_by = flask.request.args.get("sort_by")
    allowed_sorts = [
        None, "name ASC", "name DESC", "total_size ASC", "total_size DESC",
        "discovered_on ASC", "discovered_on DESC"
    ]
    if sort_by not in allowed_sorts:
        return flask.Response(
            "Invalid value for `sort_by! (Allowed values are %s)" %
            (allowed_sorts, ), 400)

    if search:
        if sort_by:
            SQL_query = SQL_query.format(sort_by + ", " + "rank ASC")
        else:
            SQL_query = SQL_query.format("rank ASC")
    else:
        if sort_by:
            SQL_query = SQL_query.format(sort_by + ", " + "id DESC")
        else:
            SQL_query = SQL_query.format("id DESC")

    with magneticod_db:
        if search:
            cur = magneticod_db.execute(SQL_query, (search, 20 * page))
        else:
            cur = magneticod_db.execute(SQL_query, (20 * page, ))
        context["torrents"] = [
            Torrent(t[0].hex(), t[1], utils.to_human_size(t[2]),
                    datetime.fromtimestamp(t[3]).strftime("%d/%m/%Y"), [])
            for t in cur.fetchall()
        ]

    if len(context["torrents"]) < 20:
        context["next_page_exists"] = False
    else:
        context["next_page_exists"] = True

    if app.arguments.noauth:
        context["subscription_url"] = "/feed/?filter%s" % search
    else:
        username, password = flask.request.authorization.username, flask.request.authorization.password
        context["subscription_url"] = "/feed?filter=%s&hash=%s" % (
            search, generate_feed_hash(username, password, search))

    if sort_by:
        context["sorted_by"] = sort_by

    return flask.render_template("torrents.html", **context)