Просмотр исходного кода

Parse video urls a little more robustly

Non-https urls and "youtube.com" (without "www") urls are now supported.
Lucas Stadler лет назад: 9
Родитель
Сommit
dcca89e179
1 измененных файлов с 2 добавлено и 7 удалено
  1. 2 7
      go/blog/blog.go

+ 2 - 7
go/blog/blog.go

11
	"io/ioutil"
11
	"io/ioutil"
12
	"net/url"
12
	"net/url"
13
	"os"
13
	"os"
14
	"strings"
15
14
16
	"github.com/russross/blackfriday"
15
	"github.com/russross/blackfriday"
17
	"gopkg.in/yaml.v2"
16
	"gopkg.in/yaml.v2"
128
		case "text":
127
		case "text":
129
			err = textTmpl.Execute(os.Stdout, post)
128
			err = textTmpl.Execute(os.Stdout, post)
130
		case "video":
129
		case "video":
131
			if !strings.HasPrefix(post.URL, "https://www.youtube.com/watch") {
132
				exit(fmt.Errorf("can't handle video '%s'", post.URL))
133
			}
134
135
			u, err := url.Parse(post.URL)
130
			u, err := url.Parse(post.URL)
136
			if err != nil {
131
			if err != nil {
137
				exit(fmt.Errorf("invalid video url '%s'", post.URL))
132
				exit(fmt.Errorf("invalid video url '%s'", post.URL))
138
			}
133
			}
139
			id := u.Query().Get("v")
134
			id := u.Query().Get("v")
140
			if id == "" {
141
				exit(fmt.Errorf("invalid video url '%s'", post.URL))
135
			if (u.Host != "youtube.com" && u.Host != "www.youtube.com") || id == "" {
136
				exit(fmt.Errorf("unsupported video url '%s'", post.URL))
142
			}
137
			}
143
			post.URL = id
138
			post.URL = id
144
			err = videoTmpl.Execute(os.Stdout, post)
139
			err = videoTmpl.Execute(os.Stdout, post)