-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vk-youtube-connect #132
Open
vikram-kher
wants to merge
6
commits into
master
Choose a base branch
from
VK-Youtube-Connect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Vk-youtube-connect #132
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a411036
NodeMailer Fix
vikram-kher 93c7941
Merge branch 'master' of https://github.com/ctcusc/gladeo
vikram-kher e9e7c67
Merge branch 'master' of https://github.com/ctcusc/gladeo
vikram-kher c1c196d
Youtube Integration
vikram-kher 73c89d2
.env updates
vikram-kher 1c381c3
Merge branch 'master' into VK-Youtube-Connect
vikram-kher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,16 @@ const { base } = require('./index') | |
const { getCompany } = require('./company') | ||
const nodemailer = require('nodemailer') | ||
const { extractContentFromRecords, getAllFromTable } = require('./helpers') | ||
const fs = require('fs') | ||
const readline = require('readline') | ||
const {google} = require('googleapis') | ||
const youtube = google.youtube('v3') | ||
const OAuth2 = google.auth.OAuth2 | ||
const SCOPES = [ 'https://www.googleapis.com/auth/youtube.upload', | ||
'https://www.googleapis.com/auth/youtube'] | ||
const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || | ||
process.env.USERPROFILE) + '/.credentials/' | ||
const TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json' | ||
|
||
async function getUser(userId) { | ||
const userRecords = base('Users').select({ | ||
|
@@ -93,16 +103,16 @@ async function sendPasswordResetEmail(email, fullName) { | |
|
||
// Sets up sender details | ||
const transporter = nodemailer.createTransport({ | ||
service: 'INSERT SENDER EMAIL SERVICE HERE (EX: Outlook365, Gmail)', | ||
service: 'Gmail', | ||
auth: { | ||
user: 'INSERT SENDER EMAIL HERE', | ||
pass: 'INSERT SENDER EMAIL PASSWORD HERE' | ||
user: '[email protected]', | ||
pass: 'Abq12Yx34z!8' | ||
} | ||
}) | ||
|
||
// Message contents | ||
const info = { | ||
from: '"Gladeo" <vikramkher@live.com>', | ||
from: '"Gladeo" <gladeo.app@gmail.com>', | ||
to: email, | ||
subject: 'Gladeo Password Reset', | ||
html: '<p>Hello ' + fullName + ',</p><p>There was a request to change your password.<br>Please enter this' | ||
|
@@ -153,6 +163,121 @@ async function updateUserPassword(email, password) { | |
return true | ||
} | ||
|
||
|
||
function storeToken(token) { | ||
try { | ||
fs.mkdirSync(TOKEN_DIR) | ||
} catch (err) { | ||
if (err.code != 'EEXIST') { | ||
throw err | ||
} | ||
} | ||
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => { | ||
if (err) throw err | ||
console.log('Token stored to ' + TOKEN_PATH) | ||
}) | ||
} | ||
|
||
function getNewToken(oauth2Client, callback) { | ||
const authUrl = oauth2Client.generateAuthUrl({ | ||
access_type: 'offline', | ||
scope: SCOPES | ||
}) | ||
console.log('Authorize this app by visiting this url: ', authUrl) | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
rl.question('Enter the code from that page here: ', function(code) { | ||
rl.close() | ||
oauth2Client.getToken(code, function(err, token) { | ||
if (err) { | ||
console.log('Error while trying to retrieve access token', err) | ||
return | ||
} | ||
oauth2Client.credentials = token | ||
storeToken(token) | ||
callback(oauth2Client) | ||
}) | ||
}) | ||
} | ||
|
||
function authorize(credentials, callback, name, email, URI) { | ||
const clientSecret = process.env.CLIENT_SECRET | ||
const clientId = credentials.web.client_id | ||
const redirectUrl = credentials.web.redirect_uris[0] | ||
const oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl) | ||
// Check if we have previously stored a token. | ||
fs.readFile(TOKEN_PATH, function(err, token) { | ||
if (err) { | ||
getNewToken(oauth2Client, callback) | ||
} else { | ||
oauth2Client.credentials = JSON.parse(token) | ||
return(callback(oauth2Client, name, email, URI)) | ||
} | ||
}) | ||
} | ||
|
||
async function uploadVideo(auth, name, email, URI) { | ||
google.options({auth}) | ||
const fileSize = fs.statSync(URI).size | ||
try { | ||
const res = await youtube.videos.insert( | ||
{ | ||
part: 'id,snippet,status', | ||
notifySubscribers: false, | ||
requestBody: { | ||
snippet: { | ||
title: '[Draft - ready for review] video by ' + name, | ||
description: 'Draft video created by ' + name + '\nEmail: ' + email, | ||
}, | ||
status: { | ||
privacyStatus: 'private', | ||
}, | ||
}, | ||
media: { | ||
body: fs.createReadStream(URI), | ||
}, | ||
}, | ||
|
||
{ | ||
// Use the `onUploadProgress` event from Axios to track the | ||
// number of bytes uploaded to this point. | ||
onUploadProgress: evt => { | ||
const progress = (evt.bytesRead / fileSize) * 100 | ||
readline.clearLine(process.stdout, 0) | ||
readline.cursorTo(process.stdout, 0, null) | ||
process.stdout.write(`${Math.round(progress)}% complete`) | ||
}, | ||
} | ||
) | ||
|
||
console.log(res.data) | ||
return res.data | ||
|
||
}catch(err) { | ||
console.log(err) | ||
} | ||
|
||
} | ||
|
||
|
||
async function videoAuthorize(name, email, URI) { | ||
name = name | ||
URI = URI | ||
email = email | ||
fs.readFile('client_secret.json', function processClientSecrets(err, content) { | ||
if (err) { | ||
console.log('Error loading client secret file: ' + err) | ||
return | ||
} | ||
// Authorize a client with the loaded credentials, then call the YouTube API. | ||
authorize(JSON.parse(content), uploadVideo, name, email, URI) | ||
}) | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
getUser, | ||
getUserByEmail, | ||
|
@@ -162,5 +287,10 @@ module.exports = { | |
sendPasswordResetEmail, | ||
verifyPasswordCode, | ||
updateUserPassword, | ||
updateEmailandPassword | ||
updateEmailandPassword, | ||
uploadVideo, | ||
authorize, | ||
getNewToken, | ||
storeToken, | ||
videoAuthorize | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this a .env variable