This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathandroid-os.coffee
61 lines (52 loc) · 1.5 KB
/
android-os.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Description:
# Android os version usage by percentage
#
# Dependencies:
# "phantom": "0.5.2"
# "cheerio": "0.12.1"
#
# Configuration:
# None
#
# Commands:
# hubot android usage
#
# Author:
# mikebob
phantom = require("phantom")
cheerio = require("cheerio")
codenames = []
api = []
url = "http://developer.android.com/about/dashboards/index.html"
module.exports = (robot) ->
last = (arr) ->
return arr[arr.length-1] if arr.length > 0
rpad = (str, length) ->
while str.length < length
str = str + '.'
return str
robot.respond /android usage?$/i, (msg) ->
phantom.create (ph) ->
ph.createPage (page) ->
page.open url, (status) ->
page.evaluate ()->
return {
message: document.getElementById("version-chart").innerHTML
}
, (result)->
$ = cheerio.load result.message, { ignoreWhitespace: true };
res = $("tr").slice(1).map (i, el) ->
v = cheerio.load this
tds = v("td")
if tds.length is 3
codenames.push last codenames # repeat codename
api.push tds.eq(1).html()
else
codenames.push tds.eq(1).html()
api.push tds.eq(2).html()
percent = tds.last().html()
nameapi = rpad "#{last codenames} [#{last api}] ", 30
"#{nameapi} #{percent}"
.join '\n'
msg.send "#{res}"
ph.exit()