This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
70 lines (59 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"net/http"
"fmt"
"io/ioutil"
"github.com/mvdan/xurls"
"strconv"
"strings"
)
func buscaUrls(body string) []string {
return xurls.Strict.FindAllString(body, -1)
}
func getBody(url string) string {
resp, err := http.Get(url)
if err != nil {
println("deu erro")
return ""
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
stringBody := string(body)
return stringBody
}
func main(){
url := "http://g1.globo.com/"
body := getBody(url)
urls := buscaUrls(body)
var stringUrls string
for i:=0; i < len(urls); i++ {
stringUrls = stringUrls + "\n" + urls[i]
}
arquivo := "urls.txt"
f, err := Open(arquivo)
bytesWritten, err := f.WriteString(stringUrls)
fmt.Printf("%d bytes\n", bytesWritten)
if err != nil {
fmt.Println("Vish, deu problema!")
}
defer f.Close()
urlsRetornadas := GetUrlsFromFile(arquivo)
fmt.Println(urlsRetornadas)
for j:=0;j<len(urlsRetornadas);j++{
if strings.HasSuffix(urlsRetornadas[j], "jpeg"){
nomeArquivo:= strconv.Itoa(j) + ".jpeg"
f, err = Open(nomeArquivo)
if err != nil{
fmt.Println("Deu ruim!")
}else{
bytesWritten, err = f.WriteString(getBody(urlsRetornadas[j]))
fmt.Printf("%d bytes\n", bytesWritten)
if err != nil {
fmt.Println("Vish, deu problema!")
fmt.Println(err)
}
defer f.Close()
}
}
}
}