InstaSynchP TrimWall Command

Trim a users wall down to the specified time

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        InstaSynchP TrimWall Command
// @namespace   InstaSynchP
// @description Trim a users wall down to the specified time

// @version     1
// @author      Zod-
// @source      https://github.com/Zod-/InstaSynchP-TrimWall-Command
// @license     MIT

// @include     *://instasync.com/r/*
// @include     *://*.instasync.com/r/*
// @grant       none
// @run-at      document-start

// @require     https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js?version=37716
// ==/UserScript==

function TrimWall(version) {
  "use strict";
  this.version = version;
  this.name = 'InstaSynchP TrimWall Command';
  this.settings = [{
    'label': 'Default trim length (minutes)',
    'id': 'trimwall-length',
    'type': 'int',
    'min': 0,
    'default': 60,
    'size': 8,
    'section': ['Playlist', 'Trimwall']
  }];
  this.commands = {
    "'trimwall": {
      'hasArguments': true,
      'reference': this,
      'description': 'Trim the wall of all or the specified users to a time limit.',
      'callback': this.execute
    }
  };
}

TrimWall.prototype.execute = function (opts) {
  "use strict";
  var th = this,
    map = {},
    maxTimeLimit = opts.numbers[0] || gmc.get('trimwall-length');
    maxTimeLimit *= 60;

    //add all users when no user specified
    if(opts.usernames.length === 0){
      window.room.userlist.users.forEach(function(user){
        opts.usernames.push(user.username);
      });
    }

    //initialize the map
    opts.usernames.forEach(function (user){
      map[user.toLowerCase()] = {
        videos: [],
        time: 0
      };
    });

    //collect videos and durations
    window.room.playlist.videos.forEach(function(video){
      var key = video.addedby.toLowerCase();
      if(!map.hasOwnProperty(key)){
        return;
      }
      map[key].videos.push(video);
      map[key].time += video.duration;
    });

    function cmpVidDur(v1, v2) {
      return v2.duration - v1.duration;
    }

    for(var username in map){
      if(!map.hasOwnProperty(username)){
        continue;
      }
      //sort videos by length
      map[username].videos.sort(cmpVidDur);
      //remove till the video limit is hit
      for(var i = 0; i < map[username].videos.length && map[username].time > maxTimeLimit; i++){
        map[username].time -= map[username].videos[i].duration;
        sendcmd('remove',{
          info: map[username].videos[i].info
        });
      }
    }
};

window.plugins = window.plugins || {};
window.plugins.trimwall = new TrimWall('1');