Пример #1
0
func (f *defaultFeedparserImpl) FetchChannel(channel *models.Channel) error {

	result, err := fetch(channel.URL)

	if err != nil {
		return err
	}

	// update channel

	f.Log.Info("Channel:"+channel.Title, " podcasts:", len(result.Items))

	channel.Title = result.Channel.Title
	channel.Image = result.Channel.Image.Url
	channel.Description = result.Channel.Description

	website := result.getWebsiteURL()
	if website != "" {
		channel.Website.String = website
		channel.Website.Valid = true
	}

	// we just want unique categories
	categoryMap := make(map[string]string)

	for _, category := range result.Channel.Categories {
		categoryMap[category.Text] = category.Text
	}

	var categories []string
	for _, category := range categoryMap {
		categories = append(categories, category)
	}

	channel.Categories.String = strings.Join(categories, " ")
	channel.Categories.Valid = true

	if err := f.DB.Channels.Create(channel); err != nil {
		return err
	}

	for _, item := range result.Items {
		podcast := &models.Podcast{
			ChannelID:   channel.ID,
			Title:       item.Title,
			Description: item.Description,
		}

		if len(item.Enclosures) == 0 {
			continue
		}

		podcast.EnclosureURL = item.Enclosures[0].Url

		if item.Guid == nil {
			f.Log.Debug("Podcast ID:" + podcast.Title + " has no GUID, using pub date")
			// use pub date + URL as standin Guid

			podcast.Guid = item.PubDate + ":" + podcast.EnclosureURL

		} else {
			podcast.Guid = *item.Guid
		}

		if item.Source != nil {
			podcast.Source = item.Source.Url
		}

		if podcast.Guid == "" {
			f.Log.Error("Could not find suitable GUID for " + podcast.Title)
		}

		var pubDate time.Time

		// try using the builtin RSS parser first
		if pubDate, err = item.ParsedPubDate(); err != nil {
			// try some other parsers
			pubDate, err = now.Parse(item.PubDate)
			// pubdate will be "empty", we'll have to live with that
			// but log anyway to see if we can fix that format
			if err != nil {
				f.Log.Error(err)
			}
		}
		podcast.PubDate = pubDate

		if err = f.DB.Podcasts.Create(podcast); err != nil {
			return err
		}
	}

	return nil

}
Пример #2
0
func (f *feedparserImpl) Fetch(channel *models.Channel) error {

	result, err := fetch(channel.URL)

	if err != nil {
		return err
	}

	channel.Title = result.channel.Title
	channel.Image = result.channel.Image.Url
	channel.Description = result.channel.Description

	website := result.getWebsiteURL()

	if website != "" {
		channel.Website.String = website
		channel.Website.Valid = true
	}

	// we just want unique categories
	categoryMap := make(map[string]string)

	for _, category := range result.channel.Categories {
		categoryMap[category.Text] = category.Text
	}

	var categories []string
	for _, category := range categoryMap {
		categories = append(categories, category)
	}

	channel.Categories.String = strings.Join(categories, " ")
	channel.Categories.Valid = true

	var podcasts []*models.Podcast

	for _, item := range result.items {

		podcast := &models.Podcast{
			Title:       item.Title,
			Description: item.Description,
		}

		podcast.EnclosureURL = item.Enclosures[0].Url

		if item.Guid == nil {
			// use pub date + URL as standin Guid

			podcast.Guid = item.PubDate + ":" + podcast.EnclosureURL

		} else {
			podcast.Guid = *item.Guid
		}

		if item.Source != nil {
			podcast.Source = item.Source.Url
		}

		var pubDate time.Time

		// try using the builtin RSS parser first
		if pubDate, err = item.ParsedPubDate(); err != nil {
			// try some other parsers
			pubDate, err = now.Parse(item.PubDate)
			// pubdate will be "empty", we'll have to live with that
		}
		podcast.PubDate = pubDate

		podcasts = append(podcasts, podcast)

	}

	channel.Podcasts = podcasts
	return nil

}