利用anacrolix/torrent批量下載種子文件

Torrent.go

package main

import (
    "fmt"
    "github.com/anacrolix/torrent"
    "io/fs"
    "log"
    "os"
    "path/filepath"
    "strings"
)

func getFileList(path string, prefix string)  []string{
    var files []string
    err := filepath.Walk(path, func(path string, f fs.FileInfo, err error) error {
        if f == nil{
            return err
        }
        if f.IsDir() {
            return nil
        }
        comma := strings.LastIndex(path,"_") +1
        name := path[comma:]
        if strings.HasPrefix(name, prefix) && !strings.HasSuffix(name, "@SynoEAStream"){
            fmt.Println(name)
            files = append(files, path)
        }

        return nil
    })
    if err != nil{
        fmt.Printf("filepath.Walk() returned %v\n", err)
    }
    return files
}

func main() {
    downPath := os.Getenv("DOWNLOAD_PATH")
    if len(downPath) == 0 {
        downPath = "D:\\download"
    }
    prefix := os.Getenv("PREFIX")
    if len(prefix) == 0 {
        prefix = "03"
    }
    torrentPath := os.Getenv("TORRENT_PATH")
    if len(torrentPath) == 0 {
        torrentPath = downPath + "/torrent"
    }
    log.Printf("prefix:%s,torrentPath:%s,downPath:%s\n", prefix, torrentPath, downPath)
    _, err := os.Stat(downPath)
    if err != nil && os.IsNotExist(err) {
        os.MkdirAll(downPath, 0666)
    }

    files := getFileList(torrentPath, prefix)

    config := torrent.NewDefaultClientConfig()
    config.DataDir = downPath
    c, _ := torrent.NewClient(config)
    defer c.Close()
    for _, path := range files{
        fmt.Println(path)
        t, _ := c.AddTorrentFromFile(path)
        <-t.GotInfo()
        t.DownloadAll()
        c.WaitAll()
    }

}




Dockfile

FROM golang:stretch

MAINTAINER panxi "7066450@qq.com"

ENV GO111MODULE=on \
    GOPROXY=https://goproxy.cn,direct \
    GIN_MODE=release

RUN apt install gcc

WORKDIR $GOPATH/src/torrent

ADD . ./

ARG GO_MAIN=Torrent.go

RUN go build -o torrent Torrent.go

ENTRYPOINT  ["./torrent"]

K8S Job

apiVersion: batch/v1
kind: Job
metadata:
  #{{}}模板變量括號(hào)中間不能有空格
  name: torrent-{{prefix}}
  namespace: dev
spec:
  ttlSecondsAfterFinished: 10 #1.21版本以下需要啟用api-server feature-gates:TTLAfterFinished=true
  template:
    spec:
      containers:
        - name: torrent-{{prefix}}
          image: harbor.lab/apps/torrent
          volumeMounts:
            - mountPath: "/storage"
              name: storage
          imagePullPolicy: Always
          env:
            - name: DOWNLOAD_PATH
              value:  "/storage/{{prefix}}"
            - name: PREFIX
              value: "{{prefix}}"
            - name: TORRENT_PATH
              value: "{{torrentPath}}"
      imagePullSecrets:
        - name: docker-secret
      restartPolicy: Never
      volumes:
        - name: storage
          persistentVolumeClaim:
            claimName: docs-storageclass

Java調(diào)用k8s API發(fā)布job

@Override
    public void createDocTorrentResolve(String prefix, String torrentPath) throws IOException {
        List<V1Job> jobs =  clusterService.listJob("job-name=torrent-" + prefix);
        if(!ObjectUtils.isEmpty(jobs)){
            for (V1Job job : jobs) {
                V1JobStatus status = job.getStatus();
                if(status != null
                        && status.getSucceeded() != null
                        && 1 == status.getSucceeded()){
                    V1ObjectMeta v1ObjectMeta = job.getMetadata();
                    if(v1ObjectMeta != null){
                        clusterService.deleteJob(v1ObjectMeta.getName());
                    }
                }else{
                    throw new ArgumentException("存在未完成任務(wù),無(wú)法創(chuàng)建文獻(xiàn)種子文件下載任務(wù)!");
                }
            }
        }
        clusterService.applyYaml(
                "torrent.yaml",
                new HashedMap<String, String>() {
                    {
                        put("prefix", prefix);
                        put("torrentPath", torrentPath);
                    }
                });
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容