Skip to content

Commit

Permalink
Added handler for shared playlists from the YouTube app (#472)
Browse files Browse the repository at this point in the history
- also fixed missing "/play/" segment in Vimeo plugin url
  • Loading branch information
monzee authored and SyncedSynapse committed Oct 17, 2017
1 parent 9e65350 commit 41a433c
Showing 1 changed file with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,28 +346,17 @@ private void handleStartIntent(Intent intent) {
!(action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_VIEW)))
return;

Uri videoUri = null;
Uri videoUri;
if (action.equals(Intent.ACTION_SEND)) {
// Get the URI, which is stored in Extras
videoUri = getYouTubeUri(intent.getStringExtra(Intent.EXTRA_TEXT));
if (videoUri == null) return;
} else if (action.equals(Intent.ACTION_VIEW)) {
if (intent.getData() == null) return;
videoUri = Uri.parse(intent.getData().toString());
} else {
videoUri = intent.getData();
}
if (videoUri == null) return;

final String videoId = getVideoId(videoUri);
String videoUrl;
if (videoId != null) {
if (videoUri.getHost().endsWith("svtplay.se")) {
videoUrl = "plugin://plugin.video.svtplay/?url=%2Fvideo%2F" + URLEncoder.encode(videoId) + "&mode=video";
} else if (videoUri.getHost().endsWith("vimeo.com")) {
videoUrl = "plugin://plugin.video.vimeo?video_id=" + videoId;
} else {
videoUrl = "plugin://plugin.video.youtube/play/?video_id=" + videoId;
}

} else {
String videoUrl = toPluginUrl(videoUri);
if (videoUrl == null) {
videoUrl = videoUri.toString();
}

Expand Down Expand Up @@ -505,35 +494,42 @@ private Uri getYouTubeUri(String extraText) {
}

/**
* Returns the youtube/vimeo video ID from its URL
* Converts a video url to a Kodi plugin URL.
*
* @param playuri Youtube/Vimeo URL
* @return Youtube/Vimeo Video ID
* @param playuri some URL
* @return plugin URL
*/
private String getVideoId(Uri playuri) {
if (playuri.getHost().endsWith("svtplay.se") || playuri.getHost().endsWith("youtube.com") || playuri.getHost().endsWith("youtu.be") || playuri.getHost().endsWith("vimeo.com")) {
// We'll need to get the v= parameter from the URL
final Pattern pattern;
if (playuri.getHost().endsWith("svtplay.se")) {
pattern = Pattern.compile("^(?:https?:\\/\\/)?(?:www\\.)?svtplay\\.se\\/video\\/(\\d+\\/.*)",
Pattern.CASE_INSENSITIVE);
} else if (playuri.getHost().endsWith("vimeo.com")) {
pattern = Pattern.compile("^(?:https?:\\/\\/)?(?:www\\.|player\\.)?vimeo\\.com\\/(?:.*\\/)?(\\d+)(?:\\?.*)?$",
Pattern.CASE_INSENSITIVE);
private String toPluginUrl(Uri playuri) {
String host = playuri.getHost();
if (host.endsWith("svtplay.se")) {
Pattern pattern = Pattern.compile(
"^(?:https?:\\/\\/)?(?:www\\.)?svtplay\\.se\\/video\\/(\\d+\\/.*)",
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(playuri.toString());
if (matcher.matches()) {
return "plugin://plugin.video.svtplay/?url=%2Fvideo%2F"
+ URLEncoder.encode(matcher.group(1)) + "&mode=video";
}
else {
pattern = Pattern.compile("^(?:https?:\\/\\/)?(?:www\\.|m\\.)?youtu(?:\\.be\\/|be\\.com\\/watch\\?v=)([\\w-]+)(?:[\\?&].*)?$",
Pattern.CASE_INSENSITIVE);
} else if (host.endsWith("vimeo.com")) {
String last = playuri.getLastPathSegment();
if (last.matches("\\d+")) {
return "plugin://plugin.video.vimeo/play/?video_id=" + last;
}
final Matcher matcher = pattern.matcher(playuri.toString());
if (matcher.matches()) {
return matcher.group(1);
} else if (host.endsWith("youtube.com")) {
if (playuri.getPathSegments().contains("playlist")) {
return "plugin://plugin.video.youtube/play/?order=default&playlist_id="
+ playuri.getQueryParameter("list");
} else {
return "plugin://plugin.video.youtube/play/?video_id="
+ playuri.getQueryParameter("v");
}
} else if (host.endsWith("youtu.be")) {
return "plugin://plugin.video.youtube/play/?video_id="
+ playuri.getLastPathSegment();
}
return null;
}


// Default page change listener, that doesn't scroll images
ViewPager.OnPageChangeListener defaultOnPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
Expand Down

0 comments on commit 41a433c

Please sign in to comment.