forked from njvillavicencio/GoogleDriveM3U
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
276 lines (242 loc) · 9.66 KB
/
index.html
File metadata and controls
276 lines (242 loc) · 9.66 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html>
<head>
<title>Video file streaming</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="./style/style.css" />
<link rel="icon" sizes="200x200" type="image/png" href="./style/play.png">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Poppins" rel="stylesheet" type="text/css">
</head>
<body>
<nav class="topnav">
<a id="home" href="./index.html">Home</a>
<a id="login">Log in</a>
<a id="connect">Disconnect</a>
</nav>
<div style="line-height:10px">
<a href="./index.html"><img id="reader" src="./style/tv.png" width = 90px style='float: left;'/></a>
<h1><span class="black"><a href="./index.html">m3u</a></span></h1>
<h1><a href="./index.html"><span class="grey">Google</span> <span class="grey-light">Drive</span></a></h1>
</div>
<h2>Directly from the Cloud</h2>
<br/>
<!--Add buttons to initiate auth sequence and sign out-->
<button type="button" id="auth" enabled>Authenticate</button>
<div id="result" style="display:none"></div>
<pre id="content"></pre>
<a href="#" id="nextPage" style="display: none;" >Ver más »</a>
<script type="text/javascript">
var CLIENT_ID = '983869916425-495k0u2mf6i377r6c04d1nkleek9qbbj.apps.googleusercontent.com';
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
var SCOPES = 'https://www.googleapis.com/auth/drive';
var pickerApiLoaded = false;
var API_KEY = 'AIzaSyBa-rF9qjubzf5GM0I5vI847KeXv-0TS5A';
var PATH="https://www.googleapis.com/drive/v3/files/"
var q="?alt=media&key=";
var oauthToken;
var login = document.getElementById('login');
var connect = document.getElementById('connect');
var initialized=false;
function handleClientLoad() {
gapi.load('client:auth2', initClient);
<!-- gapi.client.load('drive', 'v3'); -->
gapi.load('picker', onPickerApiLoad);
}
function initClient() {
gapi.client.init({
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
//login.href="javascript:handleAuthClick()";
//gapi.auth2.authorize({client_id: CLIENT_ID, scope: SCOPES}, handleAuthResult);
});
}
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
login.href="javascript:handleSignoutClick()";
login.innerHTML="Log out";
connect.href="javascript:disconnect()";
oauthToken = gapi.auth.getToken().access_token;
}
else {
login.href="javascript:handleAuthClick()";
login.innerHTML="Log in";
}
}
function listFiles(query,nextPageToken="",fileType="",currentLevelFolderList=[],folderList=[]) {
gapi.client.drive.files.list({
pageSize: 1000, //no puede ser menor a 4
'pageToken': nextPageToken,
includeTeamDrives : true,
fields: "nextPageToken, files(id, name, mimeType, fileExtension, webContentLink, parents, properties)",
orderBy:"name,folder",
q: query
}).then(function(response) {
var files = response.result.files.concat(currentLevelFolderList);
if (response.result.nextPageToken && fileType=="folder"){
listFiles(query, response.result.nextPageToken, "folder", files);
}
else{
if (files && files.length > 0 && fileType=="video") {
buildM3U(files,folderList);
}
else if (files && files.length > 0) {
var query;
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (i==0){
query="('"+file.id+"' in parents";
}
else {
query+=" or '"+file.id+"' in parents";
}
}
query+=") and mimeType='application/vnd.google-apps.folder'";
files=folderList.concat(files);
listFiles(query,"","folder",[],files);
}
else {
files=folderList;
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (i==0){
query="('"+file.id+"' in parents";
}
else {
query+=" or '"+file.id+"' in parents";
}
}
query+=") and (mimeType contains 'video' or mimeType contains 'image')";
var delayInMilliseconds = 1000; //1 second
setTimeout(listFiles(query,"","video",[],folderList), delayInMilliseconds);
}
}
});
}
function buildM3U(files,folderList) {
var videoFilesTitles={};
for (var i = 0; i<folderList.length; i++){
var key=folderList[i].id;
videoFilesTitles[key]=folderList[i].name;
}
var videoFiles={};
var parents=[];
var video="";
var logo="";
var name="";
var keys=[];
for (var i = 0; i < files.length; i++) {
var file = files[i];
keys = Object.keys(videoFiles);
if (keys.indexOf(file.parents[0])<0){
videoFiles[file.parents[0]]={"video":"","title":"","logo":""};
}
if (file.mimeType.indexOf("video") != -1) {
videoFiles[file.parents[0]].video=file.id;
videoFiles[file.parents[0]].title=videoFilesTitles[file.parents[0]];
}
else if (file.mimeType.indexOf("image") != -1) {
videoFiles[file.parents[0]].logo=file.id;
}
}
buildM3U2(videoFiles);
}
function buildM3U2(videoFiles) {
var keys = Object.keys(videoFiles);
var video="";
var logo="";
var m3u="#EXTM3U"+"\n\n";
for (var i = 0; i < keys.length; i++) {
var file = keys[i];
var video=videoFiles[file].video;
var logo=videoFiles[file].logo;
var name=videoFiles[file].title;
if (video!="") {
videoLink=PATH + video + q + API_KEY;
if (logo!="") {
logoLink=PATH + logo + q + API_KEY;
m3u+= '#EXTINF:-1 tvg-logo="'+logoLink+'"'+" group-title='Movies' type='video', "+name+"\n" + videoLink + "\n\n\n";
}
else {
m3u+= "#EXTINF:-1 group-title='Movies' type='video', "+name+"\n" + videoLink + "\n\n\n";
}
}
}
//document.getElementById('result').style.display = "none";
document.getElementById('result').innerHTML='<textarea id="m3u" style="display: none"></textarea>';
document.getElementById("m3u").value=m3u;
document.getElementById("m3u").style.display="block";
}
function onPickerApiLoad() {
pickerApiLoaded = true;
var authBtn = document.getElementById('auth');
//authBtn.disabled = false;
authBtn.setAttribute("onclick","createPicker()");
}
function createPicker() {
if (pickerApiLoaded && oauthToken) {
document.getElementById('result').style.display = "none";
var view = new google.picker.DocsView()
.setOwnedByMe(true)
.setIncludeFolders(true)
.setSelectFolderEnabled(true)
.setMimeTypes('application/vnd.google-apps.folder');
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.setTitle('Select a folder to use for the playlist')
.addView(view)
.setOAuthToken(oauthToken)
// .setDeveloperKey('AIzaSyAksMFVtrWBKqN2gaDnP29FdpB3omikrQU')
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
}
else if (pickerApiLoaded) {
handleAuthClick();
}
}
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
query="'"+data.docs[0].id+"' in parents and mimeType='application/vnd.google-apps.folder'";
document.getElementById('result').innerHTML = '<img src="./style/loading.gif" class="center" width = 90px>';
document.getElementById('result').style.display = "block";
listFiles(query,"","folder",[],data.docs);
}
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick(event) {
initialized=true;
gapi.auth2.getAuthInstance().signIn({ux_mode: "redirect"});
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut().then(function () {
location.reload();
});
}
function disconnect(event) {
gapi.auth2.getAuthInstance().disconnect().then(function () {
signoutButton.click();
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
</body>
</html>