-
Notifications
You must be signed in to change notification settings - Fork 0
/
notion_import.go
121 lines (100 loc) · 2.56 KB
/
notion_import.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
package main
import (
"crypto/sha1"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/kjk/notionapi"
"github.com/kjk/notionapi/caching_downloader"
)
var (
cacheDir = "notion_cache"
)
func sha1OfLink(link string) string {
link = strings.ToLower(link)
h := sha1.New()
h.Write([]byte(link))
return fmt.Sprintf("%x", h.Sum(nil))
}
var imgFiles []os.FileInfo
func findImageInDir(imgDir string, sha1 string) string {
if len(imgFiles) == 0 {
imgFiles, _ = ioutil.ReadDir(imgDir)
}
for _, fi := range imgFiles {
if strings.HasPrefix(fi.Name(), sha1) {
return filepath.Join(imgDir, fi.Name())
}
}
return ""
}
func guessExt(fileName string, contentType string) string {
ext := strings.ToLower(filepath.Ext(fileName))
switch ext {
case ".png", ".jpg", ".jpeg":
return ext
}
contentType = strings.ToLower(contentType)
switch contentType {
case "image/png":
return ".png"
case "image/jpeg":
return ".jpg"
}
panic(fmt.Errorf("didn't find ext for file '%s', content type '%s'", fileName, contentType))
}
func downloadImage(c *notionapi.Client, uri string) ([]byte, string, error) {
img, err := c.DownloadFile(uri)
if err != nil {
logf("\n failed with %s\n", err)
return nil, "", err
}
ext := guessExt(uri, img.Header.Get("Content-Type"))
return img.Data, ext, nil
}
// return path of cached image on disk
func downloadAndCacheImage(c *notionapi.Client, uri string) (string, error) {
sha := sha1OfLink(uri)
//ext := strings.ToLower(filepath.Ext(uri))
imgDir := filepath.Join(cacheDir, "img")
err := os.MkdirAll(imgDir, 0755)
panicIfErr(err)
cachedPath := findImageInDir(imgDir, sha)
if cachedPath != "" {
verbose("Image %s already downloaded as %s\n", uri, cachedPath)
return cachedPath, nil
}
timeStart := time.Now()
logf("Downloading %s ... ", uri)
imgData, ext, err := downloadImage(c, uri)
//must(err)
cachedPath = filepath.Join(imgDir, sha+ext)
err = ioutil.WriteFile(cachedPath, imgData, 0644)
if err != nil {
return "", err
}
logf("finished in %s. Wrote as '%s'\n", time.Since(timeStart), cachedPath)
return cachedPath, nil
}
/*
func rmFile(path string) {
err := os.Remove(path)
if err != nil {
logf("os.Remove(%s) failed with %s\n", path, err)
}
}
func rmCached(pageID string) {
id := normalizeID(pageID)
rmFile(filepath.Join(cacheDir, id+".txt"))
}
*/
func loadPageAsArticle(d *caching_downloader.Downloader, pageID string) *Article {
page, err := d.DownloadPage(pageID)
panicIfErr(err)
logf("Downloaded %s %s\n", pageID, page.Root().Title)
c := ¬ionapi.Client{}
return notionPageToArticle(c, page)
}