Help us shape the future of Go! Take the 2026 Go Developer Survey

instant

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 9 Imported by: 0

README

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..

package main

import (
	"encoding/xml"
	"time"
	"github.com/mileusna/facebook-instant-articles"
)

func main() {
	a := instant.Article{}

	// required
	a.SetTitle("My article title")
	a.SetCanonical("http://mysite/url-to-this-article")
	a.SetPublish(time.Now())
	a.SetContent("<p>My content</p><p>Other paragraph</p>")

	// optional
	a.SetLang("fr") // default is en
	a.SetSubtitle("My article subtitle")
	a.SetKick("Exclusive")
	a.SetFooter("", "(C)2016 MyComp")
	a.AddAuthor("Michael", "http://facebook.com/mmichael", "Guest writter")

	html, err := xml.Marshal(a)
	// html, err := a.HTML() // synonym for xml.Marshal
	if err != nil {
		return
	}
    // html contains Facebook instant article html as []byte
}

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.

package main

import (
	"encoding/xml"
	"net/http"
	"time"

	"github.com/mileusna/facebook-instant-articles"
)

func main() {
	http.HandleFunc("/instant-articles/", handleInstantArticles)
	http.ListenAndServe(":8080", nil)
}

func handleInstantArticles(w http.ResponseWriter, r *http.Request) {
	var f instant.Feed
	// I don't believe Facebook cares about feed title, link and description, but if you like...
	f.SetTitle("Title feed")
	f.SetLink("http://www.mysite.com")
	f.SetDescription("Feed description")

	var a instant.Article
	a.SetTitle("My article title")
	a.SetCanonical("http://mysite/url-to-this-article")
	a.SetPublish(time.Now())
	a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
	f.AddArticle(a) // add article, GUID will be hash of URL

	a = instant.Article{}
	a.SetTitle("My other title")
	a.SetCanonical("http://mysite/url-to-this-article-number-two")
	a.SetPublish(time.Now())
	a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
	f.AddArticleWithGUID(a, "12333") // add article and use for example mysql id as GUID

	rss, err := xml.Marshal(f)
	// rss, err := f.RSS() // synonym for xml.Marshal
	if rss != nil {
		return
	}
	w.Write(rss)
}

Documentation

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

License

MIT

Documentation

Overview

Package instant enables creation and publishing of Facebook Instant Articles.

Full Facebook instant articles documentation can be found here https://developers.facebook.com/docs/instant-articles

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.

Example:

package main

import (
    "encoding/xml"
    "time"
    "github.com/mileusna/facebook-instant-articles"
)

func main() {
    a := instant.Article{}

    // required
    a.SetTitle("My article title")
    a.SetCanonical("http://mysite/url-to-this-article")
    a.SetPublish(time.Now())
    a.SetContent("<p>My content</p><p>Other paragraph</p>")

    // optional
    a.SetSubtitle("My article subtitle")
    a.SetKick("Exclusive")
    a.SetFooter("", "(C)2016 MyComp")
    a.AddAuthor("Michael", "http://facebook.com/mmichael", "Guest writter")

    html, err := xml.Marshal(a)
    if err != nil {
        return
    }
    // html contains Facebook instant article html as []byte
}

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.NewFeed() to create initial struct with all headers set up and use helper functions like AddArticles to add instant.Article to feed. Custom marshaler provides valid Facebook instant article RSS feed.

Example:

package main

import (
    "encoding/xml"
    "net/http"
    "time"

    "github.com/mileusna/facebook-instant-articles"
)

func main() {
    http.HandleFunc("/instant-articles/", handleInstantArticles)
    http.ListenAndServe(":8080", nil)
}

func handleInstantArticles(w http.ResponseWriter, r *http.Request) {
    var f instant.Feed
    // I don't believe Facebook cares about feed title, link and description, but you can set them
    f.SetTitle("Title feed")
    f.SetLink("http://www.mysite.com")
    f.SetDescription("Feed description")

    var a instant.Article
    a.SetTitle("My article title")
    a.SetCanonical("http://mysite/url-to-this-article")
    a.SetPublish(time.Now())
    a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
    f.AddArticle(a) // add article, GUID will be hash of URL

    a = instant.Article{}
    a.SetTitle("My other title")
    a.SetCanonical("http://mysite/url-to-this-article-number-two")
    a.SetPublish(time.Now())
    a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
    f.AddArticleWithGUID(a, "12333") // add article and use for example mysql id as GUID

    feed, err := xml.Marshal(f)
    if err != nil {
        return
    }
    w.Write(feed)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Article

type Article struct {
	Prefix string `xml:"prefix,attr"`
	Lang   string `xml:"lang,attr"`

	Head head `xml:"head"`
	Body body `xml:"body"`
}

Article struct

func (*Article) AddAd

func (a *Article) AddAd(position int, src string, width, height int, style, code string)

AddAd manually in article content.

func (*Article) AddAuthor

func (a *Article) AddAuthor(name, link, description string)

AddAuthor adds article author. Link and description are optional (use "")

func (*Article) AddFigure

func (a *Article) AddFigure(f Figure)

AddFigure to article content

func (*Article) AddParagraph

func (a *Article) AddParagraph(html string)

AddParagraph to Instant Article.

func (Article) HTML

func (a Article) HTML() ([]byte, error)

HTML is synonym for xml.Marshal(a)

func (*Article) InsertAd

func (a *Article) InsertAd(position int, src string, width, height int, style, code string)

InsertAd manually on position between paragraphs.

func (*Article) InsertFigure

func (a *Article) InsertFigure(position int, f Figure)

InsertFigure in content on specified position within existing elements (paragraphs)

func (Article) MarshalXML

func (a Article) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for xml.Marshaler interface, marshal Article struct to Facebook Instant Article format.

func (*Article) SetAutomaticAd

func (a *Article) SetAutomaticAd(src string, width, height int, style, code string)

SetAutomaticAd in header that Facebook will place automatically in article

func (*Article) SetCanonical

func (a *Article) SetCanonical(url string)

SetCanonical sets public web url for this article. Setting canonical link is required.

func (*Article) SetContent

func (a *Article) SetContent(html string)

SetContent of instant article. All html should be in <p> ... </p> elements. If no <p> elements found, entire HTML param will be added as one paragraph. Images and videos can be added later using InsertFigure() but they can also be contained in HTML param if formated properly. See https://developers.facebook.com/docs/instant-articles/reference for more info.

func (*Article) SetCoverImage

func (a *Article) SetCoverImage(url, caption string)

SetCoverImage of instant article. Caption can be empty string.

func (*Article) SetCoverVideo

func (a *Article) SetCoverVideo(url, videoType, caption string)

SetCoverVideo of instant article. videoType in format video/mp4 See the list of supported formats here https://www.facebook.com/help/218673814818907 Caption can be empty string.

func (*Article) SetFooter

func (a *Article) SetFooter(credits, copyright string)

SetFooter sets text in footer You can use <p> in credits

func (*Article) SetKick

func (a *Article) SetKick(kick string)

SetKick sets article kick text.

func (*Article) SetLang

func (a *Article) SetLang(lang string)

SetLang sets two-chars language code for article. Default is set to en.

func (*Article) SetModified

func (a *Article) SetModified(date time.Time)

SetModified sets modified date if article has been modified.

func (*Article) SetPublish

func (a *Article) SetPublish(date time.Time)

SetPublish sets published date.

func (*Article) SetStyle

func (a *Article) SetStyle(style string)

SetStyle set user defined style for this article. See https://developers.facebook.com/docs/instant-articles/guides/design#style for more info.

func (*Article) SetSubtitle

func (a *Article) SetSubtitle(subtitle string)

SetSubtitle sets article subtitle.

func (*Article) SetTitle

func (a *Article) SetTitle(title string)

SetTitle sets article title. Setting article title is mandatory.

func (*Article) SetTrackerCode

func (a *Article) SetTrackerCode(code string)

SetTrackerCode for 3rd party analytics (Google Analytics for example) Visit https://developers.facebook.com/docs/instant-articles/reference/analytics for more info.

func (*Article) SetTrackerURL

func (a *Article) SetTrackerURL(url string)

SetTrackerURL for 3rd party analytics that can be included with url. Visit https://developers.facebook.com/docs/instant-articles/reference/analytics for more info.

type Content

type Content []ContentTag

Content of Facebook Instant Article, currently support <p> and <figure>

func (Content) MarshalXML

func (c Content) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for elements in body article content

type ContentTag

type ContentTag interface {
	StartElement() xml.StartElement
}

ContentTag interface for supported tag elements in Facebook Article Content

type Feed

type Feed struct {
	Version string  `xml:"version,attr"`
	Content string  `xml:"xmlns:content,attr"`
	Channel channel `xml:"channel"`
}

Feed struct represents Facebook Instant Articles RSS feed. See https://developers.facebook.com/docs/instant-articles/publishing/setup-rss-feed for more info.

func (*Feed) AddArticle

func (f *Feed) AddArticle(a Article) error

AddArticle to feed. Md5 checksum of URL will be used as GUID.

func (*Feed) AddArticleWithGUID

func (f *Feed) AddArticleWithGUID(a Article, guid string) error

AddArticleWithGUID to feed.

func (Feed) MarshalXML

func (f Feed) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for xml.Marshaler interface

func (Feed) RSS

func (f Feed) RSS() ([]byte, error)

RSS is synonym for xml.Marshal(f)

func (*Feed) SetDescription

func (f *Feed) SetDescription(s string)

SetDescription set feed description. Optional.

func (*Feed) SetLanguage

func (f *Feed) SetLanguage(l string)

SetLanguage sets feed language, default is set to en-us.

func (f *Feed) SetLink(url string)

SetLink for feed. Optional.

func (*Feed) SetTitle

func (f *Feed) SetTitle(s string)

SetTitle of feed. Optional.

type Figure

type Figure struct {
	Img        *Img    `xml:"img,omitempty"`
	IFrame     *IFrame `xml:"iframe,omitempty"`
	Video      *Video  `xml:"video,omitempty"`
	Figcaption string  `xml:"figcaption,omitempty"`
	Class      string  `xml:"class,attr,omitempty"`
}

Figure HTML <figure>

func (Figure) StartElement

func (f Figure) StartElement() xml.StartElement

StartElement for ContentElement interface

type IFrame

type IFrame struct {
	Src    string `xml:"src,attr,omitempty"`
	Height string `xml:"height,attr,omitempty"`
	Width  string `xml:"width,attr,omitempty"`
	Style  string `xml:"style,attr,omitempty"`
	Hidden string `xml:"hidden,attr,omitempty"`
	Text   string `xml:",innerxml"`
}

IFrame selement

type Img

type Img struct {
	Src string `xml:"src,attr"`
}

Img struct for images

type Meta

type Meta struct {
	Charset  string `xml:"charset,attr,omitempty"`
	Property string `xml:"property,attr,omitempty"`
	Content  string `xml:"content,attr,omitempty"`
}

Meta element

type P

type P struct {
	Text string `xml:",innerxml"`
}

P HTML <p>

func (P) StartElement

func (p P) StartElement() xml.StartElement

StartElement for ContentElement interface

type Time

type Time struct {
	Text     string `xml:",chardata"`
	Class    string `xml:"class,attr"`
	Datetime string `xml:"datetime,attr"`
}

Time struct for publish date and modified date

type Video

type Video struct {
	Source source `xml:"source"`
}

Video for article

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL