Skip to content

Instantly share code, notes, and snippets.

@dovydasvenckus
Last active May 10, 2018 10:52
Show Gist options
  • Save dovydasvenckus/1793a0d4bfc4d9c9339685bab5a7ba14 to your computer and use it in GitHub Desktop.
Save dovydasvenckus/1793a0d4bfc4d9c9339685bab5a7ba14 to your computer and use it in GitHub Desktop.
Groovy script that scrapes and counts volunteers in Lithuania army
#!/usr/bin/env groovy
@Grab('org.gebish:geb-spock:2.1')
@Grab('org.seleniumhq.selenium:selenium-chrome-driver:3.6.0')
@Grab('org.seleniumhq.selenium:selenium-support:3.6.0')
@Grab('com.caseyscarborough.colorizer:groovy-colorizer:1.0.0')
import geb.Browser
import com.caseyscarborough.colorizer.Colorizer
final String KAM_URL = 'https://kam.lt/lt/aktuali_informacija_apie_privalomaja_karine_tarnyba/new_2716.html'
final datePattern = ~/\d{4}\.\d{2}\.\d{2}/
class VolunteerLimits {
static final BigDecimal TOTAL_POSITIONS = 3864 //2018 conscripts count
static final BigDecimal WARNING_COUNT = 3500
}
Colorizer.initializeMixins()
Browser.drive {
go KAM_URL
Date lastUpdate = Date.parse('yyyy.MM.dd', ($('.updTimeString').text() =~ datePattern)[0])
List<String> lastRowValues = $("#inner-container tr").last().children().collect { it.first().text() }
assert lastRowValues[0] == 'Iš viso:'
int volunteersThatWereNotInList = lastRowValues[1].toInteger()
int volunteersThatWereInList = lastRowValues[3].toInteger()
int totalVolunteerCount = volunteersThatWereNotInList + volunteersThatWereInList
int countOfPeopleWhoPassedMedicalInspection = lastRowValues[5].toInteger()
println "\nLast update of list: ${lastUpdate.format('yyyy-MM-dd')}".green()
println '-' * 30
println "Volunteers that were not in list: ${volunteersThatWereNotInList}".blue()
println "Volunteers that were in list: ${volunteersThatWereInList}".blue()
println '-' * 30
println "Total number of positions: $VolunteerLimits.TOTAL_POSITIONS".green()
println "Total count of volunteers: ${totalVolunteerCount}"."${getColor(totalVolunteerCount)}"()
println "Count of people who passed medical inspection: ${countOfPeopleWhoPassedMedicalInspection}"."${getColor(countOfPeopleWhoPassedMedicalInspection)}"()
println '-' * 30
[10, 20, 30].each {printCountWhenPercentageRejected(totalVolunteerCount, it)}
println '-' * 30
}
private void printCountWhenPercentageRejected(int totalCount, Integer percentageRejected) {
BigDecimal countAfterRejection = totalCount * (1 - percentageRejected / 100)
println "Total volunteer count if ${percentageRejected}% percent would be rejected during medical examination: ${countAfterRejection}"."${getColor(countAfterRejection)}"()
}
String getColor(BigDecimal count) {
if (count >= VolunteerLimits.TOTAL_POSITIONS)
return 'green'
if (count < VolunteerLimits.TOTAL_POSITIONS && count > VolunteerLimits.WARNING_COUNT)
return 'yellow'
return 'red'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment