Golang : Get YouTube playlist
A programmer productivity can be increased with a good playlist of music to get into the "zone" and I need this while coding out the job board for socketloop.com(1)
Just for fun, this is the Golang code to download a playlist from YouTube, unmarshal the XML data and display the content.
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
type YouTubeEntries struct {
XMLName xml.Name `xml:"entry"`
ID string `xml:"id"`
Published string `xml:"published"`
Updated string `xml:"updated"`
Category string `xml:"category"`
Title string `xml:"title"`
Content string `xml:"content"`
Link string `xml:"link"`
Author string `xml:"author"`
}
type YouTubeFeeds struct {
XMLName xml.Name `xml:"feed"`
YouTubeEntries []YouTubeEntries `xml:"entry"`
}
// modify here to add more data to be displayed
func (data YouTubeEntries) String() string {
dateToYMD := strings.Split(data.Published, "T")
return fmt.Sprintf("\t%s - %s - [%s]\n", dateToYMD[0], data.Title, data.ID)
}
func main() {
response, err := http.Get("http://gdata.youtube.com/feeds/api/playlists/RDXIzDDYrS4iE?v=2")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer response.Body.Close()
XMLdata, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var youtubeFeeds YouTubeFeeds
xml.Unmarshal(XMLdata, &youtubeFeeds)
fmt.Println(youtubeFeeds.YouTubeEntries)
// alternate ways to display the list
for k, v := range youtubeFeeds.YouTubeEntries {
fmt.Printf("\t%d - [%s]\n", k, v)
}
}
Have fun learning Go and YouTube API !
1 - it is still a work in progress and I will announce it soon
Reference :
See also : Read a XML file in Go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12k Golang : Simple client-server HMAC authentication without SSL example
+19.7k Golang : Determine if directory is empty with os.File.Readdir() function
+6.1k Golang : Extract sub-strings
+14.8k JavaScript/JQuery : Detect or intercept enter key pressed example
+9.9k Golang : Bcrypting password
+19.2k Golang : How to Set or Add Header http.ResponseWriter?
+21.5k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+26.8k Golang : Find files by name - cross platform example
+5k Golang : Print instead of building pyramids
+9.7k Golang : Setting variable value with ldflags
+29.9k Golang : How to verify uploaded file is image or allowed file types
+18.2k Golang : Example for RSA package functions