Created
June 25, 2014 15:33
-
-
Save ruescasd/c94e82e32179fc41d21a to your computer and use it in GitHub Desktop.
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
import java.io.BufferedReader | |
import java.io.IOException | |
import java.io.InputStreamReader | |
import java.net.URL | |
import java.net.URLConnection | |
import java.net.HttpURLConnection | |
import java.net.URLStreamHandler | |
import java.net.URLEncoder | |
import java.net.CookieManager | |
import java.net.CookieHandler | |
import java.net.CookiePolicy | |
import org.silvertunnel_ng.netlib.adapter.url.NetlibURLStreamHandlerFactory | |
import org.silvertunnel_ng.netlib.adapter.url.URLGlobalUtil | |
import org.silvertunnel_ng.netlib.api.NetFactory | |
import org.silvertunnel_ng.netlib.api.NetLayer; | |
import org.silvertunnel_ng.netlib.api.NetLayerIDs | |
import org.silvertunnel_ng.netlib.api.util.TcpipNetAddress | |
import org.silvertunnel_ng.netlib.util.HttpUtil | |
import org.silvertunnel_ng.netlib.api.util._ | |
import org.silvertunnel_ng.netlib.api._ | |
import scala.collection.JavaConversions._ | |
import scala.util.Random | |
object Main extends App { | |
def initTor() = { | |
val netLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR) | |
print("Wait until ready..") | |
netLayer.waitUntilReady() | |
println("OK") | |
netLayer | |
} | |
def openHome(factory: NetlibURLStreamHandlerFactory) = { | |
val urlStr = "http://www.allourideas.org/ostracismo?locale=es" | |
val url = Utils.getUrl(factory, urlStr) | |
val urlConnection = url.openConnection() | |
urlConnection.setDoInput(true) | |
urlConnection.setDoOutput(false) | |
urlConnection.connect() | |
val response = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())) | |
val data = Stream.continually(response.readLine()).takeWhile(_ != null).mkString | |
Utils.parseHome(data) | |
} | |
def init() = { | |
val netLayer = initTor() | |
val factory = new NetlibURLStreamHandlerFactory(false) | |
factory.setNetLayerForHttpHttpsFtp(netLayer) | |
factory | |
} | |
def startSession(factory: NetlibURLStreamHandlerFactory) = { | |
println("** Starting new session") | |
val cookieManager = new CookieManager() | |
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL) | |
CookieHandler.setDefault(cookieManager) | |
val (parseData, token, questionId) = openHome(factory) | |
val session = Session(token, questionId, factory, cookieManager) | |
val cookieJar = cookieManager.getCookieStore() | |
val cookies = cookieJar.getCookies() | |
(session, parseData) | |
} | |
val factory = init() | |
1 to 20 foreach { _ => | |
val votes = 10 + Random.nextInt(15) | |
val (session, parseData) = startSession(factory) | |
session.vote(parseData, votes) | |
} | |
} | |
case class Session(token: String, questionId: String, factory: NetlibURLStreamHandlerFactory, cookieManager: CookieManager) { | |
val urlRoot = "http://www.allourideas.org/" | |
val CHARSET = "UTF-8" | |
def makePostQuery(appearance: String, promptId: String, direction: String, time: String) = { | |
val tokenU = URLEncoder.encode(token, CHARSET) | |
val timeU = URLEncoder.encode(time, CHARSET) | |
val promptU = URLEncoder.encode(promptId, CHARSET) | |
val appearanceU = URLEncoder.encode(appearance, CHARSET) | |
val directionU = URLEncoder.encode(direction, CHARSET) | |
s"authenticity_token=$tokenU&time_viewed=$timeU&prompt_id=$promptU&appearance_lookup=$appearanceU&direction=$directionU" | |
} | |
def vote(parseData: ParseData, max: Int = 0, count: Int = 0): Int = { | |
val delay = 2000 + Random.nextInt(4000) | |
println(s"* waiting $delay ms..") | |
Thread.sleep(delay) | |
val positionLeft = Choice.prefs.indexOf(parseData.leftId.toInt) | |
val positionRight = Choice.prefs.indexOf(parseData.rightId.toInt) | |
println(s"* got ${Choice.names(parseData.leftId.toInt)} vs ${Choice.names(parseData.rightId.toInt)}") | |
println(s"* got $positionLeft vs $positionRight") | |
val direction = if(positionLeft > positionRight) "left" | |
else "right" | |
println(s"* VOTING $direction ($count)") | |
val query = makePostQuery(parseData.appearance, parseData.promptId, direction, delay.toString) | |
println(s"* Query = $query") | |
val urlStr = urlRoot + "questions/" + questionId + "/prompts/" + parseData.promptId + "/vote.js" + "?locale=es" | |
val url = Utils.getUrl(factory, urlStr) | |
val postConnection = url.openConnection().asInstanceOf[HttpURLConnection] | |
postConnection.setRequestMethod("POST") | |
postConnection.setRequestProperty("Content-Length", query.length.toString) | |
postConnection.setRequestProperty("Content-Type", s"application/x-www-form-urlencoded; charset=${CHARSET}") | |
postConnection.setRequestProperty("Connection", "keep-alive") | |
postConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01") | |
postConnection.setRequestProperty("Accept-Language", "en,es;q=0.8,en-US;q=0.6,pt;q=0.4") | |
postConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36") | |
postConnection.setRequestProperty("Host", "www.allourideas.org") | |
postConnection.setRequestProperty("Origin", "http://www.allourideas.org") | |
postConnection.setRequestProperty("Referer", "http://www.allourideas.org/ostracismo?locale=es") | |
postConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest") | |
postConnection.setDoInput(true) | |
postConnection.setDoOutput(true) | |
val cookieJar = cookieManager.getCookieStore() | |
val cookies = cookieJar.getCookies() | |
val cookieString = cookies.map(_.toString).mkString("; ") | |
postConnection.setRequestProperty("Cookie", cookieString) | |
val output = postConnection.getOutputStream() | |
output.write(query.getBytes(CHARSET)) | |
output.close() | |
postConnection.connect() | |
val postResponse = new BufferedReader(new InputStreamReader(postConnection.getInputStream())) | |
val postResponseString = Stream.continually(postResponse.readLine()).takeWhile(_ != null).mkString | |
println("* RESPONSE " + postConnection.getResponseCode()) | |
val parsed = Utils.parseJson(postResponseString) | |
if(count < max) { | |
vote(parsed, max, count + 1) | |
} | |
else { | |
count | |
} | |
} | |
} | |
object Utils { | |
def printCookies(cookieManager: CookieManager) = { | |
val cookieJar = cookieManager.getCookieStore() | |
val cookies = cookieJar.getCookies() | |
for(cookie <- cookies) { | |
println("CookieHandler retrieved cookie: " + cookie) | |
} | |
} | |
def getUrl(factory: NetlibURLStreamHandlerFactory, urlString: String) = { | |
val handler = factory.createURLStreamHandler("http"); | |
new URL(null, urlString, handler) | |
} | |
def parseHome(html: String) = { | |
val tokenRx = "AUTH_TOKEN = \"([^\"]+)\"".r.unanchored | |
val questionIdRx = "data-question_id='([0-9]+)'".r.unanchored | |
val questionIdRx(questionId) = html | |
val tokenRx(token) = html | |
(parseHtml(html), token, questionId) | |
} | |
def parseJson(json: String) = { | |
val leftIdRx = "\"left_choice_id\":([^,]+),".r.unanchored | |
val leftIdRx(leftId) = json | |
val rightIdRx = "\"right_choice_id\":([^,]+),".r.unanchored | |
val rightIdRx(rightId) = json | |
val leftRx = "\"left_choice_url\":\"([^\"]+)\"".r.unanchored | |
val leftRx(leftUrl) = json | |
val rightRx = "\"right_choice_url\":\"([^\"]+)\"".r.unanchored | |
val rightRx(rightUrl) = json | |
val promptRx = "\"prompt_id\":([0-9]+)".r.unanchored | |
val promptRx(prompt) = json | |
val appearanceRx = "\"appearance_lookup\":\"([^\"]+)\"".r.unanchored | |
val appearanceRx(appearance) = json | |
println(s"parseJson: leftId=$leftId rightId=$rightId prompt=$prompt appearance=$appearance") | |
ParseData(leftId, rightId, appearance, prompt, leftUrl, rightUrl) | |
} | |
def parseHtml(html: String) = { | |
val leftIdRx = "<button[^>]*data-choice_id='([0-9]*)'[^>]*data-side='leftside'".r.unanchored | |
val leftIdRx(leftId) = html | |
val rightIdRx = "<button[^>]*data-choice_id='([0-9]*)'[^>]*data-side='rightside'".r.unanchored | |
val rightIdRx(rightId) = html | |
val promptRx = "<input[^>]*id=\"prompt_id\"[^>]*value=\"([0-9]*)\"".r.unanchored | |
val promptRx(prompt) = html | |
val appearanceRx = "<input[^>]*id=\"appearance_lookup\"[^>]*value=\"([^\"]*)\"".r.unanchored | |
val appearanceRx(appearance) = html | |
println(s"parseHtml: leftId=$leftId rightId=$rightId prompt=$prompt appearance=$appearance") | |
ParseData(leftId, rightId, appearance, prompt) | |
} | |
} | |
case class ParseData(leftId: String, rightId: String, appearance: String, promptId: String, leftUrl: String = "", rightUrl: String = "") | |
case object Choice { | |
val names = Map( | |
319522 -> "Alberto Ruiz- Gallardón", | |
319503 -> "Bárcenas", | |
319532 -> "Ana Botella", | |
319516 -> "José María Aznar", | |
319512 -> "Mª Dolores de Cospedal", | |
319497 -> "Mariano Rajoy", | |
319509 -> "Cristóbal Montoro", | |
319495 -> "Infanta Cristina", | |
319511 -> "Artur Mas", | |
319507 -> "Emilio Botín", | |
319530 -> "José Luis Rodríguez Zapatero", | |
319498 -> "Alfredo Pérez Rubalcaba", | |
319508 -> "Luis de Guindos", | |
319517 -> "Amancio Ortega", | |
319506 -> "Soraya Sáenz de Santamaría", | |
319518 -> "J. M. Lara Bosch", | |
319504 -> "Jorge Verstrynge", | |
319502 -> "Juan Carlos I", | |
319526 -> "Miguel Arias Cañete", | |
319519 -> "Rosa Díez", | |
319531 -> "Juan Rosell", | |
319521 -> "Jorge Fernández Díaz", | |
319514 -> "Pedro J. Ramírez", | |
319513 -> "Florentino Pérez", | |
319501 -> "Felipe VI", | |
319496 -> "Pablo Iglesias", | |
319533 -> "Fátima Báñez", | |
319525 -> "Alberto Rivera", | |
319524 -> "J. M. García- Margallo", | |
319523 -> "Alberto Núñez Feijoó", | |
319510 -> "César Alierta", | |
319534 -> "Manolo Canduela", | |
319505 -> "Eduardo Madina", | |
319529 -> "Jesús Posada", | |
319515 -> "Susana Díaz", | |
319535 -> "Paquirrin", | |
319520 -> "Carme Chacón", | |
319528 -> "Patxi López", | |
319500 -> "Willy Meyer", | |
319499 -> "Cayo Lara", | |
319527 ->"Rafael Nadal" | |
) | |
val prefs = List( | |
319522, | |
319503, | |
319532, | |
319516, | |
319512, | |
319497, | |
319509, | |
319495, | |
319511, | |
319507, | |
319530, | |
319498, | |
319508, | |
319517, | |
319506, | |
319518, | |
319504, | |
319502, | |
319526, | |
319519, | |
319531, | |
319521, | |
319514, | |
319513, | |
319501, | |
319496, | |
319533, | |
319525, | |
319524, | |
319523, | |
319510, | |
319534, | |
319505, | |
319529, | |
319515, | |
319535, | |
319520, | |
319528, | |
319500, | |
319499, | |
319527 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment