|
本帖最后由 DDD888 于 2-12-2014 20:09 编辑
刚学golang,试着写一个,练练手啦
只要装了go,应该在windows,linux下可以运行 我只测试了windows 7, ubuntu linux 14.10
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
func init() {
log.SetFlags(log.Lshortfile)
}
func getFileName(url string) (string){
fName := filepath.Base(url)
extName := filepath.Ext(url)
bName := fName[:len(fName)-len(extName)]
return bName
}
func downloadFromUrl(url string) {
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
// TODO: check file existence first with io.IsExist
output, err := os.Create(fileName)
if err != nil {
fmt.Println("Error while creating", fileName, "-", err)
return
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
fmt.Println(n, "bytes downloaded.")
}
func downloadUrl(regularExpression *regexp.Regexp, websiteUrl string) {
response, err := http.Get(websiteUrl)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
var htmlContents = string(contents)
matches := regularExpression.FindAllString(htmlContents, -1)
for _, mp3Url := range matches {
fmt.Println(mp3Url)
var mp3FileName = getFileName(mp3Url)
if _, err := os.Stat(mp3FileName); os.IsNotExist(err) {
//fmt.Printf("no such file or directory: %s", filename)
downloadFromUrl(mp3Url)
}
}
}
}
func main() {
// Open an input file, exit on error.
inputFile, err := os.Open("bbc.txt")
if err != nil {
log.Fatal("Error opening input file:", err)
}
var regularExpression = regexp.MustCompile("http://downloads.bbc.co.uk/podcasts/[^.]*.mp3")
// Closes the file when we leave the scope of the current function,
// this makes sure we never forget to close the file if the
// function can exit in multiple places.
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
// scanner.Scan() advances to the next token returning false if an error was encountered
for scanner.Scan() {
var websiteUrl = scanner.Text()
fmt.Println(websiteUrl)
downloadUrl(regularExpression, websiteUrl)
}
// When finished scanning if any error other than io.EOF occured
// it will be returned by scanner.Err().
if err := scanner.Err(); err != nil {
log.Fatal(scanner.Err())
}
} |
|