Created
May 22, 2011 10:32
-
-
Save ucnv/985341 to your computer and use it in GitHub Desktop.
phantomjs script to tweet through shindanmaker.com
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
var tweet; | |
var twitter_id = ""; | |
var twitter_pw = ""; | |
var phase; | |
var setState = function(nextPhase) { | |
var s = {tweet: tweet, twitter_id: twitter_id, twitter_pw: twitter_pw, phase: nextPhase}; | |
phantom.state = JSON.stringify(s); | |
}; | |
if(!phantom.state.length) { | |
var i, args = Array.prototype.slice.apply(phantom.args); | |
var has_options = args.indexOf('-p') != -1; | |
if(!twitter_id && !twitter_pw) { | |
if((i = args.indexOf('-u')) != -1) { | |
twitter_id = args[i + 1]; | |
args.splice(i, 2); | |
} | |
if((i = args.indexOf('-p')) != -1) { | |
twitter_pw = args[i + 1]; | |
args.splice(i, 2); | |
} | |
tweet = args.join(' '); | |
} | |
if(has_options) { | |
tweet = args[0]; | |
} else { | |
tweet = args.join(' '); | |
} | |
if(!tweet) { | |
console.log('Usage: phantomjs tweet-shindanmaker.js TWEET'); | |
phantom.exit(); | |
} | |
phase = 0; | |
setState(phase); | |
} else { | |
var state = JSON.parse(phantom.state); | |
tweet = state.tweet, phase = state.phase, | |
twitter_id = state.twitter_id, twitter_pw = state.twitter_pw; | |
} | |
switch(+phase) { | |
case 1: | |
var form = document.querySelector('form[name="enter"]'); | |
setState(phase + 1); | |
form.submit(); | |
break; | |
case 2: | |
var f = document.createElement('div'); | |
f.innerHTML = '<form action="" method="post" id="fake-form"> \ | |
<input name="pd[posttitle]" /> \ | |
<input name="pd[postdesc]" /> \ | |
<input name="pd[postlist1]" /> \ | |
<input name="pd[postresult]" /> \ | |
<input name="pd[time]" /> \ | |
</form>'; | |
document.body.appendChild(f); | |
var form = document.getElementById('fake-form'); | |
var t = parseInt(Date.now() / 1000); | |
form.action = 'http://shindanmaker.com/c/fin'; | |
form['pd[posttitle]'].value = | |
form['pd[postdesc]'].value = | |
form['pd[postlist1]'].value = tweet; | |
form['pd[postresult]'].value = '[RESULT1]'; | |
form['pd[time]'].value = t; | |
setState(phase + 1); | |
form.submit(); | |
break; | |
case 3: | |
var n = document.querySelector('.text a'); | |
if(n == null) { | |
console.log('Error occured:'); | |
var ul = document.querySelector('ul'); | |
for(var i = 0; i < ul.childNodes.length; i++) { | |
if(!ul.childNodes[i].innerHTML) continue; | |
console.log(ul.childNodes[i].innerHTML); | |
} | |
phantom.exit(); | |
} else { | |
var url = 'http://twitter.com/intent/tweet?text=+' + tweet + '+' + n.innerHTML + '+'; | |
setState(phase + 1); | |
phantom.open(url); | |
} | |
break; | |
case 4: | |
var form = document.getElementById('update-form'); | |
if(!form && !twitter_id && !twitter_pw) { | |
console.log('Usage: phantomjs tweet-shindanmaker.js TWEET [-u ACCOUNT -p PASSWORD]'); | |
console.log('Account name and passowrd may be required once. Use options like above,'); | |
console.log('or rewrite code directly for the arguments "twitter_id" and "twitter_pw".'); | |
phantom.exit(); | |
} else if(!form) { | |
form = document.getElementById('login-form'); | |
form['session[username_or_email]'].value = twitter_id; | |
form['session[password]'].value = twitter_pw; | |
form.submit(); | |
} else { | |
setState(phase + 1); | |
form.submit(); | |
} | |
break; | |
case 5: | |
phantom.exit(); | |
break; | |
default: | |
setState(phase + 1); | |
phantom.open('http://shindanmaker.com/c/make'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment