Last active
October 13, 2016 06:21
-
-
Save gkio/a1c2511ea716b23172df1a917d85a763 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 yesterdayDataLoaded = false; | |
var lengthOfTables; | |
var yesterdayData; | |
var matches = []; | |
var fs = require('fs') | |
var casper = require('casper').create(); | |
casper.start('http://www.myscore.com.ua/'); | |
casper.then(function() { | |
this.click('.yesterday'); | |
}); | |
function matchTableGeneral(){ | |
var out = {} | |
var externalObject = {} | |
var table; | |
var idofSection = $('.submenu-container .ifmenu .selected').attr('id') | |
idofSection = idofSection.split('-') | |
idofSection = idofSection[idofSection.length - 1] | |
externalObject = [] | |
if(idofSection == 'overall'){ | |
table = '#table-type-1' | |
}else if(idofSection == 'home'){ | |
table = '#table-type-2' | |
}else if(idofSection == 'away'){ | |
table = '#table-type-3' | |
} | |
$(table+' tbody tr').each(function(index){ | |
var obj = {} | |
var id = index + 1 | |
obj['rank'] = $(table+' tbody tr:nth-child('+ id +') td.rank').text() | |
obj['team'] = $(table+' tbody tr:nth-child('+ id +') td.participant_name').text() | |
obj['matches_played'] = $(table+' tbody tr:nth-child('+ id +') td.matches_played').text() | |
obj['wins'] = $(table+' tbody tr:nth-child('+ id +') td.wins').text() | |
obj['draws'] = $(table+' tbody tr:nth-child('+ id +') td.draws').text() | |
obj['losses'] = $(table+' tbody tr:nth-child('+ id +') td.losses').text() | |
obj['goals'] = $(table+' tbody tr:nth-child('+ id +') td.goals').first().text() | |
obj['points'] = $(table+' tbody tr:nth-child('+ id +') td.goals').last().text() | |
externalObject.push(obj); | |
}) | |
out[idofSection] = externalObject; | |
return out | |
} | |
function getOdds(){ | |
var obj = {} | |
obj.odd = {} | |
obj['home_team'] = $('.tname').first().text() | |
obj['away_team'] = $('.tname').last().text() | |
obj['away_team'] = $('.tname').last().text() | |
obj['odd']['bookmaker'] = $('div.blink a').last().text() | |
obj['odd']['1'] = $('td.kx.o_1 span.odds-wrap').text() | |
obj['odd']['X'] = $('td.kx.o_0 span.odds-wrap').text() | |
obj['odd']['2'] = $('td.kx.o_2 span.odds-wrap').text() | |
if(obj['odd']['1'][0] == '-'){ | |
obj['odd']['1'] = obj['odd']['1'].substring(1) | |
} | |
if(obj['odd']['X'][0] == '-'){ | |
obj['odd']['X'] = obj['odd']['X'].substring(1) | |
} | |
if(obj['odd']['2'][0] == '-'){ | |
obj['odd']['2'] = obj['odd']['2'].substring(1) | |
} | |
return obj | |
} | |
function getData(){ | |
var out = []; | |
$('div.table-main table').each(function(index){ | |
var obj = {}; | |
var globalIndex = index +1; | |
obj.country = $('div.table-main table:nth-child('+ globalIndex +') span.country_part').text() | |
obj.tournament = $('div.table-main table:nth-child('+ globalIndex +') span.tournament_part').text() | |
obj.matches = {} | |
$('div.table-main table:nth-child('+ globalIndex +') tbody tr').each(function(index){ | |
var subIndex = index + 1; | |
var num = 'match_' + index; | |
obj.matches[num] = {} | |
obj.matches[num]['time'] = $('div.table-main table:nth-child('+ globalIndex +') tbody tr:nth-child('+ subIndex +') td.cell_ad.time').text() | |
obj.matches[num]['home_team'] = $('div.table-main table:nth-child('+ globalIndex +') tbody tr:nth-child('+ subIndex +') td.team-home span.padr').text() | |
obj.matches[num]['away_team'] = $('div.table-main table:nth-child('+ globalIndex +') tbody tr:nth-child('+ subIndex +') td.team-away span.padl').text() | |
obj.matches[num]['score'] = $('div.table-main table:nth-child('+ globalIndex +') tbody tr:nth-child('+ subIndex +') td.cell_sa.score').text() | |
}) | |
out[index] = obj; | |
}) | |
return out; | |
} | |
casper.then(function(){ | |
this.waitWhileVisible('#preload', function () { | |
// this.echo("its visible"); | |
}) | |
}) | |
casper.then(function(){ | |
yesterdayData = this.evaluate(getData); | |
}) | |
casper.then(function(){ | |
fs.write('yesterdayData.json', JSON.stringify(yesterdayData)); | |
this.capture('yesterday.png') | |
}) | |
casper.then(function() { | |
this.click('.tomorrow'); | |
this.click('.tomorrow'); | |
}); | |
casper.then(function(){ | |
this.waitWhileVisible('#preload', function () { | |
}) | |
}) | |
casper.then(function(){ | |
tomorrowData = this.evaluate(getData); | |
}) | |
casper.then(function(){ | |
fs.write('tomorrowData.json', JSON.stringify(tomorrowData)); | |
this.capture('tomorrow.png') | |
}) | |
casper.then(function() { | |
this.click('div.table-main table:nth-child(1) tbody tr:nth-child(1) td.team-home'); | |
this.waitForPopup(/match/, function(){ | |
console.log('popup opened') | |
}); | |
this.withPopup(/match/, function(){ | |
this.capture('popup.png') | |
this.waitForSelector("#default-odds", function(){ | |
var matchOdds = this.evaluate(getOdds); | |
matches.push(matchOdds); | |
this.click('#li-match-standings a') | |
},function(){ | |
var matchOdds = this.evaluate(getOdds); | |
matches.push(matchOdds); | |
this.click('#li-match-standings a') | |
}); | |
this.waitForSelector("#table-type-1", function(){ | |
this.capture('1.png') | |
var tableGeneral = this.evaluate(matchTableGeneral); | |
this.echo(JSON.stringify(tableGeneral)) | |
matches.push(tableGeneral); | |
this.click('li#tabitem-table-home a'); | |
},function(){ | |
this.capture('1.png') | |
var tableGeneral = this.evaluate(matchTableGeneral); | |
this.echo(JSON.stringify(tableGeneral)) | |
matches.push(tableHome); | |
this.click('li#tabitem-table-home a'); | |
}); | |
this.waitForSelector("#table-type-2", function(){ | |
this.capture('2.png') | |
var tableHome = this.evaluate(matchTableGeneral); | |
this.echo(JSON.stringify(tableHome)) | |
matches.push(tableHome); | |
this.click('li#tabitem-table-away a'); | |
},function(){ | |
this.capture('2.png') | |
var tableHome = this.evaluate(matchTableGeneral); | |
this.echo(JSON.stringify(tableHome)) | |
matches.push(tableHome); | |
this.click('li#tabitem-table-away a'); | |
}); | |
this.waitForSelector("#table-type-3", function(){ | |
this.capture('3.png') | |
var tableAway = this.evaluate(matchTableGeneral); | |
this.echo(JSON.stringify(tableAway)) | |
matches.push(tableAway); | |
},function(){ | |
this.capture('3.png') | |
var tableAway = this.evaluate(matchTableGeneral); | |
this.echo(JSON.stringify(tableAway)) | |
matches.push(tableAway); | |
}); | |
}) | |
}); | |
casper.then(function(){ | |
console.log(JSON.stringify(matches)) | |
fs.write('matchesData.json', JSON.stringify(matches)); | |
}) | |
casper.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment