项目作者: mileusna

项目描述 :
Go (golang) package for creating and publishing Facebook Instant Articles
高级语言: Go
项目地址: git://github.com/mileusna/facebook-instant-articles.git
创建时间: 2016-06-08T19:03:17Z
项目社区:https://github.com/mileusna/facebook-instant-articles

开源协议:MIT License

下载


Facebook Instant Articles for Go GoDoc

Package Instant enables creation and publishing of Facebook Instant Articles https://developers.facebook.com/docs/instant-articles

Article

Struct instant.Article represents Facebook instant article as described on
https://developers.facebook.com/docs/instant-articles/guides/articlecreate
Use instant.Article and then use helper functions like SetTitle(), SetCoverImage() etc.
to easily create instant article without setting struct properties directly. Elemenets are
nested in inner struct, so using setter functions is much easier. Custom marshaler will generate
Facebook instant article valid html..

  1. package main
  2. import (
  3. "encoding/xml"
  4. "time"
  5. "github.com/mileusna/facebook-instant-articles"
  6. )
  7. func main() {
  8. a := instant.Article{}
  9. // required
  10. a.SetTitle("My article title")
  11. a.SetCanonical("http://mysite/url-to-this-article")
  12. a.SetPublish(time.Now())
  13. a.SetContent("<p>My content</p><p>Other paragraph</p>")
  14. // optional
  15. a.SetLang("fr") // default is en
  16. a.SetSubtitle("My article subtitle")
  17. a.SetKick("Exclusive")
  18. a.SetFooter("", "(C)2016 MyComp")
  19. a.AddAuthor("Michael", "http://facebook.com/mmichael", "Guest writter")
  20. html, err := xml.Marshal(a)
  21. // html, err := a.HTML() // synonym for xml.Marshal
  22. if err != nil {
  23. return
  24. }
  25. // html contains Facebook instant article html as []byte
  26. }

Feed

Struct instant.Feed represents Facebook instant article RSS feed as described on
https://developers.facebook.com/docs/instant-articles/publishing/setup-rss-feed
Use instant.Feed{} and AddArticles to add instant.Article to feed. Custom
marshaler provides valid Facebook instant article RSS feed.

  1. package main
  2. import (
  3. "encoding/xml"
  4. "net/http"
  5. "time"
  6. "github.com/mileusna/facebook-instant-articles"
  7. )
  8. func main() {
  9. http.HandleFunc("/instant-articles/", handleInstantArticles)
  10. http.ListenAndServe(":8080", nil)
  11. }
  12. func handleInstantArticles(w http.ResponseWriter, r *http.Request) {
  13. var f instant.Feed
  14. // I don't believe Facebook cares about feed title, link and description, but if you like...
  15. f.SetTitle("Title feed")
  16. f.SetLink("http://www.mysite.com")
  17. f.SetDescription("Feed description")
  18. var a instant.Article
  19. a.SetTitle("My article title")
  20. a.SetCanonical("http://mysite/url-to-this-article")
  21. a.SetPublish(time.Now())
  22. a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
  23. f.AddArticle(a) // add article, GUID will be hash of URL
  24. a = instant.Article{}
  25. a.SetTitle("My other title")
  26. a.SetCanonical("http://mysite/url-to-this-article-number-two")
  27. a.SetPublish(time.Now())
  28. a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
  29. f.AddArticleWithGUID(a, "12333") // add article and use for example mysql id as GUID
  30. rss, err := xml.Marshal(f)
  31. // rss, err := f.RSS() // synonym for xml.Marshal
  32. if rss != nil {
  33. return
  34. }
  35. w.Write(rss)
  36. }

Documentation

https://godoc.org/github.com/mileusna/facebook-instant-articles

License

MIT