Calculate a User's Reputation

12

1

Challenge

Given a user's name (not ID, we have that challenge already), output their current reputation and their ID.

Input

Input will be a single string which is the user's username. You may assume that this user exists.

Output

Output will be two integers to either STDOUT or STDERR which will be the user's reputation and the user's ID. They must be in that specific order and can be output in any reasonable format

Specifics

  • The reputation output must not be more than 10 minutes out of date.
  • Your program may not throw any errors during normal execution
  • Standard loopholes apply (including No URL Shorteners)
  • If there is more than one user with that username, you may output the stats of any user with that username. This is also why you must output the ID.
  • The reputation to be output is the reputation of the user on PPCG, not the network total score.

The Stack Exchange API site can be found here. You can read the documentation from there.

HyperNeutrino

Posted 2017-04-19T12:38:44.613

Reputation: 26 575

Link the stack-exchange API. – Magic Octopus Urn – 2017-04-19T12:44:11.793

@carusocomputing Will do, thanks. I also need to fix something because apparently multiple users can have the same name. – HyperNeutrino – 2017-04-19T12:45:14.357

I suppose this rule applies? Also, can you confirm that it's the total reputation of the user on PPCG rather than SE?

– Arnauld – 2017-04-19T13:01:04.693

@Arnauld I will say yes. And I confirm that it's just the PPCG reputation. I will clarify that in the challenge. Thanks. – HyperNeutrino – 2017-04-19T13:04:35.977

Well I did this T-SQL entry but it doesn't meet rule 1 as it's not updated enough :(

– ʰᵈˑ – 2017-04-19T13:17:41.537

@ʰᵈˑ Oh :( That's too bad. Yeah I did that to prevent answers using the cache :P – HyperNeutrino – 2017-04-19T13:20:54.063

Is an answer valid if they output the results for a user whose name just contains the input string instead of having an exact match? – Dason – 2017-04-19T14:21:30.910

Would {"items":[{"reputation":2820,"user_id":42295}]} be a reasonable output format? – Tom – 2017-04-19T14:23:40.320

@Tom Sure. I will say yes. As long as it's obvious where the data is. – HyperNeutrino – 2017-04-19T14:24:22.657

@HyperNeutrino Any input on the exact match question I had? So let's say your input was "tom" and your answer output was for "Tom Carpenter" would that be acceptable? – Dason – 2017-04-19T14:28:11.907

Answers

1

Bash + JQ, 93 bytes

Rolled back the 87 byte version, as it was not handling multi-user responses correctly.

Golfed

curl "api.stackexchange.com/users?site=codegolf&inname=$1"|zcat|jq ..\|numbers|sed -n 4p\;12p

Will output first user id and reputation on the separate lines.

How It Works ?

1)curl + zcat are used to fetch the JSON formatted API reply

2) jq ..|numbers will unfold JSON recursively, and print all the numeric values, one per line

...
1   35
2   8
3   2
4   3315904
5   1487694154
6   1492702469
7   4565
8   82
9   82
10  60
11  20
12  6275
...

(line numbers were added with nl for illustration purposes only)

3) Next we use sed to lookup the first account_id and reputation, by their absolute row numbers

Test

>./reputation zeppelin
3315904
6275

zeppelin

Posted 2017-04-19T12:38:44.613

Reputation: 7 884

6

JavaScript (ES6), 145 139 136 125 123 117 bytes

a=>fetch(`//api.stackexchange.com/users?site=codegolf&filter=!)LgZAmQ6ls0hH&inname=`+a).then(_=>_.text()).then(alert)

Saved 6 bytes thanks to Shaggy and 6 bytes thanks to Cyoce.

I'm not sure if it should output all users with the same name, or just one of them; this code outputs all of them.

f=a=>fetch(`//api.stackexchange.com/users?site=codegolf&filter=!)LgZAmQ6ls0hH&inname=`+a).then(_=>_.text()).then(alert)

f("tom")

Tom

Posted 2017-04-19T12:38:44.613

Reputation: 3 078

Beat me to it. Save 6 bytes with "//api.stackexchange.com/users?site=codegolf&inname="+a. – Shaggy – 2017-04-19T14:16:13.260

Note: It seems you return more results than you (possibly) should. I've been trying to get HyperNeutrino's feedback on what is appropriate but your result returns values for users whose name just contains the input - not exactly matches. So your top result is for "Tom Carpenter" and not "tom". – Dason – 2017-04-19T15:11:26.517

Would it be possible to replace _=>alert(_) with just alert? – Cyoce – 2017-04-19T15:15:12.117

@Dason Yep, that's one point I wasn't sure on; I'll change my answer when it's confirmed. – Tom – 2017-04-19T15:21:21.910

@Cyoce That worked, thanks! – Tom – 2017-04-19T15:23:46.630

@Tom I think the answer to that question would impact all the current answers so far since they're all basically using the same method to get the results. – Dason – 2017-04-19T15:25:40.877

4

Python 2, 178 169 149 Bytes

I'd use requests for this:

from requests import*
a=get("http://api.stackexchange.com/users?site=codegolf&inname="+input()).json()["items"][0]
print a["reputation"],a["user_id"]

Basically, it uses stack's api to fetch the information as JSON and then gets the item "reputation". Additionally, the API featured many extra parameters, I shaved those off as well.

Generous contributions from: carusocomputing, ElPedro, Malivil, Keerthana Prabhakaran

Neil

Posted 2017-04-19T12:38:44.613

Reputation: 2 417

