generated from ACM-VIT/hacktoberfest-readme
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmusic.go
106 lines (93 loc) · 2.69 KB
/
music.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
package main
import (
"encoding/json"
"flag"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
// For handling dynamic videoId and playlistId keys
type Info map[string]string
type Response struct {
Kind string `json:"kind"`
Etag string `json:"etag"`
NextPageToken string `json:"nextPageToken"`
RegionCode string `json:"regionCode"`
PageInfo struct {
TotalResults int `json:"totalResults"`
ResultsPerPage int `json:"resultsPerPage"`
} `json:"pageInfo"`
Items []struct {
Kind string `json:"kind"`
Etag string `json:"etag"`
ID Info `json:"id"`
Snippet struct {
PublishedAt time.Time `json:"publishedAt"`
ChannelID string `json:"channelId"`
Title string `json:"title"`
Description string `json:"description"`
Thumbnails struct {
Default struct {
URL string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
} `json:"default"`
Medium struct {
URL string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
} `json:"medium"`
High struct {
URL string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
} `json:"high"`
} `json:"thumbnails"`
ChannelTitle string `json:"channelTitle"`
LiveBroadcastContent string `json:"liveBroadcastContent"`
PublishTime time.Time `json:"publishTime"`
} `json:"snippet"`
} `json:"items"`
}
func searchYT(query string, youtubeAPIKey string) (string, string) {
flag.Parse()
// URL encode the query
query = url.QueryEscape(query)
// Make the API call to YouTube.
resp, err := http.Get("https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=" + query + "&key=" + youtubeAPIKey)
if err != nil {
log.Fatalf("Error making search API call: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var result Response
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer
fmt.Println("Can not unmarshal JSON")
}
items := result.Items
// Check if query returned any results
if len(items) == 0 {
return "No search results were found!", ""
}
// Check if result is video or playlist
kind := items[0].ID["kind"]
var link string
if kind == "youtube#video" {
videoId := items[0].ID["videoId"]
link = "https://www.youtube.com/watch?v=" + videoId
} else {
playlistId := items[0].ID["playlistId"]
link = "https://youtube.com/playlist?list=" + playlistId
}
// Parse title and replace html entities
title := items[0].Snippet.Title
title = html.UnescapeString(title)
return title, link
}