news.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package news
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/xml"
  6. "fmt"
  7. "html"
  8. "io"
  9. "net/http"
  10. "os"
  11. "strings"
  12. "time"
  13. "github.com/Jguer/yay/v12/pkg/text"
  14. )
  15. type item struct {
  16. Title string `xml:"title"`
  17. Link string `xml:"link"`
  18. Description string `xml:"description"`
  19. PubDate string `xml:"pubDate"`
  20. Creator string `xml:"dc:creator"`
  21. }
  22. func (item *item) print(buildTime time.Time, all, quiet bool) {
  23. var fd string
  24. date, err := time.Parse(time.RFC1123Z, item.PubDate)
  25. if err != nil {
  26. fmt.Fprintln(os.Stderr, err)
  27. } else {
  28. fd = text.FormatTime(int(date.Unix()))
  29. if !all && !buildTime.IsZero() {
  30. if buildTime.After(date) {
  31. return
  32. }
  33. }
  34. }
  35. fmt.Println(text.Bold(text.Magenta(fd)), text.Bold(strings.TrimSpace(item.Title)))
  36. if !quiet {
  37. desc := strings.TrimSpace(parseNews(item.Description))
  38. fmt.Println(desc)
  39. }
  40. }
  41. type channel struct {
  42. Title string `xml:"title"`
  43. Link string `xml:"link"`
  44. Description string `xml:"description"`
  45. Language string `xml:"language"`
  46. Lastbuilddate string `xml:"lastbuilddate"`
  47. Items []item `xml:"item"`
  48. }
  49. type rss struct {
  50. Channel channel `xml:"channel"`
  51. }
  52. func PrintNewsFeed(ctx context.Context, client *http.Client, cutOffDate time.Time, bottomUp, all, quiet bool) error {
  53. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://archlinux.org/feeds/news", http.NoBody)
  54. if err != nil {
  55. return err
  56. }
  57. resp, err := client.Do(req)
  58. if err != nil {
  59. return err
  60. }
  61. defer resp.Body.Close()
  62. body, err := io.ReadAll(resp.Body)
  63. if err != nil {
  64. return err
  65. }
  66. rssGot := rss{}
  67. d := xml.NewDecoder(bytes.NewReader(body))
  68. if err := d.Decode(&rssGot); err != nil {
  69. return err
  70. }
  71. if bottomUp {
  72. for i := len(rssGot.Channel.Items) - 1; i >= 0; i-- {
  73. rssGot.Channel.Items[i].print(cutOffDate, all, quiet)
  74. }
  75. } else {
  76. for i := 0; i < len(rssGot.Channel.Items); i++ {
  77. rssGot.Channel.Items[i].print(cutOffDate, all, quiet)
  78. }
  79. }
  80. return nil
  81. }
  82. // Crude html parsing, good enough for the arch news
  83. // This is only displayed in the terminal so there should be no security
  84. // concerns.
  85. func parseNews(str string) string {
  86. var (
  87. buffer bytes.Buffer
  88. tagBuffer bytes.Buffer
  89. escapeBuffer bytes.Buffer
  90. inTag = false
  91. inEscape = false
  92. )
  93. for _, char := range str {
  94. if inTag {
  95. if char == '>' {
  96. inTag = false
  97. switch tagBuffer.String() {
  98. case "code":
  99. buffer.WriteString(text.CyanCode)
  100. case "/code":
  101. buffer.WriteString(text.ResetCode)
  102. case "/p":
  103. buffer.WriteRune('\n')
  104. }
  105. continue
  106. }
  107. tagBuffer.WriteRune(char)
  108. continue
  109. }
  110. if inEscape {
  111. if char == ';' {
  112. inEscape = false
  113. escapeBuffer.WriteRune(char)
  114. s := html.UnescapeString(escapeBuffer.String())
  115. buffer.WriteString(s)
  116. continue
  117. }
  118. escapeBuffer.WriteRune(char)
  119. continue
  120. }
  121. if char == '<' {
  122. inTag = true
  123. tagBuffer.Reset()
  124. continue
  125. }
  126. if char == '&' {
  127. inEscape = true
  128. escapeBuffer.Reset()
  129. escapeBuffer.WriteRune(char)
  130. continue
  131. }
  132. buffer.WriteRune(char)
  133. }
  134. buffer.WriteString(text.ResetCode)
  135. return buffer.String()
  136. }