-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
99 lines (94 loc) · 5.39 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sub-Box</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<style type="text/css">
body { background: #f1f1f1; line-height: 1.3em; }
.main.container { font-size: 14px; }
#login-container { padding: 40px 16px; text-align: center; }
#video-container { margin: 10px 0; background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .1); }
.video { height: 210px; padding: 15px; display: flex; flex-direction: column; }
.video .title { overflow: hidden; max-height: 2.6em; font-weight: 500; }
.video .img-container { width: 180px; height: 100px; padding-bottom: 10px; }
.video img { width: 100%; height: 100%; object-fit: cover; }
.video .channel, .video .time { color: #767676; font-size: 12px; }
</style>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container"><div class="navbar-header"><a href="#" class="navbar-brand">Sub-Box</small></a></div></div>
</nav>
<div class="main container">
<div class="row">
<div id="login-container" class="pre-auth col-xs-12 col-sm-6 col-sm-offset-3">
<h1>Access to YouTube Required</h1>
<p class="lead">This application requires acces to your YouTube account in order to see what channels you're subscribed to.</p>
<button class="btn btn-primary" id="login-link">Authorize with OAuth</button>
</div>
</div>
<div class="row"><div id="video-container" class="col-xs-12"></div></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
var threeDaysAgo = new Date(); threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
checkAuth = () => gapi.auth.authorize({
client_id: '126126374554-rds5pbeaqeriq64jqfsnmgev16rnhn72.apps.googleusercontent.com',
scope: ['https://www.googleapis.com/auth/youtube'], immediate: false
}, handleAuthResult);
googleApiClientReady = () => gapi.auth.init(() => window.setTimeout(checkAuth, 1));
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
$('.pre-auth').hide();
gapi.client.load('youtube', 'v3', () => subscriptions(gapi.client.youtube));
} else {
$('#login-link').click(checkAuth);
}
}
function subscriptions(youtube, token) {
youtube.subscriptions.list({ mine: true, part: 'snippet', maxResults: 50, pageToken: token}).then(response => {
youtube.channels.list({
part: 'contentDetails', maxResults: 50,
id: response.result.items.map(sub => sub.snippet.resourceId.channelId).join(',')
}).then( response => {
var videos = [];
var batch = gapi.client.newBatch();
response.result.items.map(ch => ch.contentDetails.relatedPlaylists.uploads).forEach(
id => batch.add(youtube.playlistItems.list({ playlistId: id, part: 'snippet', maxResults: 5 }))
);
batch.then(responses => {
[].concat.apply(
[], Object.keys(responses.result).map( key => responses.result[key].result.items )
).filter( video => new Date(video.snippet.publishedAt) > threeDaysAgo )
.forEach( video => {
date = new Date(video.snippet.publishedAt);
videos.push({
date: date,
html: $('<div>').addClass('video col-xs-6 col-sm-4 col-md-2')
.append(
ytlink(video).append(
$('<div>').addClass('img-container').append(
$('<img>').attr({src: video.snippet.thumbnails.medium.url})
)
)
)
.append(ytlink(video).addClass('title').text(video.snippet.title))
.append($('<span>').addClass('channel').text(video.snippet.channelTitle))
.append($('<span>').addClass('time').text(date.toLocaleString()))
});
videos.sort( (a, b) => b.date - a.date );
}
);
$("#video-container").append(videos.map(vid => vid.html));
});
});
if(response.result.nextPageToken) subscriptions(youtube, response.result.nextPageToken);
});
}
ytlink = vid => $('<a>').attr({href: 'https://youtube.com/watch?v=' + vid.snippet.resourceId.videoId});
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
</html>