Skip to main content
Loading...

More JavaScript Posts

function filePath(file){
  let folders = file.getParents();
  const parents = [];

  function makePathString(folderArray){
    let path = "";
    folderArray.forEach((folder)=>{
      path += "/" + folder.getName();
    });
    return path;
  }
  
  while (folders.hasNext()) {
    const folder = folders.next(); //This should hopefully remove that folder from the iterator
    parents.unshift(folder);
  }

  return makePathString(parents);
}

function myFunction() {
  const myEmail = Session.getEffectiveUser().getEmail();
  Logger.log("My email is " + myEmail);
  // Log the name of every file in the user's Drive.
  var fileIterator = DriveApp.getFiles();
  const files = []; //List of [File, size] entries
  const filesMaxSize = 10;
  while (fileIterator.hasNext()) {
    var file = fileIterator.next();

    const owner = file.getOwner();

    //Only files I own
    if((!(owner)) || (!(owner.getEmail)) || owner.getEmail() != myEmail){
      continue;
    }

    const entry = [file, file.getSize()];

    function slideUp(arr, index){
      //Let's keep sliding it up so we don't have to sort it at the end.
      for(let i = index; i > 0; --i){ //Stops at 1
        const nextFile = arr[i-1];

        if(nextFile[1] > entry[1]){
          break;
        }else{
          //Swap with the next file to slide the file up
          const temp = arr[i];
          arr[i] = arr[i - 1];
          arr[i - 1] = temp;
        }
      }
    }

    if(files.length < filesMaxSize){
      files.push(entry);

      slideUp(files, files.length - 1);
    }else{
      if(entry[1] > files[files.length - 1][1]){ //If it's bigger than the smallest file in the list.
        files[files.length - 1] = entry;  //Replace the smallest file

        slideUp(files, files.length - 1); //Slide it up
      }
    }
  }

  for(let i = 0; i < filesMaxSize; ++i){
    const file = files[i][0];
    const size = files[i][1];
    Logger.log(size + "\t" + filePath(file) + "/" + file.getName() + "\t" + file.getOwner().getName());
  }
}
//Allegedly never tested on the twitter website
function scrapeScreen(){
    let articles = Array.from(document.getElementsByTagName("article"));
    let results = [];
    for(let tweet of articles){
        const isAnAd = Array.from(tweet.querySelectorAll("span")).map((e)=>e.textContent).includes("Ad");
        if(isAnAd){
            //console.log(tweet, "is an ad. Skipping...");
            continue;
        }
        const userName = tweet.querySelector("[data-testid='User-Name'] > div:nth-child(2) > div > div")?.textContent;
        const tweetContent = tweet.querySelector("[data-testid='tweetText']")?.textContent;
        const timeStamp = tweet.querySelector("time")?.getAttribute("datetime");
        const tweetLink = tweet.querySelector("time")?.parentElement?.getAttribute("href");
        if((!userName) || (!tweetContent)) continue;
        results.push({
            username: userName,
            tweetText: tweetContent,
            timeStamp: timeStamp,
            tweetLink: tweetLink
        });
    }
    return results;
}
let scraped = scrapeScreen();
setInterval(()=>{
    scraped = scraped.concat(
        scrapeScreen().filter((tweet)=>{
            for(let scrapedTweet of scraped){
                if(scrapedTweet.username == tweet.username && scrapedTweet.tweetText == tweet.tweetText) return false;
            }
            return true;
        })
    );
}, 500); //Scrape everything on the screen twice a second

window.scrollIntervalId = setInterval(function(){
    window.scrollBy(0, 1000);
}, 500); //Scroll for me


//http://bgrins.github.io/devtools-snippets/#console-save
(function(console){

console.save = function(data, filename){

 if(!data) {
 console.error('Console.save: No data')
 return;
 }

 if(!filename) filename = 'console.json'

 if(typeof data === "object"){
 data = JSON.stringify(data, undefined, '\t')
 }

 var blob = new Blob([data], {type: 'text/json'}),
 e = document.createEvent('MouseEvents'),
 a = document.createElement('a')

 a.download = filename
 a.href = window.URL.createObjectURL(blob)
 a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
 e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
 a.dispatchEvent(e)
 }
})(console)

setTimeout(()=>{
    clearTimeout(window.scrollIntervalId);
    delete window.scrollIntervalId;

    console.save(scraped, "TwitterScrape" + Date.now() + ".json");
}, 60 * 1000 * 20); //Twenty minutes
var arr = [{
    "success": true,
    "data": [
        {
            "id": "ipi_1KrrbvDOiB2klwsKhuqUWqt1",
            "object": "issuing.transaction",
            "amount": -2743,
            "amount_details": {
                "atm_fee": null
            },
            "authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
            "balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
            "card": "ic_1Krqe5DOiB2klwsK44a35eiE",
            "cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
            "created": 1650753567,
            "currency": "usd",
            "dispute": null,
            "livemode": false,
            "merchant_amount": -2743,
            "merchant_currency": "usd",
            "merchant_data": {
                "category": "advertising_services",
                "category_code": "7311",
                "city": "San Francisco",
                "country": "US",
                "name": "Aeros Marketing, LLC",
                "network_id": "1234567890",
                "postal_code": "94103",
                "state": "CA"
            },
            "metadata": {},
            "type": "capture",
            "wallet": null
        },
                {
            "id": "ipi_1Krrbvc62B2klwsKhuqUWqt1",
            "object": "issuing.transaction",
            "amount": -9999,
            "amount_details": {
                "atm_fee": null
            },
            "authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
            "balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
            "card": "ic_1Krqe5DOiB2klwsK44a35eiE",
            "cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
            "created": 1650753567,
            "currency": "USD",
            "dispute": null,
            "livemode": false,
            "merchant_amount": -9999,
            "merchant_currency": "usd",
            "merchant_data": {
                "category": "fast_food",
                "category_code": "7311",
                "city": "San Francisco",
                "country": "US",
                "name": "Aeros Marketing, LLC",
                "network_id": "1234567890",
                "postal_code": "94103",
                "state": "CA"
            },
            "metadata": {},
            "type": "capture",
            "wallet": null
        }
    ]
}];
    

const reduced = arr.reduce((prev, curr) => {
    prev.push({
        amount: curr.data[0].merchant_amount,
        id: curr.data[0].id,
        currency: curr.data[0].currency,
        category: curr.data[0].merchant_data.category
    });
    console.log(prev)

    return prev;
    
}, []);