Created
January 6, 2012 01:22
-
-
Save lucdew/1568387 to your computer and use it in GitHub Desktop.
Javablogs audience chart
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
import java.util.*; | |
/** | |
* @author Luc | |
* Change startDate and endDate (must span at least 2 full months) | |
* | |
*/ | |
def sdf = new java.text.SimpleDateFormat("dd-MM-yyyy") | |
def baseUrl = "http://www.javablogs.com" | |
def startDate = sdf.parse("01-01-2007") | |
def endDate = sdf.parse("01-02-2009") | |
def resultMap = [:] as TreeMap | |
def addStatsForDate = { Calendar c -> | |
def dayOfMonth = c.get(Calendar.DAY_OF_MONTH) | |
def month = c.get(Calendar.MONTH) | |
def year = c.get(Calendar.YEAR) | |
def xmlSurpler = new XmlSlurper() | |
def url = "${baseUrl}/ViewDaysBlogs.action?date=${dayOfMonth}&month=${month}&year=${year}&view=rss" | |
def rss = xmlSurpler.parse(url) | |
def items = rss.channel.item | |
resultMap[c.getTimeInMillis()]=items.size() | |
println "Processed date ${dayOfMonth} ${month+1} ${year}" | |
} | |
def encodeMap = { | |
List list = [] | |
for (i in (('A'..'Z') + ('a'..'z') + (0..9))) { | |
list << i | |
} | |
String s = "&chd=t:" | |
def resultByYears = [:] | |
int curYear = -1 | |
resultMap.each { long t,int v -> | |
Calendar c = Calendar.getInstance() | |
c.setTimeInMillis(t) | |
if (c.get(Calendar.YEAR)!=curYear) { | |
curYear = c.get(Calendar.YEAR) | |
resultByYears[curYear]=[:] | |
} | |
int count = resultByYears[curYear][c.get(Calendar.MONTH)]?:0 | |
count+=v | |
resultByYears[curYear][c.get(Calendar.MONTH)]=count | |
} | |
resultByYears.values().eachWithIndex { val,index -> | |
if (index > 0) { | |
s+="," | |
} | |
s+=val.values().join(",") | |
} | |
s += "&chxl=0:|" | |
resultByYears.eachWithIndex { k,v,index -> | |
println k | |
if (index > 0) { | |
s+="|" | |
} | |
s+=v.keySet().collect{ | |
(it+1)+"-"+"${k}"[-2..-1] | |
}.join("|") | |
} | |
return s | |
} | |
def createGChart = { | |
def chartTitle = URLEncoder.encode("Javablogs monthly frequentation | since ${sdf.format(startDate)} to ${sdf.format(endDate)}") | |
def url="http://chart.apis.google.com/chart?cht=lc&chs=500x500&chtt=${chartTitle}&chxt=x,y&chxr=1,0,10000&chds=0,10000"+encodeMap() | |
return url | |
} | |
def currentDate = Calendar.getInstance() | |
currentDate.setTime(startDate) | |
while(currentDate.getTimeInMillis() < endDate.getTime()) { | |
addStatsForDate(currentDate) | |
currentDate.add(Calendar.DAY_OF_MONTH,1) | |
} | |
def url = createGChart() | |
print url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment