aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/players/Streamable.hx
blob: 21ef0f0fcf33a0a63175f28e9fd6a397aa4bb8ab (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
package client.players;

import Types.VideoData;
import Types.VideoDataRequest;
import haxe.DynamicAccess;
import haxe.Http;
import haxe.Json;

class Streamable extends Raw {
	final matchStreamable = ~/streamable\.com\/(.+)/g;
	final matchBadStreamableId = ~/[^0-9A-z-_]/g;

	override function isSupportedLink(url:String):Bool {
		if (!matchStreamable.match(url)) return false;
		final id = matchStreamable.matched(1);
		if (matchBadStreamableId.match(id)) return false;
		return true;
	}

	override function getVideoData(data:VideoDataRequest, callback:(data:VideoData) -> Void) {
		getStreamableVideoData(data.url, info -> {
			if (info == null) {
				callback({duration: 0});
				return;
			}

			getRawVideoData({url: info.url, atEnd: data.atEnd}, data -> {
				// set new url instead of using input one to load video
				data.url = info.url;
				data.title = info.title;
				callback(data);
			});
		});
	}

	function getRawVideoData(data:VideoDataRequest, callback:(data:VideoData) -> Void):Void {
		super.getVideoData(data, callback);
	}

	function getStreamableVideoData(url:String, callback:(info:Null<{url:String, title:String}>) -> Void):Void {
		if (!matchStreamable.match(url)) {
			callback(null);
			return;
		}
		final id = matchStreamable.matched(1);
		final http = new Http('https://api.streamable.com/videos/$id');
		http.onData = text -> {
			try {
				final json:{title:String, ?files:DynamicAccess<Dynamic>} = Json.parse(text);
				final files = json?.files;
				var item = files["mp4"];
				if (item == null) {
					final key = files.keys()[0];
					item = files[key];
				}
				callback({
					url: item.url,
					title: json.title
				});
			} catch (e) {
				callback(null);
			}
		}
		http.request();
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage