aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/players/Youtube.hx
blob: 15eb014216fc71332e5adefcb3c0050150fc545f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package client.players;

import haxe.Json;
import haxe.Http;
import js.html.Element;
import js.Browser.document;
import client.Main.ge;
import js.youtube.Youtube as YtInit;
import js.youtube.YoutubePlayer;
import Types.VideoData;
import Types.VideoItem;
using StringTools;

class Youtube implements IPlayer {

	static final matchId = ~/v=([A-z0-9_-]+)/;
	static final matchShort = ~/youtu.be\/([A-z0-9_-]+)/;
	static final matchEmbed = ~/embed\/([A-z0-9_-]+)/;
	static final apiUrl = "https://www.googleapis.com/youtube/v3/videos";
	static final urlTitleDuration = "?part=snippet,contentDetails&fields=items(snippet/title,contentDetails/duration)";
	static final apiKey = "AIzaSyDTk1OPRI9cDkAK_BKsBcv10DQCHse-QaA";
	final main:Main;
	final player:Player;
	final playerEl:Element = ge("#ytapiplayer");
	var video:Element;
	var youtube:YoutubePlayer;
	var isLoaded = false;

	public function new(main:Main, player:Player) {
		this.main = main;
		this.player = player;
	}

	public static function isYoutube(url:String):Bool {
		return extractVideoId(url) != "";
	}

	static function extractVideoId(url:String):String {
		if (url.contains("youtu.be/")) {
			matchShort.match(url);
			return matchShort.matched(1);
		}
		if (url.contains("youtube.com/embed/")) {
			matchEmbed.match(url);
			return matchEmbed.matched(1);
		}
		if (!matchId.match(url)) return "";
		return matchId.matched(1);
	}

	final matchHours = ~/([0-9]+)H/;
	final matchMinutes = ~/([0-9]+)M/;
	final matchSeconds = ~/([0-9]+)S/;

	function convertTime(duration:String):Float {
		var total = 0;
		final hours = matchHours.match(duration);
		final minutes = matchMinutes.match(duration);
		final seconds = matchSeconds.match(duration);
		if (hours) total += Std.parseInt(matchHours.matched(1)) * 3600;
		if (minutes) total += Std.parseInt(matchMinutes.matched(1)) * 60;
		if (seconds) total += Std.parseInt(matchSeconds.matched(1));
		return total;
	}

	public function getVideoData(url:String, callback:(data:VideoData)->Void):Void {
		final id = extractVideoId(url);
		final url = '$apiUrl$urlTitleDuration&id=$id&key=$apiKey';
		final http = new Http(url);
		http.onData = data -> {
			final json = Json.parse(data);
			if (json.error != null) {
				getRemoteDataFallback(url, callback);
				return;
			}
			final item = json.items[0];
			if (item == null) {
				callback({duration: 0});
				return;
			}
			final title:String = item.snippet.title;
			final duration:String = item.contentDetails.duration;
			// TODO duration is PT0S for streams
			callback({
				duration: convertTime(duration),
				title: title
			});
		}
		http.onError = msg -> getRemoteDataFallback(url, callback);
		http.request();
	}

	function getRemoteDataFallback(url:String, callback:(data:VideoData)->Void):Void {
		if (!YtInit.isLoadedAPI) {
			YtInit.init(() -> getRemoteDataFallback(url, callback));
			return;
		}
		final video = document.createDivElement();
		video.id = "temp-videoplayer";
		Utils.prepend(playerEl, video);
		youtube = new YoutubePlayer(video.id, {
			videoId: extractVideoId(url),
			playerVars: {
				modestbranding: 1,
				rel: 0,
				showinfo: 0
			},
			events: {
				onReady: e -> {
					if (playerEl.contains(video)) playerEl.removeChild(video);
					callback({
						duration: youtube.getDuration()
					});
				},
				onError: e -> {
					// TODO message error codes
					trace('Error ${e.data}');
					if (playerEl.contains(video)) playerEl.removeChild(video);
					callback({duration: 0});
				}
			}
		});
	}

	public function loadVideo(item:VideoItem):Void {
		if (!YtInit.isLoadedAPI) {
			YtInit.init(() -> loadVideo(item));
			return;
		}
		video = document.createDivElement();
		video.id = "videoplayer";
		playerEl.appendChild(video);

		youtube = new YoutubePlayer(video.id, {
			videoId: extractVideoId(item.url),
			playerVars: {
				autoplay: 1,
				modestbranding: 1,
				rel: 0,
				showinfo: 0,
				start: 0
			},
			events: {
				onReady: e -> isLoaded = true,
				onStateChange: e -> {
					switch (e.data) {
						case UNSTARTED:
							player.onCanBePlayed();
						case ENDED:
						case PLAYING:
							player.onPlay();
						case PAUSED:
							player.onPause();
						case BUFFERING:
							player.onSetTime();
						case CUED:
					}
				}
			}
		});
	}

	public function removeVideo():Void {
		if (video == null) return;
		if (playerEl.contains(video)) playerEl.removeChild(video);
		video = null;
	}

	public function play():Void {
		if (!isLoaded) return;
		youtube.playVideo();
	}

	public function pause():Void {
		if (!isLoaded) return;
		youtube.pauseVideo();
	}

	public function getTime():Float {
		if (!isLoaded) return 0;
		return youtube.getCurrentTime();
	}

	public function setTime(time:Float):Void {
		if (!isLoaded) return;
		youtube.seekTo(time, true);
	}

}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage