Beispiel #1
0
func (s *Soundcloud) Refresh() (protocol.SongList, error) {
	service, _, err := s.getService()
	if err != nil {
		return nil, err
	}
	favorites, err := service.Favorites().Do()
	if err != nil {
		return nil, err
	}
	favs := make(map[codec.ID]*soundcloud.Favorite)
	for _, f := range favorites {
		favs[codec.Int64(f.ID)] = f
	}
	s.Favorites = favs
	return s.SongList(), err
}
Beispiel #2
0
func (b *Bandcamp) Refresh() (protocol.SongList, error) {
	resp, err := http.Get(b.URL)
	if err != nil {
		return nil, err
	}
	y, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		return nil, err
	}
	var tracks []*track
	var a album
	{
		const start = "    trackinfo : ["
		i := bytes.Index(y, []byte(start))
		if i < 0 {
			return nil, fmt.Errorf("not a bandcamp site: %v", b.URL)
		}
		e := bytes.Index(y[i:], []byte("],\n"))
		if e < 0 {
			return nil, fmt.Errorf("not a bandcamp site: %v", b.URL)
		}
		if err := json.Unmarshal(y[i+len(start)-1:e+1+i], &tracks); err != nil {
			return nil, err
		}
	}
	{
		const start = "    current: {"
		i := bytes.Index(y, []byte(start))
		if i < 0 {
			return nil, fmt.Errorf("not a bandcamp site: %v", b.URL)
		}
		e := bytes.Index(y[i:], []byte("},\n"))
		if e < 0 {
			return nil, fmt.Errorf("not a bandcamp site: %v", b.URL)
		}
		if err := json.Unmarshal(y[i+len(start)-1:e+1+i], &a); err != nil {
			return nil, err
		}
	}
	var artURL string
	func() {
		const start = `    artThumbURL: "`
		i := bytes.Index(y, []byte(start))
		if i < 0 {
			return
		}
		e := bytes.Index(y[i:], []byte("\",\n"))
		if e < 0 {
			return
		}
		if err := json.Unmarshal(y[i+len(start)-1:e+1+i], &artURL); err != nil {
			return
		}
	}()
	tracklist := make(map[codec.ID]*track)
	songs := make(protocol.SongList)
	for _, t := range tracks {
		if t.File.Mp3_128 == "" {
			continue
		}
		id := codec.Int64(t.ID)
		tracklist[id] = t
		songs[id] = &codec.SongInfo{
			Time:     time.Duration(t.Duration) * time.Second,
			Album:    a.Title,
			Artist:   a.Artist,
			Title:    t.Title,
			Track:    float64(t.TrackNum),
			ImageURL: artURL,
		}
	}
	b.Songs = songs
	b.Tracks = tracklist
	return songs, err
}