Introduce "--images" flag to enable image link parsing and disable image link parsing by default.

develop
Icedream 2016-07-26 20:36:36 +02:00
parent 6d70e02641
commit b37a15ac1f
Signed by: icedream
GPG Key ID: 1573F6D8EFE4D0CF
2 changed files with 32 additions and 16 deletions

12
main.go
View File

@ -32,6 +32,8 @@ func main() {
var soundcloudClientId string var soundcloudClientId string
var soundcloudClientSecret string var soundcloudClientSecret string
var webEnableImages bool
var debug bool var debug bool
var useTLS bool var useTLS bool
var server string var server string
@ -58,9 +60,14 @@ func main() {
// Youtube config // Youtube config
kingpin.Flag("youtube-key", "The API key to use to access the YouTube API.").StringVar(&youtubeApiKey) kingpin.Flag("youtube-key", "The API key to use to access the YouTube API.").StringVar(&youtubeApiKey)
// SoundCloud config
kingpin.Flag("soundcloud-id", "The SoundCloud ID.").StringVar(&soundcloudClientId) kingpin.Flag("soundcloud-id", "The SoundCloud ID.").StringVar(&soundcloudClientId)
kingpin.Flag("soundcloud-secret", "The SoundCloud secret.").StringVar(&soundcloudClientSecret) kingpin.Flag("soundcloud-secret", "The SoundCloud secret.").StringVar(&soundcloudClientSecret)
// Web parser config
kingpin.Flag("images", "Enables parsing links of images. Disabled by default for legal reasons.").BoolVar(&webEnableImages)
kingpin.Parse() kingpin.Parse()
if len(nickname) == 0 { if len(nickname) == 0 {
@ -100,7 +107,10 @@ func main() {
must(m.RegisterParser(new(wikipedia.Parser))) must(m.RegisterParser(new(wikipedia.Parser)))
// Load web parser // Load web parser
must(m.RegisterParser(new(web.Parser))) webParser := &web.Parser{
EnableImages: webEnableImages,
}
must(m.RegisterParser(webParser))
// IRC // IRC
conn := m.AntifloodIrcConn(irc.IRC(nickname, ident)) conn := m.AntifloodIrcConn(irc.IRC(nickname, ident))

View File

@ -33,7 +33,9 @@ const (
maxHtmlSize = 8 * 1024 maxHtmlSize = 8 * 1024
) )
type Parser struct{} type Parser struct {
EnableImages bool
}
func (p *Parser) Init() error { func (p *Parser) Init() error {
return nil return nil
@ -119,23 +121,27 @@ func (p *Parser) Parse(u *url.URL, referer *url.URL) (result parsers.ParseResult
result.Information[0]["Title"] = noTitleStr result.Information[0]["Title"] = noTitleStr
} }
case "image/png", "image/jpeg", "image/gif": case "image/png", "image/jpeg", "image/gif":
if p.EnableImages {
// No need to limit the reader to a specific size here as // No need to limit the reader to a specific size here as
// image.DecodeConfig only reads as much as needed anyways. // image.DecodeConfig only reads as much as needed anyways.
if m, imgType, err := image.DecodeConfig(resp.Body); err != nil { if m, imgType, err := image.DecodeConfig(resp.Body); err != nil {
result.UserError = ErrCorruptedImage result.UserError = ErrCorruptedImage
} else { } else {
info := map[string]interface{}{ info := map[string]interface{}{
"IsUpload": true, "IsUpload": true,
"ImageSize": image.Point{X: m.Width, Y: m.Height}, "ImageSize": image.Point{X: m.Width, Y: m.Height},
"ImageType": strings.ToUpper(imgType), "ImageType": strings.ToUpper(imgType),
}
if resp.ContentLength > 0 {
info["Size"] = uint64(resp.ContentLength)
}
result.Information = []map[string]interface{}{info}
} }
if resp.ContentLength > 0 { break
info["Size"] = uint64(resp.ContentLength)
}
result.Information = []map[string]interface{}{info}
log.Printf("Got through: %+v!", info)
} }
fallthrough
default: default:
// TODO - Implement generic head info? // TODO - Implement generic head info?
result.Ignored = true result.Ignored = true