Advertisement
Guest User

Untitled

a guest
Sep 1st, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. const { google } = require('googleapis');
  2. const fs = require('fs');
  3. const readline = require('readline');
  4.  
  5. const SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl'];
  6. const TOKEN_PATH = 'token.json';
  7. const CREDENTIALS_PATH = 'client_secrets.json';
  8.  
  9. function authenticate() {
  10. const credentials = JSON.parse(fs.readFileSync(CREDENTIALS_PATH));
  11. const { client_secret, client_id, redirect_uris } = credentials.installed;
  12. const oAuth2Client = new google.auth.OAuth2(
  13. client_id,
  14. client_secret,
  15. redirect_uris[0]
  16. );
  17.  
  18. // Check if we have previously stored a token.
  19. if (fs.existsSync(TOKEN_PATH)) {
  20. const token = fs.readFileSync(TOKEN_PATH);
  21. oAuth2Client.setCredentials(JSON.parse(token));
  22. return oAuth2Client;
  23. } else {
  24. return getNewToken(oAuth2Client);
  25. }
  26. }
  27.  
  28. function getNewToken(oAuth2Client) {
  29. const authUrl = oAuth2Client.generateAuthUrl({
  30. access_type: 'offline',
  31. scope: SCOPES,
  32. });
  33.  
  34. const rl = readline.createInterface({
  35. input: process.stdin,
  36. output: process.stdout,
  37. });
  38.  
  39. console.log('Authorize this app by visiting this url:', authUrl);
  40.  
  41. return new Promise((resolve, reject) => {
  42. rl.question('Enter the code from that page here: ', (code) => {
  43. rl.close();
  44. oAuth2Client.getToken(code, (err, token) => {
  45. if (err) {
  46. reject(err);
  47. } else {
  48. oAuth2Client.setCredentials(token);
  49. fs.writeFileSync(TOKEN_PATH, JSON.stringify(token));
  50. resolve(oAuth2Client);
  51. }
  52. });
  53. });
  54. });
  55. }
  56.  
  57. async function getVideoId() {
  58. const videoUrl = await new Promise((resolve, reject) => {
  59. const rl = readline.createInterface({
  60. input: process.stdin,
  61. output: process.stdout,
  62. });
  63. rl.question('Enter YouTube video URL: ', (url) => {
  64. rl.close();
  65. resolve(url);
  66. });
  67. });
  68. const url = new URL(videoUrl);
  69. return url.searchParams.get('v');
  70. }
  71.  
  72. async function getVideoViews(videoId) {
  73. const auth = await authenticate();
  74. const youtube = google.youtube({ version: 'v3', auth });
  75. const response = await youtube.videos.list({
  76. part: 'statistics',
  77. id: videoId,
  78. });
  79. const views = response.data.items[0].statistics.viewCount;
  80. return views;
  81. }
  82.  
  83. async function updateVideoTitle(videoId, views) {
  84. const auth = await authenticate();
  85. const youtube = google.youtube({ version: 'v3', auth });
  86. const response = await youtube.videos.update({
  87. part: 'snippet',
  88. requestBody: {
  89. id: videoId,
  90. snippet: { title: views + ' просмотров' },
  91. },
  92. });
  93. console.log('Video title successfully updated!');
  94. }
  95.  
  96. async function main() {
  97. const videoId = await getVideoId();
  98. const views = await getVideoViews(videoId);
  99. await updateVideoTitle(videoId, views);
  100. }
  101.  
  102. main().catch(console.error);
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement