-
Notifications
You must be signed in to change notification settings - Fork 0
/
slideshare.go
109 lines (92 loc) · 2.68 KB
/
slideshare.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
package main
import (
slideshare "github.com/seanwatson/go-slideshare"
"log"
"strings"
"strconv"
)
var ssSearchOptions = map[string]string{
"sort": "latest",
}
var cachedSlideshowsToUrl = make(map[string]string)
const numCachePages = 25
const cachePageSize = 200
func searchOnUserForKeyword(userName string, keyword string, ssApiKey string, ssSecret string) string {
cachedShowUrl, ok := cachedSlideshowsToUrl[keyword]
if (ok) {
return cachedShowUrl
}
log.Printf("cache miss on %s", keyword)
url := ""
ss := slideshare.SlideShare{ssApiKey, ssSecret}
shows, err := ss.SearchSlideshows(keyword, ssSearchOptions)
if err != nil {
log.Fatalf("Error connecting to slideshare - %v", err)
}
if len(shows) != 0 {
for _, show := range shows {
if (strings.Contains(show.Title, "AWS re:Invent 2016") && show.Username == userName) {
url = show.Url
break
}
}
}
if url == "" {
log.Printf("no slideshare data found for %s", keyword)
}
return url
}
func cacheSlideSharesByUsername(userName string, ssApiKey string, ssSecret string) {
ss := slideshare.SlideShare{ssApiKey, ssSecret}
var ssUsernameOptions = map[string]string {
"limit": strconv.Itoa(cachePageSize),
}
for ii := 0; ii < numCachePages; ii++ {
offset := ii * cachePageSize
ssUsernameOptions["offset"] = strconv.Itoa(offset)
// this requires a fix I need to upstream into slideshare client
shows, err := ss.GetSlideshowsByUser(userName, ssUsernameOptions)
if err != nil {
log.Fatalf("Error connecting to slideshare - %v", err)
} else if (len(shows) == 0) {
log.Printf("finished getting slideshows at offset = %v", offset)
return
}
// to speed up loading of cache (assumes in reverse cronological ordering)
if !allShowsAreNewerThan2016(shows) {
return
}
for _, show := range shows {
//log.Printf("title = %s", show.Title)
if (strings.Contains(show.Title, "AWS re:Invent 2016")) {
fields := strings.FieldsFunc(show.Title, func (r rune) bool {
return r == '(' || r == ')'
})
for _, field := range fields {
for _, track := range tracks {
trimmedField := strings.TrimSpace(field)
if (strings.HasPrefix(trimmedField, track)) {
log.Printf("adding %s to cache", trimmedField)
cachedSlideshowsToUrl[trimmedField] = show.Url
}
}
}
}
}
}
}
func allShowsAreNewerThan2016(shows []slideshare.Slideshow) bool {
for _, show := range shows {
// pretty hacky date parsing, could/should have used time
created := show.Created
fields := strings.Split(created, "-")
year, err := strconv.Atoi(fields[0])
if err != nil {
log.Fatalf("created data malformed %s", created)
}
if (year < 2016) {
return false
}
}
return true
}