|  | 
|  | 1 | +package noembed | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"encoding/json" | 
|  | 5 | +	"errors" | 
|  | 6 | +	"io" | 
|  | 7 | +	"io/ioutil" | 
|  | 8 | +	"net/http" | 
|  | 9 | +	"regexp" | 
|  | 10 | +	"strings" | 
|  | 11 | +) | 
|  | 12 | + | 
|  | 13 | +var noembedURL = "https://noembed.com/embed?url={url}" | 
|  | 14 | + | 
|  | 15 | +// NoEmbed represents this package | 
|  | 16 | +type NoEmbed struct { | 
|  | 17 | +	data *Data | 
|  | 18 | +} | 
|  | 19 | + | 
|  | 20 | +// Data represents the data for noembed providers | 
|  | 21 | +type Data []struct { | 
|  | 22 | +	Name     string  `json:"name"` | 
|  | 23 | +	Patterns []Regex `json:"patterns"` | 
|  | 24 | +} | 
|  | 25 | + | 
|  | 26 | +// Response represents the data returned by noembed server | 
|  | 27 | +type Response struct { | 
|  | 28 | +	AuthorName      string `json:"author_name"` | 
|  | 29 | +	AuthorURL       string `json:"author_url"` | 
|  | 30 | +	ProviderName    string `json:"provider_name"` | 
|  | 31 | +	ProviderURL     string `json:"provider_url"` | 
|  | 32 | +	Title           string `json:"title"` | 
|  | 33 | +	Type            string `json:"type"` | 
|  | 34 | +	URL             string `json:"url"` | 
|  | 35 | +	HTML            string `json:"html"` | 
|  | 36 | +	Version         string `json:"version"` | 
|  | 37 | +	ThumbnailURL    string `json:"thumbnail_url"` | 
|  | 38 | +	ThumbnailWidth  int    `json:"thumbnail_width,string"` | 
|  | 39 | +	ThumbnailHeight int    `json:"thumbnail_height,string"` | 
|  | 40 | +	Width           int    `json:"width,string"` | 
|  | 41 | +	Height          int    `json:"height,string"` | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +// New returns a Noembed object | 
|  | 45 | +func New() *NoEmbed { | 
|  | 46 | +	return &NoEmbed{} | 
|  | 47 | +} | 
|  | 48 | + | 
|  | 49 | +// ParseProviders parses the raw json obtained from noembed.com | 
|  | 50 | +func (n *NoEmbed) ParseProviders(buf io.Reader) error { | 
|  | 51 | +	data, err := ioutil.ReadAll(buf) | 
|  | 52 | +	if err != nil { | 
|  | 53 | +		return err | 
|  | 54 | +	} | 
|  | 55 | + | 
|  | 56 | +	var noembedData Data | 
|  | 57 | +	err = json.Unmarshal(data, &noembedData) | 
|  | 58 | +	if err != nil { | 
|  | 59 | +		return err | 
|  | 60 | +	} | 
|  | 61 | + | 
|  | 62 | +	n.data = &noembedData | 
|  | 63 | +	return nil | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | +// Get returns a noembed response object | 
|  | 67 | +func (n *NoEmbed) Get(url string) (resp *Response, err error) { | 
|  | 68 | +	if !n.ValidURL(url) { | 
|  | 69 | +		err = errors.New("Unsupported URL") | 
|  | 70 | +		return | 
|  | 71 | +	} | 
|  | 72 | + | 
|  | 73 | +	reqURL := strings.Replace(noembedURL, "{url}", url, 1) | 
|  | 74 | + | 
|  | 75 | +	var httpResp *http.Response | 
|  | 76 | +	httpResp, err = http.Get(reqURL) | 
|  | 77 | +	if err != nil { | 
|  | 78 | +		return | 
|  | 79 | +	} | 
|  | 80 | +	defer httpResp.Body.Close() | 
|  | 81 | + | 
|  | 82 | +	var body []byte | 
|  | 83 | +	body, err = ioutil.ReadAll(httpResp.Body) | 
|  | 84 | +	if err != nil { | 
|  | 85 | +		return | 
|  | 86 | +	} | 
|  | 87 | + | 
|  | 88 | +	err = json.Unmarshal(body, &resp) | 
|  | 89 | +	if err != nil { | 
|  | 90 | +		return | 
|  | 91 | +	} | 
|  | 92 | + | 
|  | 93 | +	return | 
|  | 94 | +} | 
|  | 95 | + | 
|  | 96 | +// ValidURL is used to test if a url is supported by noembed | 
|  | 97 | +func (n *NoEmbed) ValidURL(url string) bool { | 
|  | 98 | +	for _, entry := range *n.data { | 
|  | 99 | +		for _, pattern := range entry.Patterns { | 
|  | 100 | +			if pattern.Regexp.MatchString(url) { | 
|  | 101 | +				return true | 
|  | 102 | +			} | 
|  | 103 | +		} | 
|  | 104 | +	} | 
|  | 105 | +	return false | 
|  | 106 | +} | 
|  | 107 | + | 
|  | 108 | +// Regex Unmarshaler | 
|  | 109 | +type Regex struct { | 
|  | 110 | +	regexp.Regexp | 
|  | 111 | +} | 
|  | 112 | + | 
|  | 113 | +// UnmarshalText used to unmarshal regexp's from text | 
|  | 114 | +func (r *Regex) UnmarshalText(text []byte) error { | 
|  | 115 | +	reg, err := regexp.Compile(string(text)) | 
|  | 116 | +	r.Regexp = *reg | 
|  | 117 | +	return err | 
|  | 118 | +} | 
0 commit comments