Skip to content
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
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
]
},
"dependencies": {
"@google-cloud/local-auth": "^0.1.0",
"@react-native-community/async-storage": "^1.9.0",
"@react-native-community/cameraroll": "^1.6.0",
"@react-native-community/masked-view": "0.1.5",
Expand All @@ -50,8 +51,11 @@
"core-util-is": "^1.0.2",
"dotenv": "^8.2.0",
"express-session": "^1.17.0",
"google-auth-library": "^6.0.0",
"googleapis": "^50.0.0",
"mem": "^6.0.1",
"nodemailer": "^6.4.2",
"opn": "^6.0.0",
"react": "~16.9.0",
"react-dom": "16.9.0",
"react-native": "~0.61.4",
Expand Down
17 changes: 16 additions & 1 deletion server/api/routes/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express')
const router = express.Router()
const { updateAnsweredQuestions } = require('../../data_access_layer/user')
const { updateAnsweredQuestions, videoAuthorize } = require('../../data_access_layer/user')
const { getQuestion, getAllQuestions } = require('../../data_access_layer/question')

router.get('/', async (req, res) => {
Expand Down Expand Up @@ -189,5 +189,20 @@ router.delete('/questions', async (req, res) => {
}
})

router.post('/upload', async (req, res) => {
const name = req.body['Name']
const email = req.body['Email']
const URI = req.body['URI']
try {
return res.status(200).send(videoAuthorize(name, email, URI))

}catch(err) {
console.log(err)
return res.status(err.statusCode).send(err)
}


})

module.exports = router

140 changes: 135 additions & 5 deletions server/data_access_layer/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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'
Copy link
Member

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

}
})

// 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'
Expand Down Expand Up @@ -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,
Expand All @@ -162,5 +287,10 @@ module.exports = {
sendPasswordResetEmail,
verifyPasswordCode,
updateUserPassword,
updateEmailandPassword
updateEmailandPassword,
uploadVideo,
authorize,
getNewToken,
storeToken,
videoAuthorize
}
Loading