mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Bit more on the subreddit module - fixing comments and adding options for top sort
This commit is contained in:
		
							parent
							
								
									b0d6790abe
								
							
						
					
					
						commit
						02bdb405fa
					
				@ -9,8 +9,13 @@ import (
 | 
			
		||||
 | 
			
		||||
var rootPage = "https://reddit.com/r/"
 | 
			
		||||
 | 
			
		||||
func GetLinks(subreddit string, sortMode string) ([]Link, error) {
 | 
			
		||||
	request, err := http.NewRequest("GET", rootPage+subreddit+"/"+sortMode+".json", nil)
 | 
			
		||||
func GetLinks(subreddit string, sortMode string, topTimePeriod string) ([]Link, error) {
 | 
			
		||||
	url := rootPage + subreddit + "/" + sortMode + ".json"
 | 
			
		||||
	if sortMode == "top" {
 | 
			
		||||
		url = url + "?sort=top&t=" + topTimePeriod
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	request, err := http.NewRequest("GET", url, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@ -34,6 +39,10 @@ func GetLinks(subreddit string, sortMode string) ([]Link, error) {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(m.Data.Children) == 0 {
 | 
			
		||||
		return nil, fmt.Errorf("No links")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var links []Link
 | 
			
		||||
	for _, l := range m.Data.Children {
 | 
			
		||||
		links = append(links, l.Data)
 | 
			
		||||
 | 
			
		||||
@ -16,6 +16,7 @@ type Settings struct {
 | 
			
		||||
	subreddit     string `help:"Subreddit to look at" optional:"false"`
 | 
			
		||||
	numberOfPosts int    `help:"Number of posts to show. Default is 10." optional:"true"`
 | 
			
		||||
	sortOrder     string `help:"Sort order for the posts (hot, new, rising, top), default hot" optional:"true"`
 | 
			
		||||
	topTimePeriod string `help:"If top sort is selected, the time period to show posts from (hour, week, day, month, year, all, default all)"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewSettingsFromYAML creates the settings for this module from a yaml file
 | 
			
		||||
@ -26,6 +27,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
 | 
			
		||||
 | 
			
		||||
		numberOfPosts: ymlConfig.UInt("numberOfPosts", 10),
 | 
			
		||||
		sortOrder:     ymlConfig.UString("sortOrder", "hot"),
 | 
			
		||||
		topTimePeriod: ymlConfig.UString("topTimePeriod", "all"),
 | 
			
		||||
		subreddit:     subreddit,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -35,15 +35,17 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Exported Functions -------------------- */
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) Refresh() {
 | 
			
		||||
	links, err := GetLinks(widget.settings.subreddit, widget.settings.sortOrder)
 | 
			
		||||
	links, err := GetLinks(widget.settings.subreddit, widget.settings.sortOrder, widget.settings.topTimePeriod)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		widget.err = err
 | 
			
		||||
		widget.links = nil
 | 
			
		||||
		widget.SetItemCount(0)
 | 
			
		||||
	} else {
 | 
			
		||||
		widget.links = links
 | 
			
		||||
		widget.SetItemCount(len(links))
 | 
			
		||||
		widget.links = links[:widget.settings.numberOfPosts]
 | 
			
		||||
		widget.SetItemCount(len(widget.links))
 | 
			
		||||
		widget.err = nil
 | 
			
		||||
	}
 | 
			
		||||
	widget.Render()
 | 
			
		||||
@ -53,6 +55,8 @@ func (widget *Widget) Render() {
 | 
			
		||||
	widget.Redraw(widget.content)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Unexported Functions -------------------- */
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) content() (string, string, bool) {
 | 
			
		||||
	title := "/r/" + widget.settings.subreddit + " - " + widget.settings.sortOrder
 | 
			
		||||
	if widget.err != nil {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user