Last active
September 17, 2017 12:59
-
-
Save dev001hajipro/f7f072c433a4ca810fbe7cc5aec9a656 to your computer and use it in GitHub Desktop.
bubble chart for the subscribers of programming subreddit.
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
<!DOCTYPE html> | |
<svg width="1024" height="1024" font-family="sans-serif" font-size="10" text-anchor="middle"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
var format = d3.format(",d"); | |
var color = d3.scaleOrdinal(d3.schemeCategory20); | |
var pack = d3.pack() | |
.size([width, height]) | |
.padding(1.5); | |
console.log('ready'); | |
d3.csv("subreddit.csv", function (d) { | |
// drop empty data. | |
d.value = +d.value; | |
if (d.value) return d; | |
}, function (error, classes) { | |
if (error) throw error; | |
var root = d3.hierarchy({ children: classes }) | |
.sum(function (d) { return d.value; }) | |
.each(function (d) { | |
if (id = d.data.id) { | |
var id, i = id.lastIndexOf("."); | |
d.id = id; | |
d.package = id.slice(0, i); | |
d.class = id.slice(i + 1); | |
} | |
}); | |
var node = svg.selectAll(".node") | |
.data(pack(root).leaves()) | |
.enter().append("g") | |
.attr("class", "node") | |
.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
node.append("circle") | |
.attr("id", function (d) { return d.id; }) | |
.attr("r", function (d) { return d.r; }) | |
.style("fill", function (d) { return color(d.package); }); | |
node.append("clipPath") | |
.attr("id", function (d) { return "clip-" + d.id; }) | |
.append("use") | |
.attr("xlink:href", function (d) { return "#" + d.id; }); | |
node.append("text") | |
.attr("clip-path", function (d) { return "url(#clip-" + d.id + ")"; }) | |
.selectAll("tspan") | |
.data(function (d) { //return d.class.split(/(?=[A-Z][^A-Z])/g); | |
return [d.id, d.value] | |
}) | |
.enter().append("tspan") | |
.attr("x", 0) | |
.attr("y", function (d, i, nodes) { return 13 + (i - nodes.length / 2 - 0.5) * 10; }) | |
.text(function (d) { return d; }); | |
node.append("title") | |
.text(function (d) { return d.id + "\n" + format(d.value); }); | |
}); | |
</script> |
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
# -*- coding: utf-8 -*- | |
''' | |
https://www.reddit.com/r/programming/のSpecific languagesリンクの | |
プログラミング言語一覧から、購読者数を取得し、CSVを出力する。 | |
''' | |
import csv | |
import requests | |
import requests.auth | |
''' | |
このプログラムを動かすには、Redditのアカウントを取得し、ユーザー設定で | |
CLIENT_ID,SECRETを発行する必要があります。 | |
https://github.com/reddit/reddit/wiki/OAuth2-Quick-Start-Example | |
''' | |
USER = "x" | |
PASS = "x" | |
CLIENT_ID = 'x' | |
SECRET = 'x' | |
AGENT = "RedditBot/0.1 by %s" % (USER) | |
def request_a_token(): | |
''' アクセストークンの取得 ''' | |
client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, SECRET) | |
post_data = {"grant_type": "password", "username": USER, "password": PASS} | |
headers = {"User-Agent": AGENT} | |
response = requests.post("https://www.reddit.com/api/v1/access_token", | |
auth=client_auth, data=post_data, headers=headers) | |
return response.json() | |
def use_the_token(token_type, access_token, url): | |
''' リクエスト ''' | |
headers = {"Authorization": "%s %s" % (token_type, access_token), | |
"User-Agent": AGENT} | |
response = requests.get(url, headers=headers) | |
return response.json() | |
def get_subreddit_names(): | |
''' https://www.reddit.com/r/programming/wiki/faq#wiki_what_language_reddits_are_there.3F ''' | |
return "ATS+C_Programming+Clojure+Coq+Forth+Ioke+Julia+Kotlin+LaTeX+MUMPS+Mathematica+NetLogo+Oberon+ObjectiveC+PHP+PostScript+Python+Racket+Rlanguage+SNOBOL4+SQL+Tcl+VHDL+Verilog+ada+apljk+asm+awk+befunge+brainfuck+ceylon+cobol+coffeescript+coldfusion+cpp+csharp+d_language+datalog+delphi+dylanlang+elm+erlang+fortran+fsharp+golang+gpgpu+groovy+haskell+iolanguage+java+javascript+lisp+logounderground+lua+matlab+mercury+ocaml+pascal+perl+piet+prolog+rebol+rstats+ruby+rust+scala+scheme+shell+smallbasic+smalltalk+visualbasic".split("+") | |
def main(): | |
''' メイン関数 ''' | |
token = request_a_token() | |
# make csv data. | |
arr = [] | |
arr.append(['id', 'value']) # csv header | |
for subreddit in get_subreddit_names(): | |
json = use_the_token( | |
token["token_type"], | |
token["access_token"], | |
"https://oauth.reddit.com/r/%s/about" % subreddit) | |
arr.append([subreddit, json["data"]["subscribers"]]) | |
csv_file = open('subreddit.csv', 'w') | |
writer = csv.writer(csv_file, lineterminator='\n') | |
writer.writerows(arr) | |
if __name__ == '__main__': | |
main() |
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
id | value | |
---|---|---|
ATS | 212 | |
C_Programming | 29113 | |
Clojure | 12515 | |
Coq | 978 | |
Forth | 1104 | |
Ioke | 116 | |
Julia | 3470 | |
Kotlin | 5847 | |
LaTeX | 12862 | |
MUMPS | 29 | |
Mathematica | 3557 | |
NetLogo | 113 | |
Oberon | 174 | |
ObjectiveC | 5341 | |
PHP | 51230 | |
PostScript | 86 | |
Python | 186322 | |
Racket | 2649 | |
Rlanguage | 7519 | |
SNOBOL4 | 17 | |
SQL | 19128 | |
Tcl | 659 | |
VHDL | 326 | |
Verilog | 510 | |
ada | 1035 | |
apljk | 604 | |
asm | 4394 | |
awk | 680 | |
befunge | 32 | |
brainfuck | 392 | |
ceylon | 172 | |
cobol | 692 | |
coffeescript | 2411 | |
coldfusion | 920 | |
cpp | 53834 | |
csharp | 43185 | |
d_language | 2109 | |
datalog | 44 | |
delphi | 1050 | |
dylanlang | 80 | |
elm | 5356 | |
erlang | 5886 | |
fortran | 1515 | |
fsharp | 3982 | |
golang | 33668 | |
gpgpu | 1884 | |
groovy | 1754 | |
haskell | 28653 | |
iolanguage | 201 | |
java | 73552 | |
javascript | 128709 | |
lisp | 11925 | |
logounderground | 21 | |
lua | 5425 | |
matlab | 12571 | |
mercury | 92 | |
ocaml | 3737 | |
pascal | 489 | |
perl | 10530 | |
piet | 37 | |
prolog | 1710 | |
rebol | 111 | |
rstats | 13484 | |
ruby | 35617 | |
rust | 28630 | |
scala | 14663 | |
scheme | 4385 | |
shell | 1851 | |
smallbasic | 20 | |
smalltalk | 1078 | |
visualbasic | 3343 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment