Created
March 20, 2023 03:18
-
-
Save siddontang/4e58529f474789473fe8abb85c4fa821 to your computer and use it in GitHub Desktop.
Download Baidu Haokan video
This file contains 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" | |
"io" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
) | |
func downloadVideo(urlStr string) error { | |
headers := map[string]string{ | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299", | |
"Cache-Control": "no-cache", | |
"Pragma": "no-cache", | |
"Expires": "0", | |
} | |
// Make GET request to the URL | |
req, err := http.NewRequest("GET", urlStr, nil) | |
if err != nil { | |
return err | |
} | |
for key, value := range headers { | |
req.Header.Set(key, value) | |
} | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
// Read response body | |
bodyBytes, err := io.ReadAll(resp.Body) | |
if err != nil { | |
return err | |
} | |
bodyStr := string(bodyBytes) | |
// Find the "playurl" in the response body | |
playurlStart := strings.Index(bodyStr, `"playurl":"`) + len(`"playurl":"`) | |
playurlEnd := strings.Index(bodyStr[playurlStart:], `","`) + playurlStart | |
playurl := strings.ReplaceAll(bodyStr[playurlStart:playurlEnd], `\/`, `/`) | |
fmt.Println("playurl:", playurl) | |
// Parse the playurl to get the filename | |
u, err := url.Parse(playurl) | |
if err != nil { | |
return err | |
} | |
pathSegments := strings.Split(u.Path, "/") | |
filename := pathSegments[len(pathSegments)-1] | |
// Create the file and download the video | |
out, err := os.Create(filename) | |
if err != nil { | |
return err | |
} | |
defer out.Close() | |
resp, err = http.Get(playurl) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
_, err = io.Copy(out, resp.Body) | |
if err != nil { | |
return err | |
} | |
fmt.Println("Downloaded file:", filename) | |
return nil | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("Please provide the URL as an argument.") | |
os.Exit(1) | |
} | |
url := os.Args[1] | |
err := downloadVideo(url) | |
if err != nil { | |
fmt.Println("Error:", err) | |
os.Exit(1) | |
} | |
} |
This file contains 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
import requests | |
import sys | |
from urllib.parse import urlparse | |
def download_video(url): | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' | |
} | |
response = requests.get(url, headers=headers) | |
playurl_start = response.text.find('"playurl":"') + len('"playurl":"') | |
playurl_end = response.text.find('","', playurl_start) | |
playurl = response.text[playurl_start:playurl_end] | |
playurl = playurl.replace('\\/', '/') | |
print("playurl", playurl) | |
playurl_path = urlparse(playurl).path | |
filename = playurl_path.split('/')[-1] | |
with open(filename, 'wb') as f: | |
video = requests.get(playurl) | |
print("begin to download to file", filename) | |
f.write(video.content) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print('Please provide the URL as an argument.') | |
sys.exit(1) | |
url = sys.argv[1] | |
download_video(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment