10
\$\begingroup\$

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.

\$\endgroup\$
10
  • \$\begingroup\$ Link the stack-exchange API. \$\endgroup\$ Apr 19, 2017 at 12:44
  • \$\begingroup\$ @carusocomputing Will do, thanks. I also need to fix something because apparently multiple users can have the same name. \$\endgroup\$
    – hyper-neutrino
    Apr 19, 2017 at 12:45
  • \$\begingroup\$ I suppose this rule applies? Also, can you confirm that it's the total reputation of the user on PPCG rather than SE? \$\endgroup\$
    – Arnauld
    Apr 19, 2017 at 13:01
  • \$\begingroup\$ @Arnauld I will say yes. And I confirm that it's just the PPCG reputation. I will clarify that in the challenge. Thanks. \$\endgroup\$
    – hyper-neutrino
    Apr 19, 2017 at 13:04
  • \$\begingroup\$ Well I did this T-SQL entry but it doesn't meet rule 1 as it's not updated enough :( \$\endgroup\$
    – ʰᵈˑ
    Apr 19, 2017 at 13:17

6 Answers 6

6
\$\begingroup\$

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")

\$\endgroup\$
6
  • \$\begingroup\$ Beat me to it. Save 6 bytes with "//api.stackexchange.com/users?site=codegolf&inname="+a. \$\endgroup\$
    – Shaggy
    Apr 19, 2017 at 14:16
  • \$\begingroup\$ 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". \$\endgroup\$
    – Dason
    Apr 19, 2017 at 15:11
  • \$\begingroup\$ Would it be possible to replace _=>alert(_) with just alert? \$\endgroup\$
    – Cyoce
    Apr 19, 2017 at 15:15
  • \$\begingroup\$ @Dason Yep, that's one point I wasn't sure on; I'll change my answer when it's confirmed. \$\endgroup\$
    – Tom
    Apr 19, 2017 at 15:21
  • \$\begingroup\$ @Cyoce That worked, thanks! \$\endgroup\$
    – Tom
    Apr 19, 2017 at 15:23
4
\$\begingroup\$

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]]
\$\endgroup\$
4
  • 8
    \$\begingroup\$ JsonSlurper... o_O \$\endgroup\$
    – hyper-neutrino
    Apr 19, 2017 at 13:34
  • \$\begingroup\$ slurrrrrrrrrp \$\endgroup\$ Apr 19, 2017 at 13:34
  • \$\begingroup\$ @HyperNeutrino if you ever get a chance to look at it, I consider it my favorite JSON parsing library. It's so easy. \$\endgroup\$ Apr 20, 2017 at 13:53
  • \$\begingroup\$ Alright, thanks! I'll take a look at it. \$\endgroup\$
    – hyper-neutrino
    Apr 20, 2017 at 14:30
4
\$\begingroup\$

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

\$\endgroup\$
11
  • \$\begingroup\$ ["items"][0] this only gets the first result, on the multiple users case it will still ignore the extras if I'm not mistaken. \$\endgroup\$ Apr 19, 2017 at 13:50
  • \$\begingroup\$ import requests as r and r.get(... saves 4 bytes. \$\endgroup\$
    – ElPedro
    Apr 19, 2017 at 13:57
  • \$\begingroup\$ @ElPedro updated it, thanks for the input. \$\endgroup\$
    – Neil
    Apr 19, 2017 at 14:12
  • 1
    \$\begingroup\$ If you rearrange the parameters so that it reads ?site=codegolf&inname=" you can save 3 bytes (+"") \$\endgroup\$
    – Malivil
    Apr 19, 2017 at 14:21
  • 1
    \$\begingroup\$ Using from requests import*;a=get() reduces a byte! \$\endgroup\$ Apr 20, 2017 at 13:57
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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;
}
)
]
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 58 bytes

’¸¸.‚‹º.ŒŒ/†š?€¼=ƒËŠˆ&€†‚ˆ=ÿ’.wD„n"¡1è¦',¡н,'‡¾¡2覦',¡н,

Beats all other answers.

’...’.wD„n"¡1è¦',¡н,'‡¾¡2覦',¡н,  # trimmed program
                   ,               # output...
                  н                # first element of...
             è                     # element at 0-based index...
            1                      # literal...
             è                     # in...
     .wD                           # content of response to request to URL...
                                   # (implicit) "http://" concatenated with...
’...’                              # "api.stackexchange.com/users?site=codegolf&inname=ÿ"...
                                   # (implicit) with ÿ replaced by...
                                   # implicit input...
           ¡                       # split by...
        „n"                        # literal
              ¦                    # excluding the first character...
                 ¡                 # split by...
               ',                  # literal
                                ,  # output...
                               н   # first element of...
                         è         # element at 0-based index...
                        2          # literal...
                         è         # in...
     .w                            # content of response to request to URL...
                                   # (implicit) "http://" concatenated with...
’...’                              # "api.stackexchange.com/users?site=codegolf&inname=ÿ"...
                                   # (implicit) with ÿ replaced by...
                                   # implicit input...
                       ¡           # split by...
                    '‡¾            # "id"...
                          ¦        # excluding the first character...
                           ¦       # excluding the first character...
                              ¡    # split by...
                            ',     # literal
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.