Here’s my problem: I was asked to develop a script that grabs the percentage of votes a certain artist has on Q102’s Jingle Ball page, parses it and tweets the current percentage, along with the name of the artist. The API to tweet is done in AppleScript and it already tweets a custom string I pass to it. The only thing I need now is to get the values to build the string.
Now, I tried doing a simple curl like this via Terminal:
curl http://www.q102.com/pages/jb-brackets/
The problem is, in any poll, it doesn’t even get the input types when I curl (they’re radio buttons if you visit the page and try and vote), it just shows this for an artist when I curl’ed it (each poll has a specific ID – this is the poll that contains the data I need to get).
<tr>
<td rowspan="3" class="poll-left"><p><b>Demi Lovato vs. <br>
Avril Lavigne</b></p>
<div id="start_poll_203988"></div>
<div id="poll_203988"></div>
<div id="stop_poll_203988">
<script language="javascript">
//<![CDATA[
insertPoll2('203988');
//]]>
</script>
When you vote for a certain artist, and press ‘Vote’, it shows you the current percentage of votes, like so:
Demi Lovato vs.
Avril Lavigne
Demi Lovato
49.88 %
Avril Lavigne
50.12 %
What I need to do is submit a vote, and get the value, because the stats only show up AFTER I submit the vote (on the main page). The page doesn’t refresh after you vote. Basically, all I need is ‘49.88%’ and ‘50.12%’ (these stats vary, obviously).
Is there any way to do this with curl/something else?
Since you are working with JavaScript you have two options. Analyzing the JS to see what it’s doing then sending those requests manually or you need to control an app that supports JS – a web browser.
Look here for info on the first option : https://stackoverflow.com/a/8304040/991805
As for the second, given what I know, I was able to get it to work using Firefox controlled by Ruby.
This will output :
This script works on Mac and needs the two following gems (libraries) to run : watir-webdriver and Nokogori. As well as Firefox.
Check that Ruby is installed
ruby -vCheck that gem is installed
gem -vInstall the libraries
gem install watir-webdriver nokogiriRun the script
ruby ratings.rb