Danny_Berova

prepareVideoAndCollectAuthorAndType

Nov 1st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function collectAuthorAndTypeFromVideo() {
  2.     return new Promise((resolve, reject) => {
  3.         try {
  4.             var vidType = '';
  5.             var maxWaitTime = 10000;
  6.             var authorInit = document.evaluate(".//div[contains(@aria-label,'Dialog content')]//div//a[@data-hovercard and text()]", document, null, 9, null).singleNodeValue;
  7.             //var currentTime = 0;
  8.             var waitForAuthor = setInterval(() => {
  9.                 if (maxWaitTime <= 0) {
  10.                     console.log(`Did not found any author or vidType memories not supported. Continue...`);
  11.                     clearInterval(waitForAuthor);
  12.                     resolve({
  13.                         vidType: 'memories'
  14.                     });
  15.                 } else {
  16.                     console.log('IN PROFILE')
  17.                     authorInit = document.evaluate(".//div[contains(@aria-label,'Dialog content')]//div//a[@data-hovercard and text()]", document, null, 9, null).singleNodeValue;
  18.                     if (authorInit && authorInit.getAttribute('data-hovercard')) {
  19.                         vidType = 'profile';
  20.                         var authorIdProf = authorInit.getAttribute('data-hovercard').split('&')[0].split('php?id=');
  21.                         console.log(authorIdProf);
  22.  
  23.                         var liveCheck = document.evaluate(".//ul/li/a//span[text()='Comments']", document, null, 9, null).singleNodeValue;
  24.                         if (liveCheck) {
  25.                             var authorInit = document.evaluate(".//div[contains(@class,'StreamStory')]//div//a[@data-hovercard and text()]", document, null, 9, null).singleNodeValue;
  26.                             authorIdProf = authorInit.getAttribute('data-hovercard').split('&')[0].split('php?id=');
  27.                             vidType = 'live';
  28.                             console.log(`set video type to ${vidType}`)
  29.                         } else if (authorIdProf[0].includes('page')) {
  30.                             vidType = 'page';
  31.                             console.log(`set vidType to ${vidType}`);
  32.                         } else if (authorIdProf[0].includes('user')) {
  33.                             vidType = 'profile';
  34.                             console.log(`set vidType to ${vidType}`);
  35.                         } else {
  36.                             vidType = 'memories';
  37.                             console.log(`set vidType to memories`);
  38.  
  39.                         }
  40.  
  41.                         var authorTitleProf = authorInit.textContent;
  42.                         console.log(authorTitleProf);
  43.                         clearInterval(waitForAuthor);
  44.  
  45.                         var result = {
  46.                             authorId: authorIdProf[1],
  47.                             authorTitle: authorTitleProf,
  48.                             vidType: vidType
  49.                         }
  50.                         resolve(result);
  51.  
  52.                     } else {
  53.                         maxWaitTime -= 500;
  54.                         console.log('adding time profile');
  55.                     }
  56.                 }
  57.             }, 500)
  58.         } catch (e) {
  59.             console.log(`error in collectAuthorFromVideo ::: error => ${e.message}`);
  60.             clearInterval(waitForAuthor);
  61.         }
  62.     })
  63. }
  64.  
  65. async function prepareVideo() {
  66.     return new Promise((resolve, reject) => {
  67.         try {
  68.             var maxTimeCheckVideo = 10000;
  69.             var autoPlayClicked = false;
  70.  
  71.             var checkVideoInt = setInterval(() => {
  72.                 if (maxTimeCheckVideo <= 0) {
  73.                     console.log(`Did not found video. Continue...`);
  74.                     clearInterval(checkVideoInt);
  75.                     resolve('cannot find video');
  76.                 } else {
  77.                     if (document.URL.includes('/watch/?v')) {
  78.                         //vidType = 'profile';
  79.                         var autoPlayCheck = document.evaluate(".//div//input[contains(@aria-label, 'Play video')]", document, null, 9, null).singleNodeValue;
  80.                         var videoCheck = document.evaluate(".//video | .//canvas", document, null, 9, null).singleNodeValue;
  81.                         if (videoCheck) {
  82.                             if (autoPlayCheck && !autoPlayClicked) {
  83.                                 autoPlayCheck.click();
  84.                                 autoPlayClicked = true;
  85.                                 console.log('auto play clicked');
  86.                             }
  87.  
  88.                             if(autoPlayClicked || !autoPlayCheck) {
  89.  
  90.                                 videoCheck.click();
  91.                                 console.log('video clicked - resolve first stage');
  92.                                 clearInterval(checkVideoInt);
  93.                                 resolve();
  94.                             } else {
  95.                                 maxWaitTime -= 500;
  96.                                 console.log('adding time after stopping auto play');
  97.                             }
  98.                         } else {
  99.                             maxWaitTime -= 500;
  100.                             console.log('adding time first stage');
  101.                         }
  102.                     } else if (document.URL.includes('/videos/')) {
  103.                         //vidType = 'profile'
  104.                         clearInterval(checkVideoInt);
  105.                         resolve();
  106.                     } else {
  107.                         clearInterval(checkVideoInt);
  108.                         resolve();
  109.                     }
  110.  
  111.                 }
  112.             }, 500);
  113.  
  114.         } catch (er) {
  115.             console.log(`error in prepareVideo ::: er => ${e.message}`);
  116.             clearInterval(checkVideoInt);
  117.  
  118.         }
  119.     })
  120. }
  121.  
  122. var obj = await prepareVideo().then(collectAuthorAndTypeFromVideo).then(res => res);
  123. console.log(obj);
Add Comment
Please, Sign In to add comment