-
Notifications
You must be signed in to change notification settings - Fork 3
/
speakerdeck.go
123 lines (108 loc) · 2.99 KB
/
speakerdeck.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
// Package speakerdeck is Fetch info and Download Slide from Speaker Deck for Go.
package speakerdeck
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
var (
base = "https://speakerdeck.com"
reSlideURL = regexp.MustCompile(`^` + base + `/([\w_-]+)/([\w_-]+)`)
)
// User is spearkerdeck user info
type User struct {
Username string `json:"username"`
DisplayName string `json:"displayName"`
URL string `json:"url"`
}
// Category is speakerdeck category info
type Category struct {
Name string `json:"name"`
Slug string `json:"slug"`
URL string `json:"url"`
}
// Slide is speakerdeck slide info
type Slide struct {
Slug string `json:"slug"`
Title string `json:"title"`
Description string `json:"description"`
DownloadURL string `json:"downloadUrl"`
User User `json:"user"`
Stars int `json:"stars"`
Category Category `json:"category"`
Published time.Time `json:"published"`
URL string `json:"url"`
}
// NewUser is initialize user
func NewUser(username, displayName string) User {
user := User{Username: username, DisplayName: displayName}
user.URL = base + "/" + username
return user
}
// NewCategory is initialize category
func NewCategory(name, slug string) Category {
cat := Category{Name: name, Slug: slug}
cat.URL = base + "/c/" + slug
return cat
}
// DownloadPDF downloads PDF of target slide
func (s *Slide) DownloadPDF() ([]byte, error) {
res, err := http.Get(s.DownloadURL)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}
// GetSlide returns slide info from Speakerdeck slide page
func GetSlide(url string) (*Slide, error) {
slug, err := parseSlideURL(url)
if err != nil {
return nil, err
}
doc, err := goquery.NewDocument(url)
if err != nil {
return nil, err
}
details := doc.Find("#talk-details")
sidebar := doc.Find(".sidebar")
a := sidebar.Find("h2 a")
user := NewUser(a.AttrOr("href", "")[1:], a.Text())
cat := sidebar.Find(".category a")
category := NewCategory(cat.Text(), strings.Split(cat.AttrOr("href", ""), "/")[2])
stars, err := strconv.Atoi(strings.Split(sidebar.Find(".stargazers").Text(), " ")[0])
if err != nil {
return nil, err
}
published, err := time.Parse("January 2, 2006", details.Find("header mark").First().Text())
if err != nil {
return nil, err
}
return &Slide{
Slug: slug,
Title: details.Find("h1").Text(),
Description: strings.TrimSpace(details.Find(".description").Text()),
Published: published,
DownloadURL: doc.Find("#share_pdf").AttrOr("href", ""),
User: user,
Category: category,
Stars: stars,
URL: user.URL + "/" + slug,
}, nil
}
// parseSlideURL returns slide's slug from url
func parseSlideURL(url string) (string, error) {
matches := reSlideURL.FindStringSubmatch(url)
if len(matches) < 3 {
return "", fmt.Errorf("Invalid URL")
}
return matches[2], nil
}