-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make fallback embed more generalised && bundle html templates in the …
…binary
- Loading branch information
1 parent
9dded0a
commit 69dd56e
Showing
8 changed files
with
346 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package fallbackembed | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// FallbackEmbed represents this package | ||
type FallbackEmbed struct { | ||
data *Data | ||
httpClient *http.Client | ||
targetKey string | ||
providerURL string | ||
} | ||
|
||
// Data represents the data for FallbackEmbed providers | ||
type Data []struct { | ||
Name string `json:"name"` | ||
Patterns []Regex `json:"patterns"` | ||
} | ||
|
||
// New returns a FallbackEmbed object | ||
func New(providerURL, targetKey string) *FallbackEmbed { | ||
obj := &FallbackEmbed{ | ||
httpClient: &http.Client{ | ||
Timeout: time.Second * 30, | ||
}, | ||
providerURL: providerURL, | ||
targetKey: targetKey, | ||
} | ||
|
||
return obj | ||
} | ||
|
||
// ParseProviders parses the raw json obtained from noembed.com | ||
func (f *FallbackEmbed) ParseProviders(buf io.Reader) error { | ||
data, err := ioutil.ReadAll(buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var providerData Data | ||
err = json.Unmarshal(data, &providerData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f.data = &providerData | ||
return nil | ||
} | ||
|
||
// Get returns html string | ||
func (f *FallbackEmbed) Get(url string, width int, height int) (html string, err error) { | ||
if !f.ValidURL(url) { | ||
return | ||
} | ||
|
||
// Do replacements | ||
reqURL := strings.Replace(f.providerURL, "{url}", url, 1) | ||
reqURL = strings.Replace(reqURL, "{width}", strconv.Itoa(width), 1) | ||
reqURL = strings.Replace(reqURL, "{height}", strconv.Itoa(height), 1) | ||
|
||
var httpResp *http.Response | ||
httpResp, err = f.httpClient.Get(reqURL) | ||
if err != nil { | ||
return | ||
} | ||
defer httpResp.Body.Close() | ||
|
||
var body []byte | ||
body, err = ioutil.ReadAll(httpResp.Body) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Try to parse json response | ||
resp := make(map[string]interface{}) | ||
err = json.Unmarshal(body, &resp) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Check targetKey exists | ||
if jsonVal, ok := resp[f.targetKey]; ok { | ||
// Check targetVal is string | ||
if htmlString, ok := jsonVal.(string); ok { | ||
html = htmlString | ||
return | ||
} | ||
} | ||
|
||
err = errors.New("Failed to get target json key") | ||
return | ||
} | ||
|
||
// ValidURL is used to test if a url is supported by noembed | ||
func (f *FallbackEmbed) ValidURL(url string) bool { | ||
for _, entry := range *f.data { | ||
for _, pattern := range entry.Patterns { | ||
if pattern.Regexp.MatchString(url) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Regex Unmarshaler | ||
type Regex struct { | ||
regexp.Regexp | ||
} | ||
|
||
// UnmarshalText used to unmarshal regexp's from text | ||
func (r *Regex) UnmarshalText(text []byte) error { | ||
reg, err := regexp.Compile(string(text)) | ||
r.Regexp = *reg | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
const templatesDir = "./templates" | ||
|
||
func main() { | ||
files, _ := ioutil.ReadDir(templatesDir) | ||
|
||
out, _ := os.Create(path.Join(templatesDir, "templates.go")) | ||
out.Write([]byte("package templates\n\nvar Get = map[string]string{\n")) | ||
for _, fileInfo := range files { | ||
if strings.HasSuffix(fileInfo.Name(), ".html") { | ||
out.Write([]byte(strings.TrimSuffix(fileInfo.Name(), ".html") + ": `")) | ||
file, _ := os.Open(path.Join(templatesDir, fileInfo.Name())) | ||
io.Copy(out, file) | ||
out.Write([]byte("`,\n")) | ||
} | ||
} | ||
out.Write([]byte("}\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.