summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbill-auger <mr.j.spam.me@gmail.com>2018-01-22 01:49:46 -0500
committerbill-auger <mr.j.spam.me@gmail.com>2020-03-14 19:11:02 -0400
commit98eaf95da27d993176215acc84727153131cfe2d (patch)
tree31337a6bcc65483b10bcf35935d39381616f3697
parent80eb62d02f60fc5c7c5c1713f27f1cdd13d8181c (diff)
[post_fsd_wiki.phantomjs]: refactor logging and validations
-rw-r--r--post_fsd_wiki.phantomjs166
1 files changed, 95 insertions, 71 deletions
diff --git a/post_fsd_wiki.phantomjs b/post_fsd_wiki.phantomjs
index f6ee73f..a6c3d4f 100644
--- a/post_fsd_wiki.phantomjs
+++ b/post_fsd_wiki.phantomjs
@@ -56,18 +56,18 @@ function Prepare()
blacklist_data.forEach(function(package_data)
{
- var page_title = package_data[PACKAGE_NAME_KEY].trim() ;
- var page_text = package_data[DESCRIPTION_KEY ].trim() ;
+ var package_name = package_data[PACKAGE_NAME_KEY].trim() ;
+ var description = package_data[DESCRIPTION_KEY ].trim() ;
- if (!page_title || !IsA(page_title , String) ||
- !page_text || !IsA(page_text , String) )
- { ForceQuit("invalid package data: " + JSON.stringify(package_data)) ; return ; }
+ if (!IsA(package_name , String) ||
+ !IsA(description , String) )
+ { LOG_ERR("invalid package data - ignoring:\n" + JSON.stringify(package_data)) ; return ; }
LOG("found package: " + page_title) ;
var wiki_page = {} ;
- wiki_page[PACKAGE_NAME_KEY] = page_title.toLowerCase().replace(/ /g , '_') ;
- wiki_page[DESCRIPTION_KEY ] = page_text ;
+ wiki_page[PACKAGE_NAME_KEY] = package_name.toLowerCase().replace(/ /g , '_') ;
+ wiki_page[DESCRIPTION_KEY ] = description ;
WikiPages.push(wiki_page) ;
}) ;
@@ -96,17 +96,15 @@ function LoadLoginPage()
function SubmitLogin()
{
EvalQuitOnErr(Page.evaluate(function(login_input_id , pass_input_id , submit_input_id ,
- wiki_login , wiki_pass )
+ wiki_login , wiki_pass , IsA )
{
var login_input = document.getElementById(login_input_id ) ;
var pass_input = document.getElementById(pass_input_id ) ;
var submit_button = document.getElementById(submit_input_id) ;
var login_form = submit_button.form ;
- if (!login_input || login_input.nodeName != 'INPUT' ||
- !pass_input || pass_input.nodeName != 'INPUT' ||
- !submit_button || submit_button.nodeName != 'BUTTON' ||
- !login_form || login_form.nodeName != 'FORM' )
+ if (!IsA(login_input , 'INPUT' ) || !IsA(pass_input , 'INPUT') ||
+ !IsA(submit_button , 'BUTTON') || !IsA(login_form , 'FORM' ) )
return "ERROR: invalid login page" ;
login_input.value = wiki_login ;
@@ -115,31 +113,31 @@ function SubmitLogin()
login_form.submit() ;
return '' ;
- } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , LOGIN_SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ;
+ } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , LOGIN_SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS , IsA)) ;
IsReady = true ;
}
function VerifyLogin()
{
- EvalQuitOnErr(Page.evaluate(function(username_li_id , wiki_login)
+ EvalQuitOnErr(Page.evaluate(function(username_li_id , wiki_login , IsA , LOG)
{
var username_li = document.getElementById(username_li_id) ;
var username = !!username_li && username_li.textContent.toLowerCase() ;
- if (!username_li || username_li.nodeName != 'LI' ||
- username != wiki_login.toLowerCase() )
+ if (!IsA(username_li , 'LI') ||
+ username != wiki_login.toLowerCase())
return "ERROR: login failed" ;
- console.info("signed in as: " + username) ; return '' ;
- } , USERNAME_LI_ID , WIKI_LOGIN)) ;
+ LOG("signed in as: " + username) ; return '' ;
+ } , USERNAME_LI_ID , WIKI_LOGIN , IsA , LOG)) ;
IsReady = true ;
}
function LoadWikiData()
{
- LOG(WikiPages.length + " remaining") ;
+ LOG(WikiPages.length + " WikiPages remaining") ;
if (!!(WikiPage = WikiPages.shift())) IsReady = true ; else Done() ;
}
@@ -159,19 +157,17 @@ function SubmitEdit()
if (Page.url != EditPageUrl(page_title)) { ForceQuit("edit page load failed") ; return ; }
var post_wiki_text = EvalQuitOnErr(Page.evaluate(function(wiki_text_input_id , wiki_submit_button_id ,
- wiki_text_regex , new_wiki_text )
+ wiki_text_regex , new_wiki_text ,
+ IsA , DBG )
{
var wiki_text_input = document.getElementById(wiki_text_input_id ) ;
var wiki_submit_button = document.getElementById(wiki_submit_button_id) ;
var existing_wiki_text = !!wiki_text_input && wiki_text_input.value ;
-// console.log("submitEdit() wiki_text_input=" + wiki_text_input ) ;
-// console.log("submitEdit() wiki_text_input .nodeName" + wiki_text_input .nodeName) ;
-// console.log("submitEdit() wiki_submit_button" + wiki_submit_button) ;
-// console.log("submitEdit() wiki_submit_button.nodeName" + wiki_submit_button.nodeName) ;
+DBG('SubmitEditEvalIn')(wiki_text_input , wiki_submit_button) ;
- if (!wiki_text_input || wiki_text_input .nodeName != 'TEXTAREA' ||
- !wiki_submit_button || wiki_submit_button.nodeName != 'INPUT' )
+ if (!IsA(wiki_text_input , 'TEXTAREA') ||
+ !IsA(wiki_submit_button , 'INPUT' ) )
return "ERROR: invalid edit page" ;
var modified_wiki_text = existing_wiki_text.replace(wiki_text_regex , new_wiki_text) ;
@@ -179,21 +175,16 @@ function SubmitEdit()
var post_wiki_text = (!wiki_text_regex.test(existing_wiki_text)) ? concat_wiki_text :
(modified_wiki_text != existing_wiki_text ) ? modified_wiki_text : '' ;
-// console.log("submitEdit() new_wiki_text=" + new_wiki_text) ;
-// console.log("submitEdit() existing_wiki_text=" + existing_wiki_text) ;
-// console.log("submitEdit() modified_wiki_text=" + modified_wiki_text) ;
-// console.log("submitEdit() (modified_wiki_text == existing_wiki_text)=" + (modified_wiki_text == existing_wiki_text)) ;
-// console.log("submitEdit() wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text)) ;
-// console.log("submitEdit() post_wiki_text=" + post_wiki_text) ;
+DBG('SubmitEditEvalOut')(new_wiki_text , existing_wiki_text , modified_wiki_text , post_wiki_text , wiki_text_regex) ;
if (!!post_wiki_text) { wiki_text_input.value = post_wiki_text ; wiki_submit_button .click() ; }
return post_wiki_text ;
+ } , EDIT_TEXT_INPUT_ID , EDIT_SUBMIT_INPUT_ID ,
+ WIKI_TEXT_REGEX , WIKI_TEXT_BEGIN + wiki_text + WIKI_TEXT_END , IsA , DBG)) ;
- } , EDIT_TEXT_INPUT_ID , EDIT_SUBMIT_INPUT_ID ,
- WIKI_TEXT_REGEX , WIKI_TEXT_BEGIN + wiki_text + WIKI_TEXT_END)) ;
-// DBG("returned post_wiki_text=" + post_wiki_text) ;
+DBG('SubmitEditOut')(post_wiki_text) ;
if (post_wiki_text == '') { LOG("wiki text has not changed - skipping") ; NextPage() ; }
}
@@ -206,12 +197,13 @@ function VerifyEdit()
page_title = page_title.replace(/_/g , ' ') ;
wiki_text = PARABOLA_ENTRY_HEADER.replace(/=/g , '').trim() + '\n' + wiki_text ;
-// DBG("Page.url=" + Page.url.toLowerCase()) ; DUMPFILE(Page.content) ;
+if (DEBUG_VB) DUMPFILE() ;
if (Page.url.toLowerCase() != expected_url) { ForceQuit("edit post failed") ; return ; }
var err = EvalQuitOnErr(Page.evaluate(function(wiki_title_h1_id , wiki_text_div_id ,
- page_title , new_wiki_text )
+ page_title , new_wiki_text ,
+ IsA , DBG )
{
var wiki_title_h1 = document.getElementById(wiki_title_h1_id) ;
var wiki_text_div = document.getElementById(wiki_text_div_id) ;
@@ -219,21 +211,14 @@ function VerifyEdit()
var existing_wiki_text = !!wiki_text_div && wiki_text_div.textContent.trim() ;
var wiki_text_regex = RegExp(new_wiki_text) ;
-console.log("wiki_title_h1=" + wiki_title_h1 + " wiki_title_h1.nodeName=" + wiki_title_h1.nodeName) ;
-console.log("wiki_text_div=" + wiki_text_div + " wiki_text_div.nodeName=" + wiki_text_div.nodeName) ;
-console.log("existing_wiki_title=" + existing_wiki_title) ;
-console.log("page_title=" + page_title) ;
-console.log("existing_wiki_text=" + existing_wiki_text) ;
-console.log("new_wiki_text=" + new_wiki_text) ;
-console.log("(existing_wiki_title == page_title )=" + (existing_wiki_title == page_title)) ;
-console.log("wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text)) ;
-
- if (!wiki_title_h1 || wiki_title_h1.nodeName != 'H1' || existing_wiki_title != page_title ||
- !wiki_text_div || wiki_text_div.nodeName != 'DIV' || !wiki_text_regex.test(existing_wiki_text))
+DBG('VerifyEdit')(wiki_title_h1 , wiki_text_div , existing_wiki_title , page_title , existing_wiki_text , new_wiki_text , wiki_text_regex) ;
+
+ if (!IsA(wiki_title_h1 , 'H1' ) || existing_wiki_title != page_title ||
+ !IsA(wiki_text_div , 'DIV') || !wiki_text_regex .test(existing_wiki_text ))
return "ERROR: edit post failed" ;
return '' ;
- } , WIKI_TITLE_H1_ID , WIKI_TEXT_DIV_ID , page_title , wiki_text)) ;
+ } , WIKI_TITLE_H1_ID , WIKI_TEXT_DIV_ID , page_title , wiki_text , IsA , DBG)) ;
if (!err) LOG("successfully updated: " + page_title) ; NextPage() ;
}
@@ -251,10 +236,10 @@ function DefineStep(step_function , should_wait_for_pageload)
function MainLoop()
{
-if (DEBUG_VB) DBG("ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ;
+DBG('MainLoop')() ;
- if (!!ShouldQuit) { ERR(ShouldQuit) ; Done() ; return ; }
- else if (!IsReady ) { PumpMainLoop() ; return ; }
+ if (!!ShouldQuit) { LOG_ERR(ShouldQuit) ; Done() ; return ; }
+ else if (!IsReady ) { PumpMainLoop() ; return ; }
clearTimeout(StepTimeout) ;
@@ -279,7 +264,7 @@ function Done() { LOG("done") ; phantom.exit() ; LOG('') ; }
function GetUrl(get_url)
{
-DBG("get_url=" + get_url) ;
+DBG('GetUrl')(get_url) ;
Page.open(get_url , function(status) { if (status != 'success') ForceQuit("status: " + status) ; }) ;
}
@@ -301,13 +286,21 @@ function NextPage() { StepN = NEXT_PAGE_STEP ; IsReady = true ; }
function IsA(an_object , expected_type)
{
+ if (!an_object || an_object == [] || an_object == '') return false ;
+
switch(expected_type)
{
- case Array: expected_type = '[object Array]' ; break ;
- case String: expected_type = '[object String]' ; break ;
- }
-
- return (Object.prototype.toString.call(an_object) === expected_type) ;
+ case Array : return Object.prototype.toString.call(an_object) == '[object Array]' ; break ;
+ case String : return Object.prototype.toString.call(an_object) == '[object String]' ; break ;
+ case 'BUTTON' :
+ case 'DIV' :
+ case 'FORM' :
+ case 'H1' :
+ case 'INPUT' :
+ case 'LI' :
+ case 'TEXTAREA' : return an_object.nodeName == expected_type ; break ;
+ default : return false ; break ;
+ } ;
}
function EditPageUrl(page_title) { return EDIT_URL.replace(TITLE_URL_REGEX , page_title) ; }
@@ -315,28 +308,59 @@ function EditPageUrl(page_title) { return EDIT_URL.replace(TITLE_URL_REGEX , pag
/* event hendlers */
-Page.onLoadStarted = function() { IsReady = false ; ARGS.apply("Page.onLoadStarted" , arguments) ; } ;
+Page.onLoadStarted = function() { IsReady = false ; LOG_ARGS.apply("Page.onLoadStarted" , arguments) ; } ;
-Page.onLoadFinished = function() { IsReady = true ; ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ;
+Page.onLoadFinished = function() { IsReady = true ; LOG_ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ;
-Page.onUrlChanged = function() { ARGS.apply("Page.onUrlChanged" , arguments) ; } ;
+Page.onUrlChanged = function() { LOG_ARGS.apply("Page.onUrlChanged" , arguments) ; } ;
/* logging */
-Page.onConsoleMessage = function(msg) { LOG(msg) ; } ;
+Page.onConsoleMessage = function(msg) { DBG('')(msg) ; } ;
var DEBUG = false ; var DEBUG_EVS = DEBUG && false ; var DEBUG_VB = DEBUG && false ;
-function LOG (log , color) { console.log((color || '\033[00;32m') + log + '\033[00m' ) ; }
-function LOG_STEP(name) { LOG("Step: " + name , '\033[01;32m') ; }
-function ERR (err) { LOG("ERROR: " + err , '\033[00;31m') ; }
-function DBG (dbg) { if (!DEBUG) return ;
- dbg = ((!!DBG.caller.name) ? DBG.caller.name + "() " : '') + dbg ;
- LOG("DEBUG: " + dbg , '\033[00;34m') ; }
-function ARGS () { if (!DEBUG_EVS) return ;
+function LOG (log , color) { console.log((color || '\033[00;32m') + log + '\033[00m' ) ; }
+function LOG_STEP(name) { LOG("Step: " + name , '\033[01;32m') ; }
+function LOG_ERR (err) { LOG("ERROR: " + err , '\033[00;31m') ; }
+function LOG_DBG (dbg) { if (!DEBUG ) return ;
+ LOG("DEBUG: " + dbg , '\033[00;34m') ; }
+function LOG_ARGS() { if (!DEBUG_EVS) return ;
LOG("EVENT: " + this) ; var args = arguments ;
- for (arg in args) LOG(" arg: " + JSON.stringify(args[arg])) ; }
-function DUMPFILE(content) { require('fs').write('dump.txt' , content , 'w') ; }
+ for (arg in args) LOG(" arg: " + JSON.stringify(args[arg])) ; }
+function DUMPFILE() { DBG('')("dumping to file: " + Page.url.toLowerCase()) ;
+ require('fs').write('dump.txt' , Page.content , 'w') ; }
+function DBG(context)
+{
+ function Dbg(dbg) { console.log('\033[00;34m' + dbg + '\033[00m') ; }
+ function DbgSubmitEditEvalIn(wiki_text_input , wiki_submit_button)
+ {
+ Dbg("SubmitEdit() wiki_text_input=" + wiki_text_input ) ;
+ Dbg("SubmitEdit() wiki_text_input .nodeName" + wiki_text_input .nodeName) ;
+ Dbg("SubmitEdit() wiki_submit_button" + wiki_submit_button ) ;
+ Dbg("SubmitEdit() wiki_submit_button.nodeName" + wiki_submit_button.nodeName) ; }
+ function DbgSubmitEditEvalOut(new_wiki_text , existing_wiki_text , modified_wiki_text , post_wiki_text , wiki_text_regex)
+ { Dbg("SubmitEdit() new_wiki_text=" + new_wiki_text ) ;
+ Dbg("SubmitEdit() existing_wiki_text=" + existing_wiki_text ) ;
+ Dbg("SubmitEdit() modified_wiki_text=" + modified_wiki_text ) ;
+ Dbg("SubmitEdit() post_wiki_text=" + post_wiki_text ) ;
+ Dbg("SubmitEdit() (modified_wiki_text == existing_wiki_text)=" + (modified_wiki_text == existing_wiki_text)) ;
+ Dbg("SubmitEdit() wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text) ) ; }
+ function DbgSubmitEditOut(post_wiki_text) { if (DEBUG_VB) Dbg("SubmitEdit() returned post_wiki_text=" + post_wiki_text) ; }
+ function DbgVerifyEdit(wiki_title_h1 , wiki_text_div , existing_wiki_title , page_title , existing_wiki_text , new_wiki_text , wiki_text_regex)
+ { Dbg("VerifyEdit() wiki_title_h1=" + wiki_title_h1 + " wiki_title_h1.nodeName=" + wiki_title_h1.nodeName ) ;
+ Dbg("VerifyEdit() wiki_text_div=" + wiki_text_div + " wiki_text_div.nodeName=" + wiki_text_div.nodeName ) ;
+ Dbg("VerifyEdit() existing_wiki_title=" + existing_wiki_title ) ;
+ Dbg("VerifyEdit() page_title=" + page_title ) ;
+ Dbg("VerifyEdit() existing_wiki_text=" + existing_wiki_text ) ;
+ Dbg("VerifyEdit() new_wiki_text=" + new_wiki_text ) ;
+ Dbg("VerifyEdit() (existing_wiki_title == page_title )=" + (existing_wiki_title == page_title) ) ;
+ Dbg("VerifyEdit() wiki_text_regex .test(existing_wiki_text)=" + wiki_text_regex .test(existing_wiki_text) ) ; }
+ function DbgMainLoop() { if (DEBUG_VB) Dbg("MainLoop() ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ; }
+ function DbgGetUrl(get_url) { Dbg("GetUrl() " + get_url) ; }
+
+ return eval('Dbg' + context) ;
+} if (!DEBUG) DBG = function(context) { return function(){} } ;
/* main entry */