[Question]
  [
**Task:**
You are an amazing programmer and Stackoverflow-answerer, and you decide to answer every question with a bounty on Stackoverflow. You are so good, that you manage to get all the bounties in all the questions. While you wait for the rep to come flooding in, you write a program that goes and find out what the total amount of rep is in all those bounties.
**Rules:**
* When run,
	+ Your program will navigate through the featured tab on Stack Overflow.
	+ It will scrape out the value of each bounty,
	+ Then it will add it up and display the total
* It has to download data from anywhere on SO (and only SO), but I would recommend using <https://stackoverflow.com/questions?pagesize=50&sort=featured> , as it is only about 10 pages
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code wins
  
[Answer]
## JavaScript - ~~176~~ ~~133~~ ~~130~~ ~~108~~ 106
```
function f()(t+=$("[title~=an]").text(),u=$("[rel*=x]")[0])?$("html").load(u.href,f):alert(eval(t));f(t=0)
```
*Edit 1: trimmed some selectors down and used the `?:` suggestion from Google's Closure Compiler (via @Sirko - thanks)*
*Edit 2: initialise `s` inside `d` and initialise `t` as `0` instead of `""`*
*Edit 3: realised I don't actually need to target a specific container and can sweep the whole document, which gets rid of a bunch of `.find` calls and an unnecessary selector (plus the variable holding it)*
*Edit 4: shove the `t` initialiser in the function call to avoid a `;` (it'll get hoisted to the top anyway) and squash the function down to one statement (combine two statements into one inside the ternary statement condition) to drop the `{}`*
**Note**: I'm not sure if it's cheating, but this has to be run from a console window of a browser already pointing at `http://stackoverflow.com/questions?page=1&sort=featured`. It relies on the fact that **jQuery** and the appropriate paging links are available on the page itself. Also, it only appears to work in **Firefox** and not in IE or Chrome.
**Output** (at time of posting):
```
38150 (in an alert dialog)
```
**Exploded/commented**:
```
function f()
    //concat all the bounty labels to t (they take the format "+50")
    //happens to be elements with title attribute containing word 'an'
    (t+=$("[title~=an]").text(),
    //find the "next" (has rel=next attribute) button
    u = $("[rel*=x]")[0])       
        ?
        //if there is a next button, load it, and then recurse f again
        $("html").load(u.href,f)
        :
        //else eval the 0+a+b+...+z tally and alert the result
        alert(eval(t))
//kick off the initial scrape (and simultaneously init the total tally)
f(t=0)
```
[Answer]
## Python - ~~232~~, ~~231~~, ~~195~~, ~~183~~, ~~176~~, 174
Parses the HTML from <https://stackoverflow.com/questions?sort=featured> using regular expressions.
The upper bound of `range` in the `for` loop *must* be `number of pages + 1` or else the code will raise `HTTPError` because of 404s. Default number of results per-page is 15, which is what the code uses (omitting `?pagesize=50` saves on characters and is just as effective).
Thanks to **@Gabe** for the tip on reducing char count even further.
**Golfed**:
```
import requests,re;print sum(sum(map(int,re.findall(r"<.*>\+(\d+)<.*>",requests.get("https://stackoverflow.com/questions?sort=featured&page=%u"%i).text)))for i in range(1,33))
```
**Output** (at time of posting):
```
37700
```
**Un-golfed**:
Here's a somewhat un-golfed version that should be a bit easier to read and understand.
```
import requests, re
print sum(
          sum(
              map( int,
                   re.findall( r"<.*>\+(\d+)<.*>",
                               requests.get( "https://stackoverflow.com/questions?sort=featured&page=%u" % i).text
                   )
              )
          ) for i in range( 1, 33 )
      )
```
[Answer]
# Rebol - ~~164~~ ~~133~~ 130 (139 with 404 check)
Parses the html using the `parse` sub-language of Rebol. Checks the first 98 pages. I realised I have the same constraint as the python solution - too many repeats hit 404 errors and stop the execution.
Thanks to @rgchris for many improvements! Updated to check up to 98 pages.
`s: 0 repeat n 99[parse read join http://stackoverflow.com/questions?sort=featured&page= n[15[thru{>+}copy x to{<}(s: s + do x)]]]s`
With error checking for 404s (139):
`s: 0 repeat n 99[attempt[parse read join http://stackoverflow.com/questions?sort=featured&page= n[15[thru{>+}copy x to{<}(s: s + do x)]]]]s`
**Test**
```
>> s: 0 repeat n 20[parse read join http://stackoverflow.com/questions?sort=featured&page= n[15[thru{>+}copy x to{<}(s: s + do x)]]]s
== 23600
>> s: 0 repeat n 99[attempt[parse read join http://stackoverflow.com/questions?sort=featured&page= n[15[thru{>+}copy x to{<}(s: s + do x)]]]]s
Script: none Version: none Date: none
== 36050
```
**Explanation**
Rebol ignores whitespace, hence you can put it all on one line like that if you choose. PARSE takes two inputs, and the first argument *(`read join ...`)* is fairly self-explanatory. But here are some comments on the parse dialect instructions, in a more traditional indentation:
```
s: 0
repeat n 99 [
    parse read join http://stackoverflow.com/questions?sort=featured&page= n [
        ;-- match the enclosed pattern 15 times (the rule will fail politely when there are less entries)
        15 [
            ;-- seek the match position up THRU (and including) the string >+
            thru {>+}
            ;-- copy contents at the current position up TO (but not including) <
            copy x to {<}
            ;-- (Basically, run some non-dialected Rebol if this match point is reached) the do is a bit dangerous as it runs the string as code
            (s: s + do x)
        ]
    ]
]
;-- evaluator returns last value, we want the value in S
;-- (not the result of PARSE, that's a boolean on whether the end of input was reached)
s
```
[Answer]
# Ruby, 260
```
require'open-uri'
require'zlib'
i=b=0
d=''
until /"has_more":f/=~d
i+=1
d=Zlib::GzipReader.new(open("http://api.stackexchange.com/2.2/questions/featured?site=stackoverflow&page=#{i}&pagesize=100")).read
b+=d.scan(/"bounty_amount":(\d+)/).map{|x|x[0].to_i}.reduce :+
end
p b
```
Uses the Stack Exchange API.
Output (as of time of original post):
```
37200
```
I'm not counting the `&pagesize=100` in the character count, because it works without it, but I just added that for convenience while testing. If you remove that, it does the same thing (except it eats more quota and takes slightly longer).
[Answer]
# Rebmu - ~~108~~107
```
rtN99[parseRDrj[http://stackoverflow.com/questions?sort=featured&page=N][15[thru{>+}copyXto{<}(a+JdoX)]]]j
```
**Test**
(at 19:05 AEST)
```
>> rebmu [rtN99[parseRDrj[http://stackoverflow.com/questions?sort=featured&page=N][15[thru{>+}copyXto{<}(a+JdoX)]]]j]
Script: none Version: none Date: none
== 79200
```
[Rebmu](https://github.com/hostilefork/rebmu/tree/2965e013aa6e0573caa326780d97522a425103f0) looks rather cryptic, but it is quite readable once you get the hang of it. Let's start by unmushing it and laying it out properly.
```
rt n 99 [
    parse rd rj [
        http://stackoverflow.com/questions?sort=featured&page= n
    ][
        15 [
            thru {>+}
            copy x to {<}
            (a+ j do x)
        ]
    ]
]
j
```
Rebmu is a dialect of Rebol so you can see the similarities in the solution. Rebmu can't reduce the size of every statement yet, but it is an evolving language. Thanks again to @rgchris for the improvements to my first attempt.
[Answer]
# Ruby - 197
Short version:
```
require 'nokogiri'
require 'open-uri'
s=0
(1..33).each{|p|Nokogiri::HTML(open("http://stackoverflow.com/questions?page=#{p}&sort=featured")).css('.bounty-indicator').each{|b|s+=b.content.to_i}}
p s
```
Human friendly version:
```
require 'nokogiri'
require 'open-uri'
s=0
(1..33).each do |p|
    Nokogiri::HTML(open("http://stackoverflow.com/questions?page=#{p}&sort=featured")).css('.bounty-indicator').each do |b|
        s += b.content.to_i
    end
end
puts s
```
And answer - `39700`
# Ruby with script parameters - 139
```
require 'nokogiri'
require 'open-uri'
s=0
(1..33).each{|p|Nokogiri::HTML(open(ARGV[0]+p.to_s)).css(ARGV[1]).each{|b|s+=b.content.to_i}}
p s
```
To run this from bash just type
```
ruby code_golf_stack_overflow2.rb http://stackoverflow.com/questions?sort=featured\&page= .bounty-indicator
```
[Answer]
## PHP - 121 bytes
```
<?for(;preg_filter('/>\+(\d+)/e','$t+=\1',@file('http://stackoverflow.com/questions?sort=featured&page='.++$n)););echo$t;
```
Using a regex 'eval' modifier, to avoid using `array_sum` or similar. Seems to be the shortest solution among valid entries.
[Answer]
## PHP, ~~134~~, ~~131~~, 127
`while($q=array_sum(preg_filter('#.*>\+#',0,file("http://stackoverflow.com/questions?sort=featured&page=".++$n))))$s+=$q;echo$s;`
Will loop through **all** pages, `pagesize` is not set to save bytes so more `GET`s.
Very Very Dirty, but... taking advantage of `PHP`'s "flaws" !
* no space after `echo`
* `while` stops at assignment
* output after `RegEx` replace is a string starting with the bounty amount
* `array_sum()` adds up strings
* `$n` and `$s` are initialized, but starting from nothing is equiv. as starting from zero
* etc...
[Answer]
**Bash 206**
optimizations possible, too lazy
```
s=0;for i in `seq 1 11`;do for j in `wget -q -O - "http://stackoverflow.com/questions?pagesize=50&sort=featured&page=$i" | grep -o -E "bounty worth [0-9]*" | grep -o -E "[0-9]*"`;do s=$(($s+$j));done;done;echo $s
```
result:
```
39450
```
[Answer]
# Javascript - ~~129~~ ~~119~~ ~~110~~ 107 characters
## EDIT: INVALID ANSWER! This only handles the "Top featured questions", which only has a fraction of them. Alconja's answer is more valid.
```
s="#mainbar";t="";eval("$(s).find('.bounty-indicator').each(function(){t+=this.innerHTML});alert(eval(t))")
```
Execute on <https://stackoverflow.com/?tab=featured> in a console window. Based on the solution by Alconja.
Golfed it a bit more by removing unneeded whitespaces.
Used eval to remove the function call, clearing another 9 characters.
cleared out some more unneeded whitespace.
[Answer]
# Java, 540 chars
***Warning: the number of active bounties is ~470. This code will access a page on stackoverflow that many times. It may get you in trouble with them for making so many data requests.***
```
import java.io.*;import java.net.*;public class B{public static void main(String[]A){String u="http://stackoverflow.com/questions",d;Long i,s=i=0L,n=i.parseLong(o(u).replaceAll("^.*b.>(\\d+).*$","$1"));while(i++<n){d=o(u+"?pagesize=1&sort=featured&page="+n).replaceAll("^.*ion.>.(\\d+).*$","$1");s+=d.matches(".*\\D.*")?0:n.parseLong(d);}System.out.print(s);}static String o(String s){String d="";try{BufferedReader r=new BufferedReader(new InputStreamReader(new URL(s).openStream()));while((s=r.readLine())!=null)d+=s;}finally{return d;}}}
```
My output was `23400`, but when I ran @TonyH's code, I got `37550`. Bad news.
Pretty code:
```
import java.io.*;
import java.net.*;
public class StackOverflowBounty {
    public static void main(String[] args) {
        String u = "http://stackoverflow.com/questions", d;
        Long i, s = i = 0L, n = i.parseLong(o(u).replaceAll("^.*b.>(\\d+).*$", "$1"));
        while (i++ < n) {
            d = o(u + "?pagesize=1&sort=featured&page=" + n).replaceAll("^.*ion.>.(\\d+).*$", "$1");
            s += d.matches(".*\\D.*") ? 0 : n.parseLong(d);
        }
        System.out.print(s);
    }
    static String o(String s) {
        String d = "";
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader(new URL(s).openStream()));
            while ((s = r.readLine()) != null) {
                d += s;
            }
        } finally {
            return d;
        }
    }
}
```
The way this works is simple. It reads from the url `http://stackoverflow.com/questions"` to determine the number of questions that have bounties (note: if the number increases, the program fails, but if it drops, it works fine). It searches for this number using the regex: `b.>(\\d+)`. This has worked in all tests to date, but if someone asked a question that matches that regex, this might not work.
Then, we open the url `http://stackoverflow.com/questions?pagesize=1&sort=featured&page=` + `current question #`. In other words, we open a new page for each featured question, and force the number of questions to be only `1`, so we will get them all. The reputation part will always match `ion.>.(\\d+)`, so I use that to find it. I split the operation into two parts so that I could cheaply check if the number of questions reduced (ie the string returned is not an integer).
Then, we sum up all the reputation and print it.
It took about 3 minutes and 20 seconds to run on my machine.
---
Does anyone know why it isn't printing the right number?
[Answer]
## C# - 407
```
class B{void Main(string[] a){var o=0;for(int i=1;i<11;i++){var r=((System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(string.Format(a[0]+"&page={0}",i)))).GetResponse();if(r.ContentLength>0){using(var s=new StreamReader(r.GetResponseStream()))foreach(Match m in Regex.Matches(s.ReadToEnd(),"bounty worth (.+?) "))o+=int.Parse(m.Value.Substring(m.Value.IndexOf('h')+2));}}Console.Write(o);}}
```
Using Stackoverflow.com. Same as below, except no Gzip decompressing and different regex.
**Test**
```
> prog.exe http://stackoverflow.com/questions?pagesize=50&sort=featured
38150
```
Weirdly, getting a different value than below.
---
## C# - 496
This uses api.stackexchange which is gzipped and json.
```
using System.IO.Compression;class B{void Main(string[] a){var o=0;for(int i=1;i<11;i++){var r=((System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(string.Format(a[0]+"&page={0}",i)))).GetResponse();if(r.ContentLength>0)using(var s=new StreamReader(new GZipStream(r.GetResponseStream(),CompressionMode.Decompress)))foreach(Match m in Regex.Matches(s.ReadToEnd(),@"bounty_amount"":(.+?),"))o+=int.Parse(m.Value.Substring(m.Value.IndexOf(':')+1).Replace(",",""));}Console.Write(o);}}
```
**Unminified:**
```
using System.IO.Compression;
class B
{
    void Main(string[] a)
    {
        var o = 0;
        for (int i=1; i<11; i++) {
            var w = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(string.Format(a[0]+"&page={0}",i)));
            if(w.GetResponse().ContentLength > 0)
                using(var s = new StreamReader(new GZipStream(w.GetResponse().GetResponseStream(),CompressionMode.Decompress)))
                    foreach(Match m in Regex.Matches(s.ReadToEnd(), @"bounty_amount"":(.+?),"))
                        o += int.Parse(m.Value.Substring(m.Value.IndexOf(':')+1).Replace(",", ""));
        }
        Console.Write(o);
    }
}
```
**Test**
Default pagesize:
```
> prog.exe http://api.stackexchange.com/2.2/questions/featured?site=stackoverflow
25300
```
Pagesize=100:
```
> prog.exe "http://api.stackexchange.com/2.2/questions/featured?site=stackoverflow&pagesize=100"
37400
```
[Answer]
# jQuery 191
```
i=0;function f(p){$.get('//api.stackexchange.com/2.2/questions/featured?site=stackoverflow&page='+p,function(d){for(x in d.items)i+=d.items[x].bounty_amount;d.has_more?f(p+1):alert(i)})};f(1)
```
It works from anywhere in stackexchange(and many other sites), no need to be in a specific page as in @Alconja/@NateKerkhofs answers
[Answer]
## PHP - 139
**Golfed:**
```
<?php
$a=file_get_contents('http://stackoverflow.com/?tab=featured');preg_match_all('/n">\+([0-9]+)<\/div>/',$a,$r);echo array_sum($r[1]);
```
**Ungolfed - 147**
Simple `file_get_contents` / `preg_match` / `array_sum`
```
<?php
$a = file_get_contents('http://stackoverflow.com/?tab=featured');
preg_match_all('/n">\+([0-9]+)<\/div>/', $a, $r);
echo array_sum($r[1]);
```
**Test:**
> 
> php run.php
> 
> 
> 10250
> 
> 
> 
[Answer]
# Bash 174
Based on <https://codegolf.stackexchange.com/a/25180/7664>:
```
s=0;for i in {1..11};do for j in `wget -qO- "stackoverflow.com/questions?pagesize=50&sort=featured&page=$i"|cut -d' ' -f18|egrep '^[0-9]+$'`;do s=$(($s+$j));done;done;echo $s
```
[Answer]
# Python (174 characters):
Expanding on the python answer above (don't have enough karma to comment):
```
import requests,re;print sum(sum(map(int,re.findall(r"<.*>\+(\d+)<.*>",requests.get("http://stackoverflow.com/questions?sort=featured&page=%u"%i).text)))for i in range(1,33))
```
Requests in lieu of urllib cuts down on 2 chars.
[Answer]
# Ruby (176 chars):
Following Tony H.'s example of using hard-coded page numbers, here's what I got:
```
require'open-uri';b=0;(1..29).each{|i|d=open("http://stackoverflow.com/questions?sort=featured&page=#{i}").read;b+=d.scan(/<.*>\+(\d+)<.*>/).map{|x|x[0].to_i}.reduce 0,:+};p b
```
gave me 35300 at the time of writing.
]