mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-12-01 18:41:24 +01:00
v4.5
This commit is contained in:
1 parent
84b067ebd4
commit
e67d09a248
47 files changed
+3688
-958
No files matched your search
+62
-13
@@ -6,6 +6,8 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -20,14 +22,42 @@ func GetDefaultDownloadPath() string {
|
||||
return filepath.Join(homeDir, "Pictures")
|
||||
}
|
||||
|
||||
func GetProxyURL(customProxy string) (*url.URL, error) {
|
||||
func parseProxyURLs(rawValue string, sourceName string) ([]*url.URL, error) {
|
||||
if strings.TrimSpace(rawValue) == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
parts := strings.Split(rawValue, ",")
|
||||
proxyURLs := make([]*url.URL, 0, len(parts))
|
||||
|
||||
for _, part := range parts {
|
||||
trimmed := strings.TrimSpace(part)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
proxyURL, err := url.Parse(trimmed)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid proxy URL in %s: %v", sourceName, err)
|
||||
}
|
||||
if proxyURL.Scheme == "" || proxyURL.Host == "" {
|
||||
return nil, fmt.Errorf("invalid proxy URL in %s: %q", sourceName, trimmed)
|
||||
}
|
||||
|
||||
proxyURLs = append(proxyURLs, proxyURL)
|
||||
}
|
||||
|
||||
if len(proxyURLs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return proxyURLs, nil
|
||||
}
|
||||
|
||||
func GetProxyURLs(customProxy string) ([]*url.URL, error) {
|
||||
|
||||
if customProxy != "" {
|
||||
proxyURL, err := url.Parse(customProxy)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid custom proxy URL: %v", err)
|
||||
}
|
||||
return proxyURL, nil
|
||||
return parseProxyURLs(customProxy, "custom proxy setting")
|
||||
}
|
||||
|
||||
proxyEnv := os.Getenv("HTTPS_PROXY")
|
||||
@@ -42,24 +72,43 @@ func GetProxyURL(customProxy string) (*url.URL, error) {
|
||||
}
|
||||
|
||||
if proxyEnv != "" {
|
||||
proxyURL, err := url.Parse(proxyEnv)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid proxy URL from environment: %v", err)
|
||||
}
|
||||
return proxyURL, nil
|
||||
return parseProxyURLs(proxyEnv, "proxy environment variable")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetProxyURL(customProxy string) (*url.URL, error) {
|
||||
proxyURLs, err := GetProxyURLs(customProxy)
|
||||
if err != nil || len(proxyURLs) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
return proxyURLs[0], nil
|
||||
}
|
||||
|
||||
func buildProxySelector(proxyURLs []*url.URL) func(*http.Request) (*url.URL, error) {
|
||||
if len(proxyURLs) == 0 {
|
||||
return nil
|
||||
}
|
||||
if len(proxyURLs) == 1 {
|
||||
return http.ProxyURL(proxyURLs[0])
|
||||
}
|
||||
|
||||
var requestCount uint64
|
||||
return func(*http.Request) (*url.URL, error) {
|
||||
index := atomic.AddUint64(&requestCount, 1) - 1
|
||||
return proxyURLs[index%uint64(len(proxyURLs))], nil
|
||||
}
|
||||
}
|
||||
|
||||
func CreateHTTPClient(customProxy string, timeout time.Duration) (*http.Client, error) {
|
||||
proxyURL, err := GetProxyURL(customProxy)
|
||||
proxyURLs, err := GetProxyURLs(customProxy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
Proxy: buildProxySelector(proxyURLs),
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
|
||||
Reference in new issue
Block a user