TransWikia.com

What's my PPCG ID?

Code Golf Asked by musicman523 on December 3, 2020

Challenge

Given the name of a PPCG member, output their PPCG ID number. If the user does not exist, you may report an error or return any non-positive number. If there are multiple members with this name, you may choose to output only one ID or all of them.

Test Cases

"musicman523" -> 69054
"Dennis" -> 12012
"xnor" -> 20260
"Leaky Nun" -> 48934
"fəˈnɛtɪk" -> 64505
"Jörg Hülsermann" -> 59107
"Community" -> -1
"Any user that does not exist" -> 0
"Alex" -> 69198 (this is one possible result)
"Leaky N" -> 0
"Jorge" -> 3716

8 Answers

APL (Dyalog Unicode), 169 bytes

a←⍞
y←(⎕JSON((⎕SE.SALT.New'HttpCommand').Get'https://api.stackexchange.com/users?site=codegolf&inname=',('s+'⎕R'%20')a).Data).items
{a≡1⊃y.display_name:y.user_id ⋄ 0}1

This took forever to figure out, lol.

Thanks to @Adám for saving me a lot of time, and a lot of bytes on the HTTP request.

Explanation

a←⍞                                 ⍝ Store string input
y←        ('s+'⎕R'%20')a          ⍝ Convert spaces to '%20' for query
  (⎕JSON((⎕SE.SALT.New'HttpCommand').Get'https://api.stackexchange.com/users?site=codegolf&inname=',).Data).items
                                    ⍝ Get and parse JSON from API call using ⎕SE (session namespace)
 a≡⊃y.display_name                  ⍝ Is the input equal to the unwrapped user id? 
                                    ⍝ (display name comes as an array of strings)
{                  :y.user_id ⋄ 0}  ⍝ If so, print the user id
                                    ⍝ Else print zero
                                    ⍝ Call function with placeholder arg 1

Answered by Razetime on December 3, 2020

JavaScript, 128 119 bytes

-9 bytes thanks to Rogem.

n=>fetch("198.252.206.16/users?site=codegolf&inname="+n).then(r=>r.text()).then(t=>t.match(`\/([^\/]*)\/`+n+`"`)[1])

Answered by Oliver on December 3, 2020

JavaScript (ES6) + HTML, 154 152 151 202 179 161 145 bytes

Sacrificed a few bytes to handle special characters.

Needs to be run under the api.stackexchange.com domain. Returns a Promise containing the ID or Throws an error in the Promise if the username can't be found.

s=>fetch(`/users?site=codegolf&inname=`+s).then(r=>r.json()).then(j=>j.items.find(i=>(o.innerHTML=i.display_name,o.innerText==s)).user_id)

<a id=o

Note: This solution was developed independently of Uriel's and its comments; if Uriel decides to use the find method, I'm happy to roll back to my longer, recursive version.

Answered by Shaggy on December 3, 2020

Stack Exchange Data Explorer, 56 54 53 51 46 bytes

-1 byte thanks to Hyper Neutrino. -5 bytes thanks to Giacomo Garabello.

SELECT ID FROM USERS WHERE##S##=DISPLAYNAME--S

Try it online!

Not sure if this is completely valid but... Input must be surrounded in single quotes '.

Also, I still don't get why SQL programmers like to shout but it's apparently good practise so... SELECT EVERYTHING FROM EVERYTHING WHERE EVERYTHING LIKE EVERYTHING!

Explanation

LET ME EXPLAIN.

SELECT ID FROM USERS WHERE##S##=DISPLAYNAME--S

                                           --S  -- DECLARE AN INPUT PARAMETER NAMED S
SELECT                                          -- FIND...
       ID                                       -- ID OF THE USERS...
          FROM USERS                            -- IN THE TABLE USERS...
                     WHERE                      -- THAT SATISFIES THE CONDITION...
                          ##S##=DISPLAYNAME     -- S EQUALS THE USERS' DISPLAY NAME

Answered by totallyhuman on December 3, 2020

JavaScript, 155 149 142 135 bytes

i=>fetch("//api.stackexchange.com/users?site=codegolf&inname="+i).then(r=>r.json()).then(r=>r.items.find(u=>u.display_name==i).user_id)

f=i=>fetch("//api.stackexchange.com/users?site=codegolf&inname="+i).then(r=>r.json()).then(r=>r.items.find(u=>u.display_name==i).user_id)
<input onchange="f(this.value).then(console.log)"><br>Fill input and press Enter

Answered by Uriel on December 3, 2020

Python 2 + requests, 173 bytes

lambda s:[i['user_id']for i in get('http://api.stackexchange.com/users?inname=%s&site=codegolf'%utils.quote(s)).json()['items']if i['display_name']==s]
from requests import*

Sample run

>>> f=
... lambda s:[i['user_id']for i in get('http://api.stackexchange.com/users?inname=%s&site=codegolf'%utils.quote(s)).json()['items']if i['display_name']==s]
>>> from requests import*
>>>
>>> tests = ['musicman523', 'Dennis', 'xnor', 'Leaky Nun', 'Community', 'Any user that does not exist', 'Alex', 'Leaky N', 'Jorge']
>>> for i in tests: print '%-30r -> %s'%(i, f(i))
... 
'musicman523'                  -> [69054]
'Dennis'                       -> [12012, 13891, 14912, 24937]
'xnor'                         -> [20260]
'Leaky Nun'                    -> [48934]
'Community'                    -> [-1]
'Any user that does not exist' -> []
'Alex'                         -> [21536, 69198, 11192]
'Leaky N'                      -> []
'Jorge'                        -> [3716]

Fun fact: the result is sorted by reputation, highest first.

Answered by totallyhuman on December 3, 2020

Python 2 + requests, 187 bytes

from requests import*
def f(x):t=get("http://api.stackexchange.com/users?inname="+utils.quote(x)+"&site=codegolf").json()["items"];print[k['user_id']for k in t if k['display_name']==x][0]

Returns the user ID if a single user exists, the first user which matches the requirements if more exist, and reports an error otherwise.

Answered by Mr. Xcoder on December 3, 2020

Python 3 + requests, 196 bytes

Thanks @Wondercricket for -6 bytes!

from requests import*
x=lambda s:all([print(a['user_id'])if s==a['display_name']else''for a in get('http://api.stackexchange.com/users?inname=%s&site=codegolf'%utils.quote(s)).json()['items']])and d

Uses Stack Exchange API. Fixed the Leaky N and Jorge errors.

If there are multiple users with the same name, it prints all of them, which is allowed.

Answered by Oliver Ni on December 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP