Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
js3
  • Loading branch information
ligopsychakis-michalis committed Mar 1, 2020
commit e8541fcf699fda26158f42eb0497fe6be374a50d
Binary file modified Week1/.DS_Store
Binary file not shown.
Binary file added Week1/homework/.DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file modified Week2/.DS_Store
Binary file not shown.
Binary file added Week2/homework/.DS_Store
Binary file not shown.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added Week3/.DS_Store
Binary file not shown.
Binary file added Week3/homework/.DS_Store
Binary file not shown.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions Week3/homework/y-tube-api/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MY Y-T API</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

<style>
#content,#authorize-button,#signout-button { display: none; }
</style>
</head>
<body>
<nav class="black">
<div class="nav-wrapper">
<div class="container">
<a href="#!" class="brand-logo">You Tube Channel Data</a>
</div>
</div>
</nav>
<br>
<section>
<div class="container">
<p>Log In With Google</p>
<button class="btn red" id="authorize-button">Log In</button>
<button class="btn red" id="signout-button">Log Out</button>
<br>
<div id="content">
<div class="row">
<div class="col s6">
<form id="channel-form">
<div class="input-field col s6">
<input type="text" id="channel-input" placeholder="Enter Channel Name">
<input type="submit" class="btn grey" value="Get Channel Data">
</div>
</form>
</div>
<div id="channel-data" class="col s6"></div>
<div class="row" id="video-container"></div>
</div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script src="main.js"></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>

142 changes: 142 additions & 0 deletions Week3/homework/y-tube-api/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const clientId = "570509200895-4da0136v9n9cc5t369e1ij2bkerddlrt.apps.googleusercontent.com";
const discoveryDocs = ["https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest"];
const scopes = "https://www.googleapis.com/auth/youtube.readonly";

const authorizeButton = document.getElementById("authorize-button");
const signoutButton = document.getElementById("signout-button");
const content = document.getElementById("content");
const channelForm = document.getElementById("channel-form");
const channelInput = document.getElementById("channel-input");
const videoContainer = document.getElementById("video-container");

const defaultChannel = "techguyweb";

//Form submit and change channel
channelForm.addEventListener("submit", e => {
e.preventDefault();
const channel = channelInput.value;
getChannel(channel);
});


//Load auth2 library
function handleClientLoad(){
gapi.load('client:auth2', initClient);
}

//Init Api client library and set up sign in listeners
function initClient(){
gapi.client.init({
clientId: clientId,
discoveryDocs: discoveryDocs,
scope: scopes
}).then(() => {
//listen for sign in changes
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
//handle initial sign in state
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
})
}

//update UI sign state changes
function updateSigninStatus(isSignedIn){
if (isSignedIn){
authorizeButton.style.display = "none";
signoutButton.style.display = "block";
content.style.display = "block";
videoContainer.style.display = "block";
getChannel(defaultChannel);
}else{
authorizeButton.style.display = "block";
signoutButton.style.display = "none";
content.style.display = "none";
videoContainer.style.display = "none";
}
}

//Handle log in
function handleAuthClick(){
gapi.auth2.getAuthInstance().signIn();
}

//Handle log out
function handleSignoutClick(){
gapi.auth2.getAuthInstance().signOut();
}


//Display channel Data
function showChannelData(data){
const channelData = document.getElementById("channel-data");
channelData.innerHTML = data;
}



//get channel from API
function getChannel(channel){
gapi.client.youtube.channels
.list({
part:'snippet,contentDetails,statistics',
forUsername: channel
})
.then(res => {
console.log(res)
const channel = res.result.items[0];

const output = `
<ul class='collection'>
<li class='collection-item'>Title: ${channel.snippet.title}</li>
<li class='collection-item'>ID: ${channel.id}</li>
<li class='collection-item'>Subscribers: ${numberWithDots(channel.statistics.subscriberCount)}</li>
<li class='collection-item'>Views: ${numberWithDots(channel.statistics.viewCount)}</li>
<li class='collection-item'>Videos: ${numberWithDots(channel.statistics.videoCount)}</li>
</ul>
<p>${channel.snippet.discription}</p>
<hr>
<a class='btn grey darken-2' href='https://youtube.com/${channel.snippet.customUrl}'>Visit Channel</a>
`;

showChannelData(output);
const playListId = channel.contentDetails.relatedPlaylists.uploads;
requestVideoPlaylist(playListId);
})
.catch(err => alert('No Channel By That Name...'))
}


//Show the videos to UI
function requestVideoPlaylist(playListId){
const requestOptions = {
playlistId: playListId,
part: 'snippet',
maxResults:10
}
const request = gapi.client.youtube.playlistItems.list(requestOptions);
request.execute(res => {
const playlistItems = res.result.items;
if (playlistItems){
let output = `<br><h4 class="center-align">Latest Videos</h4>`
//loop through videos and append the output
playlistItems.forEach(item => {
const videoId = item.snippet.resourceId.videoId;
output += `<div class="col s3">
<iframe width="100%" height="auto" src="https://youtube.com/embeded/${videoId}"
frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>`
})
//output the videos
videoContainer.innerHTML = output;
}else{
videoContainer.innerHTML = 'No Uploaded Videos...';
}

})
}

//add dots to numbers
function numberWithDots(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}