Skip to content

Instantly share code, notes, and snippets.

@qinghon
Last active October 16, 2021 14:50
Show Gist options
  • Select an option

  • Save qinghon/61e3b95799211ba11b9e6b0ba285b2aa to your computer and use it in GitHub Desktop.

Select an option

Save qinghon/61e3b95799211ba11b9e6b0ba285b2aa to your computer and use it in GitHub Desktop.

Revisions

  1. qinghon revised this gist Oct 16, 2021. 1 changed file with 57 additions and 0 deletions.
    57 changes: 57 additions & 0 deletions organize_torrents.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    #!/usr/bin/env python3
    # 整理已经下载好的torrent,
    # usage:
    # depends: `pip install qbittorrent-api`
    # step1: create directory for single file torrent file prefix in `DIST_PATH`
    # step2: python3 organize_torrents.py
    # ps: only support single file torrent

    import os
    import sys

    import qbittorrentapi

    HOST = '127.0.0.1'
    PORT = 8080
    USERNAME = 'admin'
    PASSWORD = ''

    DIST_PATH = '/home/database/Download/'



    # instantiate a Client using the appropriate WebUI configuration
    qbt_client = qbittorrentapi.Client(host=HOST, port=PORT, username=USERNAME, password=PASSWORD)


    # the Client will automatically acquire/maintain a logged in state in line with any request.
    # therefore, this is not necessary; however, you may want to test the provided login credentials.


    def list_path(p):
    res = []
    for home, dirs, files in os.walk(p):
    for dir in dirs:
    res.append(dir)
    return res


    if __name__ == '__main__':
    try:
    qbt_client.auth_log_in()
    except qbittorrentapi.LoginFailed as e:
    print(e)
    exit(0)

    dist_paths = list_path(DIST_PATH)
    for torrent in qbt_client.torrents_info():
    if torrent['category'] == "new" and torrent['save_path'] == DIST_PATH:
    if os.path.isfile(torrent['content_path']):
    file_name = os.path.basename(torrent['content_path'])
    torrent_hash = torrent['hash']
    for p in dist_paths:
    if file_name.startswith(p):
    qbt_client.torrents_set_location(os.path.join(DIST_PATH, p), torrent_hashes=torrent_hash)
    print("set location {0}".format(os.path.join(DIST_PATH, p)))

    exit(0)
  2. qinghon created this gist Jun 13, 2021.
    64 changes: 64 additions & 0 deletions move_bt_file.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/usr/bin/env python3

    # usage:
    # depends: `pip install qbittorrent-api`
    # step1: set `/path/to/move_bt_file.py "%C" "%I" "%F"` to qbittorent "Run external program on torrent completion"
    # step2: create directory for single file torrent file prefix in `DIST_PATH`
    # ps: only support single file torrent

    import os
    import sys

    import qbittorrentapi

    HOST = '127.0.0.1'
    PORT = 8080
    USERNAME = 'admin'
    PASSWORD = ''

    DIST_PATH = '/home/database/Download/'



    # instantiate a Client using the appropriate WebUI configuration
    qbt_client = qbittorrentapi.Client(host=HOST, port=PORT, username=USERNAME, password=PASSWORD)


    # the Client will automatically acquire/maintain a logged in state in line with any request.
    # therefore, this is not necessary; however, you may want to test the provided login credentials.


    def list_path(p):
    res = []
    for home, dirs, files in os.walk(p):
    for dir in dirs:
    res.append(dir)
    return res


    if __name__ == '__main__':
    num_file_str = sys.argv[1] # %C
    torrent_hash = sys.argv[2] # %I
    file_path = sys.argv[3] # %F
    if not num_file_str or not torrent_hash:
    print("ars not vaild")
    exit(0)
    num_file = int(num_file_str)
    if num_file != 1:
    print("not a single file torrent")
    exit(0)
    file_name = os.path.basename(file_path)
    try:
    qbt_client.auth_log_in()
    except qbittorrentapi.LoginFailed as e:
    print(e)
    exit(0)

    dist_paths = list_path(DIST_PATH)
    for p in dist_paths:
    if file_name.startswith(p):
    qbt_client.torrents_set_location(os.path.join(DIST_PATH, p), torrent_hashes=torrent_hash)
    print("set location %s", os.path.join(DIST_PATH, p))
    break

    exit(0)