["items"][0] this only gets the first result, on the multiple users case it will still ignore the extras if I'm not mistaken. – Magic Octopus Urn – 2017-04-19T13:50:01.903

import requests as r and r.get(... saves 4 bytes. – ElPedro – 2017-04-19T13:57:59.157

@ElPedro updated it, thanks for the input. – Neil – 2017-04-19T14:12:14.193

It actually goes to 157 Try it online!. Note that it doesn't actually work on TryItOnline as it can't find requests but works locally for me. TIO is just somewhere to put the code :)

– ElPedro – 2017-04-19T14:16:57.283

@ElPedro Nice, I updated it again. – Neil – 2017-04-19T14:19:00.437

1If you rearrange the parameters so that it reads ?site=codegolf&inname=" you can save 3 bytes (+"") – Malivil – 2017-04-19T14:21:10.357

@Malivil Very clever, updated it. – Neil – 2017-04-19T14:23:12.147

Also it works without the 2.2/ in the url. Not sure what version of the API it is using then but it saves another 4 :) – ElPedro – 2017-04-19T14:41:36.467

@ElPedro Updated. Thanks again. – Neil – 2017-04-19T14:49:29.343

1Using from requests import*;a=get() reduces a byte! – Keerthana Prabhakaran – 2017-04-20T13:57:54.407

@KeerthanaPrabhakaran Thanks for the input, updated it. – Neil – 2017-04-20T14:25:47.817

3

Groovy, 144 156 bytes

{new groovy.json.JsonSlurper().parse(new URL("http://api.stackexchange.com/2.2/users/?site=codegolf&inname=$it")).items.collect{[it.user_id,it.reputation]}}

Anonymous closure.

EDIT: forgot to use import of groovy.json. for JSON Slurper + 14 bytes.

Example output [[UserID, Reputation],...]:

[[20260, 60695], [20469, 21465], [3103, 8856], [41805, 7783], [134, 6829], [42643, 5622], [45268, 4389], [10732, 3976], [32, 3635], [53745, 3392], [10801, 3216], [49362, 2418], [2104, 2160], [3563, 1988], [18280, 1491], [742, 1466], [59487, 1362], [19039, 1330], [56642, 1133], [9522, 951], [34438, 886], [1744, 793], [52661, 778], [18187, 768], [11426, 751], [26850, 711], [178, 637], [29451, 631], [19700, 616], [15862, 601]]

Magic Octopus Urn

Posted 2017-04-19T12:38:44.613

Reputation: 19 422

8JsonSlurper... o_O – HyperNeutrino – 2017-04-19T13:34:14.263

slurrrrrrrrrp – Magic Octopus Urn – 2017-04-19T13:34:29.383

@HyperNeutrino if you ever get a chance to look at it, I consider it my favorite JSON parsing library. It's so easy. – Magic Octopus Urn – 2017-04-20T13:53:40.993

Alright, thanks! I'll take a look at it. – HyperNeutrino – 2017-04-20T14:30:07.907

0

Swift, 225 201 bytes

import Foundation;var f:(String)->Any={return try!JSONSerialization.jsonObject(with:Data(contentsOf:URL(string:"http://api.stackexchange.com/users?site=codegolf&filter=!)LgZAmQ6ls0hH&inname=\($0)")!))}

Un-golfed:

import Foundation

var f:(String) -> [String: Any] = {
    return try! JSONSerialization.jsonObject(with:Data(contentsOf:URL(string:"http://api.stackexchange.com/users?site=codegolf&filter=!)LgZAmQ6ls0hH&inname=\($0)")!)) as! [String:Any]
}

Example output:

["items": <__NSArrayI 0x6180001ffc00>(
{
    reputation = 2820;
    "user_id" = 42295;
},
{
    reputation = 2468;
    "user_id" = 31203;
},
{
    reputation = 2106;
    "user_id" = 2800;
},
{
    reputation = 1479;
    "user_id" = 6689;
},
{
    reputation = 1287;
    "user_id" = 64424;
},
{
    reputation = 1037;
    "user_id" = 64070;
},
{
    reputation = 644;
    "user_id" = 25193;
},
{
    reputation = 641;
    "user_id" = 3171;
},
{
    reputation = 639;
    "user_id" = 743;
},
{
    reputation = 590;
    "user_id" = 33233;
},
{
    reputation = 571;
    "user_id" = 26993;
},
{
    reputation = 563;
    "user_id" = 1730;
},
{
    reputation = 321;
    "user_id" = 18570;
},
{
    reputation = 309;
    "user_id" = 39156;
},
{
    reputation = 291;
    "user_id" = 7880;
},
{
    reputation = 281;
    "user_id" = 25190;
},
{
    reputation = 261;
    "user_id" = 40820;
},
{
    reputation = 231;
    "user_id" = 14154;
},
{
    reputation = 206;
    "user_id" = 2774;
},
{
    reputation = 196;
    "user_id" = 48231;
},
{
    reputation = 181;
    "user_id" = 1230;
},
{
    reputation = 176;
    "user_id" = 64077;
},
{
    reputation = 171;
    "user_id" = 31365;
},
{
    reputation = 171;
    "user_id" = 43455;
},
{
    reputation = 163;
    "user_id" = 21469;
},
{
    reputation = 161;
    "user_id" = 11845;
},
{
    reputation = 157;
    "user_id" = 25181;
},
{
    reputation = 131;
    "user_id" = 263;
},
{
    reputation = 131;
    "user_id" = 3922;
},
{
    reputation = 128;
    "user_id" = 67227;
}
)
]

Caleb Kleveter

Posted 2017-04-19T12:38:44.613

Reputation: 647