-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAPI.go
69 lines (49 loc) · 1.45 KB
/
getAPI.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
package main
import(
"time"
"net/http"
"github.com/antonholmquist/jason"
"log"
)
const getURL = "https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular®ionCode=JP&maxResults=25&key=token"
func getYoutubeTrending() {
list := []channel{}
db := connectDB()
defer db.Close()
res, err := http.Get(getURL)
if err != nil {
panic(err)
}
defer res.Body.Close()
getJson, err := jason.NewObjectFromReader(res.Body)
if err != nil {
panic(err)
}
items, err := getJson.GetObjectArray("items")
for _, item := range items {
t := time.Now()
const layout = "20060102"
id, _:= item.GetString("id")
title, _:= item.GetString("snippet", "title")
channelTitle, _:= item.GetString("snippet","channelTitle")
channelID, _ := item.GetString("snippet","channelId")
description, _ := item.GetString("snippet", "description")
descriptionCut := isCut(description)
list = append(list, channel{URL:id, Title:title,ChannelID:channelID, ChannelTitle:channelTitle,Description:descriptionCut, Date:t.Format(layout)})
log.Println(id, title, channelTitle, channelID, descriptionCut)
}
insert(list,db)
}
func trendingTicker() {
for {
getYoutubeTrending()
time.Sleep(24 * time.Hour)
}
}
func isCut(description string) string {
if len([]rune(description)) > 105{
description:= string([]rune(description)[0:105])
return description
}
return description
}