forked from benrowe/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_external_links.go
88 lines (80 loc) · 2.03 KB
/
report_external_links.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
package main
import (
"sort"
"strings"
"github.com/kjk/notionapi"
"github.com/kjk/notionapi/caching_downloader"
)
func isNotionURL(uri string) bool {
return strings.Contains(uri, "notion.so/")
}
func isStackOverflowURL(uri string) bool {
return strings.Contains(uri, "stackoverflow.com/") && !strings.Contains(uri, "/users/")
}
func reportExternalLinksInPage(page *notionapi.Page) error {
links := map[string]struct{}{}
rememberLink := func(uri string) {
if isNotionURL(uri) {
return
}
if flgReportStackOverflowLinks {
if isStackOverflowURL(uri) {
links[uri] = struct{}{}
}
return
}
links[uri] = struct{}{}
}
findLinks := func(b *notionapi.Block) {
spans := b.GetTitle()
for _, ts := range spans {
for _, attr := range ts.Attrs {
attrType := notionapi.AttrGetType(attr)
if attrType == notionapi.AttrLink {
uri := notionapi.AttrGetLink(attr)
rememberLink(uri)
}
}
}
}
page.ForEachBlock(findLinks)
if len(links) == 0 {
return nil
}
id := toNoDashID(page.ID)
logf(" page https://www.notion.so/%s has %d links\n", id, len(links))
var a []string
for uri := range links {
a = append(a, uri)
}
sort.Strings(a)
for _, uri := range a {
logf(" %s\n", uri)
}
return nil
}
func reportExternalLinksInBook(book *Book) {
cacheDir := book.NotionCacheDir()
dirCache, err := caching_downloader.NewDirectoryCache(cacheDir)
must(err)
client := ¬ionapi.Client{
AuthToken: notionAuthToken,
}
d := caching_downloader.New(dirCache, client)
d.EventObserver = eventObserver
d.RedownloadNewerVersions = false
startPageID := book.NotionStartPageID
nProcessed = 0
nNotionPagesFromCache = 0
nDownloadedPages = 0
pages, err := d.DownloadPagesRecursively(startPageID, reportExternalLinksInPage)
must(err)
nPages := len(pages)
logf("Book %s, %d pages, downloaded: %d, from cache: %d\n", book.Title, nPages, nDownloadedPages, nNotionPagesFromCache)
}
func reportExternalLinks() {
logf("starting reportExternalLinks()\n")
for _, b := range booksMain {
reportExternalLinksInBook(b)
}
}