Skip to content

Commit

Permalink
feat: remove YouTube video page subscription finder because `meta[ite…
Browse files Browse the repository at this point in the history
…mprop="channelId"]` no longer exists
  • Loading branch information
fguillot committed Jul 13, 2024
1 parent 79ea9e2 commit cb97d4a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 64 deletions.
42 changes: 0 additions & 42 deletions internal/reader/subscription/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type youtubeKind string

const (
youtubeIDKindChannel youtubeKind = "channel"
youtubeIDKindVideo youtubeKind = "video"
youtubeIDKindPlaylist youtubeKind = "playlist"
)

Expand Down Expand Up @@ -102,12 +101,6 @@ func (f *SubscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string)
if localizedError != nil {
return nil, localizedError
}
case youtubeIDKindVideo:
slog.Debug("Try to detect feeds from YouTube video page", slog.String("website_url", websiteURL))
subscriptions, localizedError = f.FindSubscriptionsFromYouTubeVideoPage(websiteURL)
if localizedError != nil {
return nil, localizedError
}
case youtubeIDKindPlaylist:
slog.Debug("Try to detect feeds from YouTube playlist page", slog.String("website_url", websiteURL))
subscriptions, localizedError = f.FindSubscriptionsFromYouTubePlaylistPage(websiteURL)
Expand Down Expand Up @@ -319,37 +312,6 @@ func (f *SubscriptionFinder) FindSubscriptionsFromYouTubeChannelPage(websiteURL
return nil, nil
}

func (f *SubscriptionFinder) FindSubscriptionsFromYouTubeVideoPage(websiteURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
kind, _, err := youtubeURLIDExtractor(websiteURL)
if err != nil {
slog.Debug("Could not parse url", slog.String("website_url", websiteURL))
}

if kind != youtubeIDKindVideo {
slog.Debug("This website is not a YouTube video page, the regex doesn't match", slog.String("website_url", websiteURL))
return nil, nil
}

responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(websiteURL))
defer responseHandler.Close()

if localizedError := responseHandler.LocalizedError(); localizedError != nil {
return nil, localizedError
}

doc, docErr := goquery.NewDocumentFromReader(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
if docErr != nil {
return nil, locale.NewLocalizedErrorWrapper(docErr, "error.unable_to_parse_html_document", docErr)
}

if channelID, exists := doc.Find(`meta[itemprop="channelId"]`).First().Attr("content"); exists {
feedURL := fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, channelID)
return Subscriptions{NewSubscription(websiteURL, feedURL, parser.FormatAtom)}, nil
}

return nil, nil
}

func (f *SubscriptionFinder) FindSubscriptionsFromYouTubePlaylistPage(websiteURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
kind, id, _ := youtubeURLIDExtractor(websiteURL)

Expand Down Expand Up @@ -385,10 +347,6 @@ func youtubeURLIDExtractor(websiteURL string) (idKind youtubeKind, id string, er
idKind = youtubeIDKindPlaylist
id = decodedUrl.Query().Get("list")
return
case strings.HasPrefix(decodedUrl.Path, "/watch"):
idKind = youtubeIDKindVideo
id = decodedUrl.Query().Get("v")
return
case strings.HasPrefix(decodedUrl.Path, "/playlist"):
idKind = youtubeIDKindPlaylist
id = decodedUrl.Query().Get("list")
Expand Down
22 changes: 0 additions & 22 deletions internal/reader/subscription/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,13 @@ func TestFindYoutubePlaylistFeed(t *testing.T) {
}
}

func TestItDoesNotConsiderPlaylistWatchPageAsVideoWatchPage(t *testing.T) {
_, localizedError := NewSubscriptionFinder(nil).FindSubscriptionsFromYouTubeVideoPage("https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM")
if localizedError != nil {
t.Fatalf(`Should not consider a playlist watch page as a video watch page`)
}
}

func TestYoutubeIdExtractor(t *testing.T) {
type testResult struct {
ID string
Kind youtubeKind
error error
}
urls := map[string]testResult{
"https://www.youtube.com/watch?v=dQw4w9WgXcQ": {
ID: "dQw4w9WgXcQ",
Kind: youtubeIDKindVideo,
error: nil,
},
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=1": {
ID: "dQw4w9WgXcQ",
Kind: youtubeIDKindVideo,
error: nil,
},
"https://www.youtube.com/watch?t=1&v=dQw4w9WgXcQ": {
ID: "dQw4w9WgXcQ",
Kind: youtubeIDKindVideo,
error: nil,
},
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM": {
ID: "PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
Kind: youtubeIDKindPlaylist,
Expand Down

0 comments on commit cb97d4a

Please sign in to comment.