forked from labulakalia/ibm_bak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibm_bak.go
142 lines (124 loc) · 4.39 KB
/
ibm_bak.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
func savePage(url string) error {
//url := "https://developer.ibm.com/zh/articles/db-redis-from-basic-to-advanced-01/"
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return err
}
title := doc.Find("#content > div.page_content > div > div.code_postcontent > div.cpt-hero > div > div > div > div > h1")
title2 := doc.Find("#content > div.page_content > div > div.code_postcontent > div.cpt-hero > div > div > div > div > h2")
content := doc.Find("#content > div.page_content > div > div.code_postcontent > div.cpt-body > div > div > div.bx--col-md-7.bx--col-lg-7")
tag := doc.Find("#pane1")
converter := md.NewConverter("", true, nil)
converter.Before(func(selec *goquery.Selection) {
selec.Find("img").Each(func(i int, s *goquery.Selection) {
_, ok := s.Attr("src")
if ok {
return
}
_, err = os.Stat("ibm_articles_img")
if os.IsNotExist(err) {
err = os.Mkdir("ibm_articles_img", 0755)
if err != nil {
log.Println(err)
return
}
}
src, ok := s.Attr("data-src")
if !ok {
return
}
resp, err := http.Get(src)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
img_path := "ibm_articles_img/" + strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(src, "https://developer.ibm.com/developer/default/articles/", ""), "/", "_"), "images/", "")
// os.OpenFile(img_path, os, perm os.FileMode)
// "https://developer.ibm.com/developer/default/articles/images/policy-based-governance-security.png"
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
ioutil.WriteFile(img_path, data, 0644)
s.SetAttr("src", "../"+img_path)
})
})
tags := []string{}
tag.Find("a").Each(func(i int, s *goquery.Selection) {
tags = append(tags, s.Text())
})
mdcontent := fmt.Sprintf("# %s\n%s\n\n**标签:** %s\n\n[原文链接](%s)\n\n", title.Text(), title2.Text(), strings.Join(tags, ","), url)
mdcontent += converter.Convert(content)
_, err = os.Stat("ibm_articles")
if os.IsNotExist(err) {
err = os.Mkdir("ibm_articles", 0755)
if err != nil {
return err
}
}
t := strings.ReplaceAll(title.Text(), "/", "_")
t = strings.ReplaceAll(t, " ", "")
return ioutil.WriteFile(fmt.Sprintf("ibm_articles/%s.md", t), []byte(mdcontent), 0644)
}
func SavePages(urls chan string) {
for url := range urls {
log.Printf("start save page url:%s\n", url)
err := savePage(url)
if err != nil {
log.Printf("save url:%s failed: %v\n", url, err)
}
log.Println("success save")
}
}
func main() {
var urls = make(chan string)
pageurl := `https://developer.ibm.com/zh/articles/page/%d/?fa=date%3ADESC&fb`
go SavePages(urls)
for i := 1; i <= 73; i++ {
var url = fmt.Sprintf(pageurl, i)
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
if resp.StatusCode == 301 {
url = resp.Header.Get("location")
resp, err = http.Get(url)
if err != nil {
log.Fatalln(err)
}
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatalln(err)
}
sec := doc.Find("#content > div > div > div.cpt-archive-new > div.search-results > div > div:nth-child(2) > div.bx--row.bx--no-gutter--right.code-card-grid > div")
//fmt.Println(sec.Html())
sec.Each(func(i int, s *goquery.Selection) {
url, ok := s.Find("div > a").Attr("href")
if !ok {
return
}
urls <- "https://developer.ibm.com" + url
})
}
time.Sleep(time.Second * 10)
}