Example #1
0
func importHandler(w http.ResponseWriter, r *http.Request) {
	file, _, err := r.FormFile("opmlFile")

	if err != nil {
		log.Print(err)
		return
	}
	contents, err := ioutil.ReadAll(file)

	if err != nil {
		log.Print(err)
		return
	}

	userid := getUserId(w, r)
	urls, err := parser.ParseOPML(contents)

	if err != nil {
		log.Print(err)
		return
	}

	for _, url := range urls {

		feed := data.Feed{Id: data.GenerateId(url), Url: url}
		was_inserted := db.AddFeed(feed, userid)

		if was_inserted {
			fetchFeed(feed)
		}
	}

	fmt.Fprintf(w, "%s", "Imported")
}
Example #2
0
func parseRSS(contents string) (string, []data.FeedItem, error) {
	r := rss{}
	feedItems := []data.FeedItem{}
	d := xml.NewDecoder(bytes.NewReader([]byte(contents)))
	d.CharsetReader = charset.NewReader
	err := d.Decode(&r)

	if err != nil {
		return "", []data.FeedItem{}, err
	}
	channel := r.Channel
	currentTime := time.Now()
	var t time.Time

	for i, item := range channel.Items {
		if len(item.PubDate) == 0 {
			t = currentTime.Add(-time.Duration(i) * time.Second)
		} else {
			t, err = parseTime(item.PubDate)
		}

		if err != nil {
			log.Printf("%s", err)
			t = time.Now()
		}

		if len(item.EncodedContent) > len(item.Description) {
			item.Description = item.EncodedContent
		}

		words := strings.Split(item.Description, " ")
		blurb_length := len(words)
		if blurb_length > 50 {
			blurb_length = 50
		}

		id := item.Guid
		if len(id) == 0 {
			id = item.Title
		}
		feedItem := data.FeedItem{data.GenerateId(id), item.Title, item.Link, strings.Join(words[:blurb_length], " ") + "...", item.Description, t}
		feedItems = append(feedItems, feedItem)
	}

	return channel.Title, feedItems, nil
}
Example #3
0
func parseFeed(contents string) (string, []data.FeedItem, error) {
	r := feed{}
	feedItems := []data.FeedItem{}
	d := xml.NewDecoder(bytes.NewReader([]byte(contents)))
	d.CharsetReader = charset.NewReader
	err := d.Decode(&r)

	if err != nil {
		return "", []data.FeedItem{}, err
	}

	currentTime := time.Now()
	var t time.Time

	for i, entry := range r.Entries {
		if len(entry.Published) == 0 {
			entry.Published = entry.Updated
		}
		if len(entry.Published) == 0 {
			t = currentTime.Add(-time.Duration(i) * time.Second)
		} else {
			t, err = parseTime(entry.Published)
			if err != nil {
				panic(err)
			}
		}

		words := strings.Split(entry.Contents, " ")
		blurb_length := len(words)
		if blurb_length > 50 {
			blurb_length = 50
		}

		feedItem := data.FeedItem{data.GenerateId(entry.Id), entry.Title, entry.Link.Href, strings.Join(words[:blurb_length], " ") + "...", entry.Contents, t}
		feedItems = append(feedItems, feedItem)
	}

	return r.Title, feedItems, nil
}
Example #4
0
func addFeedHandler(w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(err)
	}
	type JsonData struct {
		Url string
	}
	var jsonData JsonData
	json.Unmarshal(body, &jsonData)

	feed := data.Feed{Id: data.GenerateId(jsonData.Url), Url: jsonData.Url, Title: jsonData.Url}
	userid := getUserId(w, r)

	was_inserted := db.AddFeed(feed, userid)

	if was_inserted {
		fetchFeed(feed)
	}

	listFeedsHandler(w, r)
}