Created
June 5, 2025 05:20
-
-
Save Silica163/8067a02d0781014de6f9b51612a60c3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| "flag" | |
| "path" | |
| "strings" | |
| ) | |
| var user_agent string | |
| var referrer string | |
| type FileInfo struct { | |
| name string | |
| link string | |
| } | |
| func download_file_thread(thread_id int, file_infos <- chan FileInfo, done chan <- string) { | |
| for file_info := range file_infos { | |
| fmt.Printf("[%02d][DOWNLOAD] \"%s\"\n", thread_id, file_info.name) | |
| retries := 0 | |
| for !download_file(file_info.name, file_info.link) || retries > 10 { | |
| fmt.Printf("[%02d][RETRY] \"%s\"\n", thread_id, file_info.name) | |
| retries ++ | |
| } | |
| msg := fmt.Sprintf("[%02d][DONE] \"%s\"\n", thread_id, file_info.name) | |
| if retries > 10 { | |
| msg = fmt.Sprintf("[%02d][FAILED] \"%s\"\n", thread_id, file_info.name) | |
| } | |
| fmt.Printf("%s", msg) | |
| done <- msg | |
| } | |
| } | |
| func download_file(name string, url string) bool { | |
| arg := [] string {"curl"} | |
| if user_agent != "" { | |
| arg = append(arg, "-A", user_agent) | |
| } | |
| if referrer != "" { | |
| arg = append(arg, "-e", referrer) | |
| } | |
| arg = append(arg, "-so", name, url) | |
| attr := new(os.ProcAttr) | |
| attr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr} | |
| proc , err := os.StartProcess("/usr/bin/curl", arg, attr) | |
| if err != nil { | |
| fmt.Println(err); | |
| return false | |
| } | |
| proc_state, err := proc.Wait() | |
| if err != nil { | |
| fmt.Println(err); | |
| return false | |
| } | |
| return proc_state.Success() | |
| } | |
| func flag_IsSet(name string) bool { | |
| isSet := false | |
| flag.Visit(func (f * flag.Flag){ | |
| isSet = f.Name == name | |
| }) | |
| return isSet | |
| } | |
| func main (){ | |
| user_agent = os.Getenv("USER_AGENT") | |
| pwd := os.Getenv("PWD") | |
| flag.StringVar(&referrer, "r", "", "Referrer.") | |
| series_name := flag.String ("n", path.Base(pwd), "Name of the series.") | |
| link_file_path := flag.String ("l", "links", "File that contains links") | |
| threads := flag.Int ("t", 4, "Download threads.") | |
| auto_run := flag.Bool ("auto", false, "Require when to run with default SERIES_NAME and LINK_FILE_PATH. save file to dir_name.EXX.mp4") | |
| flag.Parse() | |
| if !*auto_run && !(flag_IsSet("n") && flag_IsSet("l")){ | |
| fmt.Println("auto flag is require to run with default SERIES_NAME and LINK_FILE_PATH.") | |
| flag.Usage() | |
| os.Exit(1) | |
| } | |
| if *series_name == "" { | |
| fmt.Println("SERIES_NAME must not empty.") | |
| flag.Usage() | |
| os.Exit(1) | |
| } | |
| if *link_file_path == "" { | |
| fmt.Println("LINK_FILE_PATH must not empty.") | |
| flag.Usage() | |
| os.Exit(1) | |
| } | |
| link_file, err := os.ReadFile(*link_file_path) | |
| if err != nil { | |
| fmt.Println(err); | |
| os.Exit(1) | |
| } | |
| links := strings.Split(string(link_file),"\n") | |
| link_count := len(links) | |
| if links[link_count - 1] == "" { | |
| link_count -- | |
| } | |
| file_infos := make(chan FileInfo, link_count) | |
| done := make(chan string, link_count) | |
| for i := 0 ; i < *threads ; i++ { | |
| go download_file_thread(i, file_infos, done) | |
| } | |
| downloadable_count := 0 | |
| for i := range link_count { | |
| link := links[i] | |
| if link == "" { continue } | |
| downloadable_count ++ | |
| name := fmt.Sprintf("%s.E%02d.mp4", *series_name, i + 1) | |
| fmt.Printf("[QUEUE] \"%s\"\n", name) | |
| file_infos <- FileInfo{ | |
| name, | |
| link, | |
| } | |
| } | |
| close(file_infos) | |
| for i := 0 ; i < downloadable_count ; i ++{ | |
| <- done | |
| // msg := <- done | |
| // fmt.Printf("%s",msg) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment