3

The API provides methods to sort and filter individual questions by view count.

In order to calculate the question view count totals per User, some grouping and set operations are required.

e.g.

total_views display_name [user_id]
------------------------------------------
1000 John Doe [12]
900 Joe Snow [431]
. . . etc etc

Post your solution to this use case in the language/library syntax of your choice.

2 Answers 2

1

C# / Soapi.CS


using Soapi.CS

Context
    .Official
    .StackApps
    .Questions
    .PageCount(0) // all pages. be careful with this
    .PageSize(100)
    .GroupBy(question => question.OwnerId)
    .Select(group => new
    {
        User = group.First().Owner.DisplayName + "[" + group.First().Owner.UserId + "]",
        Views = group.Sum(question => question.ViewCount)
    }).OrderByDescending(item => item.Views)
    .ToList().ForEach(item => Console.WriteLine("{0} - {1}", item.Views, item.User));

Output:

17339 - Kevin Montrose[2]
10642 - George Edison[18]
8711 - code poet[14]
3402 - systempuntoout[51]
3015 - Farseeker[816]
2894 - lfoust[11]
2253 - Franci Penov[13]
1798 - Felix[956]
1772 - Soviut[825]
1655 - S.Mark[40]
1595 - Adam Wright[5]
1400 - Jedi Master Spooky[192]
1383 - Jeff Atwood[4]
1293 - Matt S.[30]
1158 - Edan Maor[1241]
1079 - Steffen Opel[377]
1033 - carson[33]
1024 - Bill the Lizard[9]
977 - Dave DeLong[448]
977 - Mark Rushakoff[1048]
949 - Shay Erlichmen[614]
947 - Nick Presta[186]
940 - Thomas McDonald[1031]
916 - Dave Swersky[29]
813 - Jonathan[938]
812 - eWolf[664]
806 - swanson[839]
780 - Adam[1395]
765 - Lucas Jones[570]
726 - TheHurt[17]
705 - johnwards[638]
690 - Catchwa[700]
669 - Kyle Cronin[42]
664 - Jason[19]
637 - phsr[693]
623 - codeka[1290]
602 - InfinitiesLoop[1394]
588 - nabeelmukhtar[1026]
566 - Yacoby[1049]
499 - conmulligan[449]
437 - Ricky[199]
432 - Dennis Williamson[171]
425 - radius[295]
420 - SztupY[1265]
360 - Soldier.moth[91]
359 - ColinD[222]
353 - Igor Zevaka[113]
336 - Koning Baard[451]
320 - Ryan Brunner[1263]
311 - jjnguy[39]
298 - Greg Bray[150]
297 - fukas78[667]
287 - Nathan Reed[680]
287 - Michael B.[234]
283 - iconiK[375]
258 - Peter Mourfield[424]
247 - RichH[757]
244 - blork[1849]
238 - Casebash[169]
237 - Joel Potter[8]
221 - ripper234[298]
214 - TheKaptain[1232]
207 - mlaw[2467]
207 - Lucas McCoy[239]
205 - Nippysaurus[1221]
203 - James A. Rosen[140]
190 - Vitaly Polonetsky[1771]
188 - bpedro[2222]
180 - Shane[1270]
168 - Robert Munteanu[358]
158 - svick[505]
138 - Martin Plante[28]
133 - mikej[339]
132 - chenyuejie[2292]
124 - adrianbanks[127]
122 - Peter Mortensen[151]
120 - Tim Post[52]
119 - daltojr[1009]
117 - magcius[1304]
114 - Josh Kelley[2287]
113 - Riduidel[2192]
110 - Martijn Laarman[56]
108 - pufferfish[1689]
104 - balpha[43]
104 - John Bristowe[70]
103 - Peter Ajtai[2618]
103 - Mark Hurd[44]
102 - jmoy[1978]
97 - JL01[1911]
95 - Maxim Zaslavsky[32]
93 - Joe[2418]
90 - Kevin[2296]
86 - Jan Fabry[1303]
86 - denny[553]
86 - Robert Cartaino[431]
85 - Roy Tang[2300]
85 - Chacha102[23]
81 - pierocampanelli[1384]
80 - James Johnson[2517]
79 - Evan[26]
79 - Fatal510[866]
76 - tsudot[2662]
74 - arinte[2616]
71 - Charles Stewart[2083]
70 - Frank Krueger[2203]
69 - Jim McKeeth[267]
67 - Chris S[1174]
67 - voyager[20]
65 - Dinis Cruz[1676]
64 - Mattias Konradsson[564]
63 - crucible[1986]
63 - ADB[705]
62 - Dan Atkinson[587]
59 - theplic[2125]
58 - Matthew Pelser[138]
56 - Alexandre Rafalovitch[2883]
56 - Artefacto[1179]
56 - Robert Love[1578]
55 - Nathan Voxland[2442]
52 - AidenMontgomery[2894]
52 - Bidhan Baruah[2735]
51 - Can Berk Güder[636]
48 - chanchal1987[2951]
48 - Cristian Castiblanco[109]
48 - scunliffe[37]
47 - Fernando[1856]
46 - DC01[2603]
45 - Matt Culbreth[2829]
43 - user1711[1711]
42 - Simon Brown[1177]
41 - Nicolas Raoul[2679]
40 - C.W.Holeman II[1942]
37 - zengr[1416]
36 - coobird[525]
36 - Bialecki[756]
36 - peter.newhook[1924]
34 - Vicky[2139]
32 - RichOrr[1749]
30 - hvgotcodes[2532]
28 - Portman[168]
27 - Heavy Bytes[939]
25 - stacker[1339]
23 - chalup[892]
21 - David[2118]
21 - Shimmy[2134]
20 - Ngu Soon Hui[216]
20 - Wei Hu[1230]
17 - Michal[1918]
14 - tonklon[2734]
11 - Alienfluid[1044]
4 - Matt Huggins[1097]

Not related to the question, but these are the requests issued to generate the above results:

#   Result  Protocol    Host    URL Body    Caching Content-Type    Process Comments    Custom  
58  200 HTTP    api.stackapps.com   /1.0/questions?pagesize=100&key=foo-bar-fu  14,939  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:7448            
59  200 HTTP    api.stackapps.com   /1.0/questions?page=2&pagesize=100&key=foo-bar-fu   14,636  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:7448            
60  200 HTTP    api.stackapps.com   /1.0/questions?page=3&pagesize=100&key=foo-bar-fu   15,181  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:7448            
61  200 HTTP    api.stackapps.com   /1.0/questions?page=4&pagesize=100&key=foo-bar-fu   14,160  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:7448            
62  200 HTTP    api.stackapps.com   /1.0/questions?page=5&pagesize=100&key=foo-bar-fu   14,352  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:7448            
63  200 HTTP    api.stackapps.com   /1.0/questions?page=6&pagesize=100&key=foo-bar-fu   3,580   private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:7448            

1

JavaSript / Soapi.JS


using Soapi.JS


Soapi.RouteFactory("api.stackapps.com", apiKey) // get all users
.Questions({ pagesize: 100 }).getPagedResponse(function(data) {

    var values = {}, keys = [];
    // add up the view count for each user
    for (var i = 0; i < data.items.length; i++) {
        var q = data.items[i];
        var u = q.owner;
        if (u) {
            if (!values[u.user_id]) {
                keys.push(u.user_id);
                values[u.user_id] = {
                    display_name: u.display_name,
                    view_count: 0
                };
            }
            values[u.user_id].view_count += q.view_count;
        }
    }

    // sort and dump
    var output = "";
    for (var i = 0; i < keys.sort(function(a, b) {
        return values[b].view_count - values[a].view_count;
    }).length; i++) {
        output += values[keys[i]].view_count + " " + values[keys[i]].display_name + "\r\n";
    }

    document.getElementById("output").innerHTML = output;

});

Output:

17354 Kevin Montrose
10669 George Edison
8855 code poet
3417 systempuntoout
3020 Farseeker
2900 lfoust
2253 Franci Penov
1800 Felix
1786 Soviut
1657 S.Mark
1604 Adam Wright
1402 Jedi Master Spooky
1383 Jeff Atwood
1293 Matt S.
1159 Edan Maor
1087 Steffen Opel
1034 carson
1024 Bill the Lizard
977 Dave DeLong
977 Mark Rushakoff
950 Shay Erlichmen
947 Nick Presta
940 Thomas McDonald
918 Dave Swersky
814 Jonathan
812 eWolf
806 swanson
782 Adam
766 Lucas Jones
726 TheHurt
705 johnwards
691 Catchwa
669 Kyle Cronin
664 Jason
638 phsr
624 codeka
602 InfinitiesLoop
588 nabeelmukhtar
568 Yacoby
502 conmulligan
437 Ricky
432 Dennis Williamson
426 radius
420 SztupY
362 Soldier.moth
359 ColinD
353 Igor Zevaka
336 Koning Baard
320 Ryan Brunner
311 jjnguy
298 Greg Bray
297 fukas78
288 Nathan Reed
287 Michael B.
283 iconiK
258 Peter Mourfield
248 RichH
244 blork
238 Casebash
237 Joel Potter
221 ripper234
214 TheKaptain
207 mlaw
207 Lucas McCoy
205 Nippysaurus
203 James A. Rosen
191 Vitaly Polonetsky
188 bpedro
180 Shane
168 Robert Munteanu
158 svick
138 Martin Plante
133 mikej
132 chenyuejie
124 adrianbanks
122 Peter Mortensen
120 Tim Post
119 daltojr
117 magcius
114 Josh Kelley
114 Riduidel
110 Martijn Laarman
108 pufferfish
106 balpha
104 John Bristowe
103 Peter Ajtai
103 Mark Hurd
102 jmoy
97 JL01
95 Maxim Zaslavsky
93 Joe
90 Kevin
86 Roy Tang
86 Robert Cartaino
86 Jan Fabry
86 denny
85 Chacha102
83 James Johnson
81 pierocampanelli
79 Evan
79 Fatal510
76 tsudot
74 arinte
71 Charles Stewart
70 Frank Krueger
69 Jim McKeeth
68 Chris S
67 voyager
65 Dinis Cruz
64 Mattias Konradsson
63 crucible
63 ADB
62 Dan Atkinson
59 theplic
58 Matthew Pelser
56 Alexandre Rafalovitch
56 Artefacto
56 Robert Love
55 chanchal1987
55 Nathan Voxland
52 AidenMontgomery
52 Bidhan Baruah
51 Can Berk Güder
48 Cristian Castiblanco
48 scunliffe
47 Fernando
46 Matt Culbreth
46 DC01
43 user1711
42 Simon Brown
41 Nicolas Raoul
40 C.W.Holeman II
38 zengr
36 Bialecki
36 peter.newhook
36 coobird
34 Vicky
32 RichOrr
30 hvgotcodes
28 Portman
27 Heavy Bytes
25 stacker
23 chalup
22 Shimmy
21 David
20 Ngu Soon Hui
20 Wei Hu
17 Michal
14 tonklon
11 Alienfluid
4 Matt Huggins

Requests generated (elapsed time 3 seconds)

#   Result  Protocol    Host    URL Body    Caching Content-Type    Process Comments    Custom  
1   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=1&jsonp=Soapi._internal._callback0  14,494  private     application/json; charset=utf-8 iexplore:7564           
2   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=2&jsonp=Soapi._internal._callback1  14,704  private     application/json; charset=utf-8 iexplore:7564           
3   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=3&jsonp=Soapi._internal._callback2  15,408  private     application/json; charset=utf-8 iexplore:7564           
4   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=4&jsonp=Soapi._internal._callback3  14,012  private     application/json; charset=utf-8 iexplore:7564           
5   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=5&jsonp=Soapi._internal._callback4  14,457  private     application/json; charset=utf-8 iexplore:7564           
6   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=6&jsonp=Soapi._internal._callback5  4,234   private     application/json; charset=utf-8 iexplore:7564           
7   200 HTTP    api.stackapps.com   /1.0/questions?key=qgAq_KfDu0KYzlNG-qaTuw&pagesize=100&page=7&jsonp=Soapi._internal._callback6  204 private     application/json; charset=utf-8 iexplore:7564           


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .