github.com/wtfutil/wtf@v0.43.0/modules/hackernews/story.go (about)

     1  package hackernews
     2  
     3  import "fmt"
     4  
     5  const (
     6  	hnStoryPath = "https://news.ycombinator.com/item?id="
     7  )
     8  
     9  // Story represents a story submission on HackerNews
    10  type Story struct {
    11  	By          string `json:"by"`
    12  	Descendants int    `json:"descendants"`
    13  	ID          int    `json:"id"`
    14  	Kids        []int  `json:"kids"`
    15  	Score       int    `json:"score"`
    16  	Time        int    `json:"time"`
    17  	Title       string `json:"title"`
    18  	Type        string `json:"type"`
    19  	URL         string `json:"url"`
    20  }
    21  
    22  // CommentLink return the link to the HackerNews story comments page
    23  func (story *Story) CommentLink() string {
    24  	return fmt.Sprintf("%s%d", hnStoryPath, story.ID)
    25  }
    26  
    27  // Link returns the link to a story. If the story has an external link, that is returned
    28  // If the story has no external link, the HackerNews comments link is returned instead
    29  func (story *Story) Link() string {
    30  	if story.URL != "" {
    31  		return story.URL
    32  	}
    33  
    34  	// Fall back to the HackerNews comment link
    35  	return story.CommentLink()
    36  }