Last active
December 25, 2015 07:19
-
-
Save Yengas/6938635 to your computer and use it in GitHub Desktop.
Upgraded Puush.me Managing with Filtering and Multiple Page Deleting.Just copy this code to the Puush.me Managing Site after you login, initialize it to a variable and do whatever you want.This javascript class can:- Return Total Puush History Pages as INT- Get Puush history from pages (all of them or with filtering)- Delete Puushes only by thei…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function PuushMeManager(){ | |
var sizeChart = { | |
"GB": 1024 * 1024 * 1024, | |
"MB": 1024 * 1024, | |
"KB": 1024, | |
"B": 1 | |
}; | |
this.getTotalPages = function(){ | |
try{ var pagination = document.getElementById("pagination"), last = pagination.lastChild; | |
if(!last && document.location.href.indexOf("http://puush.me/account") != -1){ return 1; } | |
if(last.innerText == "»"){ pagination.removeChild(last); last = pagination.lastChild; } | |
return parseInt(last.innerText) || 0; }catch(error){ console.log("There were errors while retrieving the Total Page number."); return 0; } | |
}; | |
this.getPuushes = function(page, condition){ | |
try{ var page, puushList = []; | |
if(page > this.getTotalPages()){ Console.log("There is no urf level."); return[]; } | |
$.ajax({ url: "http://puush.me/account?page="+page, success: function(result) { page = document.createElement('div'); page.setAttribute('style', 'display: none;'); page.innerHTML = result.replace(/<img\b[^>]*>/ig, ''); document.body.appendChild(page); }, async: false }); | |
var trs = Array.prototype.slice.call(page.getElementsByClassName("r0")).concat(Array.prototype.slice.call(page.getElementsByClassName("r1"))); | |
for(var i in trs){ | |
var tr = trs[i], tds = tr.getElementsByTagName("td"), puush = { id: tr.id.replace("tr-",""), name: tr.getElementsByTagName("a")[0].innerText, size: this.getBytes(tds[3].innerText), hits: parseInt(tds[4].innerText.replace("hits","")) }; | |
if(puushList.indexOf(puush.id) == -1 && (typeof condition == "undefined" || (typeof condition == "function" && condition(puush)))){ puushList.push(puush); } | |
} | |
document.body.removeChild(page); | |
return puushList; }catch(err){ console.log("There were errors while getting the Page: "+page); return []; } | |
}; | |
this.deletePuushes = function(puushList){ | |
try{ | |
var idList = []; | |
if(typeof puushList == "string"){ idList.push(puushList); } | |
if(typeof puushList == "object"){ | |
if(puushList.length < 1){ return false; } | |
if(typeof puushList[0] == "string"){ | |
idList = puushList; | |
}else{ | |
for(var i in puushList){ idList.push(puushList[i].id); } | |
} | |
} | |
$.post("/ajax/delete_upload",{'i[]': idList}); if(idList.length > 0){ return true; }else{ return false; } }catch(err){ console.log("There were errors while trying to delete puushes."); return false; } | |
}; | |
this.deleteAllWithCondition = function(condition){ | |
var totalPages = this.getTotalPages(); | |
if(typeof condition != "function"){ condition = function(puush){ return true; }; } | |
for(var page = 1; page <= totalPages; page++){ | |
var puushList = this.getPuushes(page, condition); | |
this.deletePuushes(puushList); | |
if(puushList.length > 0){ console.log("Found and removed total of "+puushList.length+" puushes after filtering the Page: "+page); } | |
} | |
}; | |
this.getBytes = function(sizeString){ | |
for(var key in sizeChart){ | |
if(this.endsWith(sizeString, key)){ | |
sizeString = sizeString.replace(key,""); | |
return parseInt(sizeString) * sizeChart[key]; | |
} | |
} | |
return 0; | |
}; | |
this.endsWith = function(str, suffix){ return str.indexOf(suffix, str.length - suffix.length) !== -1; }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Initializing: | |
var manager = new PuushMeManager(); | |
Methods: | |
manager.getTotalPages() -> Returns total puush history pages. | |
manager.getPuushes(page, condition) -> Returns puush history as Puush Object Array according to given parameters. Condition is optional. | |
manager.deletePuushes(id) -> Deletes puushes according to the given parameter. You can pass a String, String Array or Object Array. Can be used with getPuushes method. | |
manager.deleteAllWithCondition(condition) -> Deletes all the puush history pages with the given condition. If condition is not given, it will delete all the pushes in your history. | |
Puush Object and Conditioning | |
Puush Object is a javascript object which is created by getPuushes method. It contains name, size, hits and the id of a puush. | |
Example: {id: "3RanB", name: "ss (2013-08-01 at 07.41.46).png", size: 12288, hits: 3} | |
Size is a int which reperesents byte equivalent of the puush's size. | |
Condition(Callback Function) which you can give to getPuushes or deleteAllWithCondition method is used by the method themselves to filter puush history results. They pass a Puush Object to your call back so you can filter the puushes in every way you can. | |
Example Callback Function: function(puush){ return puush.size > 2 * 1024 * 1024 } // Only the pushes which have size more than 2 mbs. | |
Examples: | |
Example 1(Delete all pushes in your history): | |
manager.deleteAllWithCondition(); | |
Example 2(Get first page of puush history): | |
manager.getPuushes(1); | |
Example 3(Removing first 5 pages of history): | |
var pagesToDelete = 5, totalPages = manager.getTotalPages(); | |
if(pagesToDelete > totalPages){ pagesToDelete = totalPages; } | |
for(var page = 1; page <= pagesToDelete; page++){ manager.deletePuushes(manager.getPuushes(page)); } | |
Example 4(Removing all the puushes which have more than 10 views): | |
manager.deleteAllWithCondition(function(puush){ return puush.hits > 10; }); | |
Example 5(Getting all the puushes in the first page which have more than 2 mb sizes): | |
manager.getPuushes(1, function(p){ return p.size > 2 * 1024 * 1024; }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment