Skip to content
Snippets Groups Projects
Commit fc75b0cd authored by Frédéric Guillot's avatar Frédéric Guillot
Browse files

Add workaround to get YouTube feed from video page

parent 7380c641
No related branches found
No related tags found
No related merge requests found
...@@ -22,11 +22,13 @@ import ( ...@@ -22,11 +22,13 @@ import (
var ( var (
errUnreadableDoc = "Unable to analyze this page: %v" errUnreadableDoc = "Unable to analyze this page: %v"
youtubeChannelRegex = regexp.MustCompile(`youtube\.com/channel/(.*)`) youtubeChannelRegex = regexp.MustCompile(`youtube\.com/channel/(.*)`)
youtubeVideoRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
) )
// FindSubscriptions downloads and try to find one or more subscriptions from an URL. // FindSubscriptions downloads and try to find one or more subscriptions from an URL.
func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) { func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
websiteURL = findYoutubeChannelFeed(websiteURL) websiteURL = findYoutubeChannelFeed(websiteURL)
websiteURL = parseYoutubeVideoPage(websiteURL)
request := client.New(websiteURL) request := client.New(websiteURL)
request.WithCredentials(username, password) request.WithCredentials(username, password)
...@@ -48,14 +50,15 @@ func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscr ...@@ -48,14 +50,15 @@ func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscr
return subscriptions, nil return subscriptions, nil
} }
subscriptions, err := parseDocument(response.EffectiveURL, strings.NewReader(body)) subscriptions, err := parseWebPage(response.EffectiveURL, strings.NewReader(body))
if err != nil || subscriptions != nil { if err != nil || subscriptions != nil {
return subscriptions, err return subscriptions, err
} }
return tryWellKnownUrls(websiteURL, userAgent, username, password) return tryWellKnownUrls(websiteURL, userAgent, username, password)
} }
func parseDocument(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) { func parseWebPage(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) {
var subscriptions Subscriptions var subscriptions Subscriptions
queries := map[string]string{ queries := map[string]string{
"link[type='application/rss+xml']": "rss", "link[type='application/rss+xml']": "rss",
...@@ -105,6 +108,29 @@ func findYoutubeChannelFeed(websiteURL string) string { ...@@ -105,6 +108,29 @@ func findYoutubeChannelFeed(websiteURL string) string {
return websiteURL return websiteURL
} }
func parseYoutubeVideoPage(websiteURL string) string {
if !youtubeVideoRegex.MatchString(websiteURL) {
return websiteURL
}
request := client.New(websiteURL)
response, browserErr := browser.Exec(request)
if browserErr != nil {
return websiteURL
}
doc, docErr := goquery.NewDocumentFromReader(response.Body)
if docErr != nil {
return websiteURL
}
if channelID, exists := doc.Find(`meta[itemprop="channelId"]`).First().Attr("content"); exists {
return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, channelID)
}
return websiteURL
}
func tryWellKnownUrls(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) { func tryWellKnownUrls(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
var subscriptions Subscriptions var subscriptions Subscriptions
knownURLs := map[string]string{ knownURLs := map[string]string{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment