Quick Golf: The Gold Leader

C (+sockets), 433 429 280 276 270 259 bytes

#define H"medalbot.com"
char**p,B[999],*b=B;main(f){connect(f=socket(2,1,getaddrinfo("www."H,"80",0,&p)),p[4],16);send(f,"GET http://"H"/api/v1/medals HTTP/1.1\r\nHost:"H"\r\n\r\n",69);read(f,b,998);for(f=3;f--;puts(p))b=strchr(p=strstr(++b,"_n")+9,34),*b=0;}

So it turns out that C isn't great at downloading resources from the internet and parsing them as JSON. Who knew?

This code is (naturally) super lax with error checking, so I guess if medalbot.com wanted to send malicious data they'd be able to trigger buffer overflows, etc. Also the latest code expects certain values for the constants (e.g. AF_INET = 2) which will probably be the case everywhere, but it's not guaranteed.

Here's the original code which isn't so fragile (but still isn't very robust or safe):

#include<netdb.h>
#define H"medalbot.com"
char*b,*B,d[999];struct addrinfo*p,h;main(f){h.ai_socktype=SOCK_STREAM;getaddrinfo("www."H,"80",&h,&p);f=socket(p->ai_family,p->ai_socktype,p->ai_protocol);connect(f,p->ai_addr,p->ai_addrlen);send(f,"GET http://"H"/api/v1/medals HTTP/1.1\r\nHost: "H":80\r\nConnection: close\r\n\r\n",92,0);recv(f,d,998,0);for(f=0,b=d;f<3;++f)B=strstr(b,"_n")+9,b=strchr(B,'}'),*strchr(B,'"')=0,puts(B);}

Breakdown:

                            // No imports needed whatsoever!
#define H"medalbot.com"     // Re-use the host in multiple places
char**p,                    // This is actually a "struct addrinfo*"
    B[999],                 // The download buffer (global to init with 0)
    *b=B;                   // A mutable pointer to the buffer

main(f){
    // Hope for the best: try the first suggested address with no fallback:
    // (medalbot.com runs on Heroku which has dynamic IPs, so we must look up the
    // IP each time using getaddrinfo)
    f=socket(2,1,getaddrinfo("www."H,"80",0,&p));
                            // 2 = AF_INET
                            // 1 = SOCK_STREAM
                            //     (may not match getaddrinfo, but works anyway)
                            // 0 = IP protocol (getaddrinfo returns 0 on success)
    connect(f,p[4],16);     // struct addrinfo contains a "struct sockaddr" pointer
                            // which is aligned at 32 bytes (4*8)

    // Send the HTTP request (not quite standard, but works. 69 bytes long)
    send(f,"GET http://"H"/api/v1/medals HTTP/1.1\r\nHost:"H"\r\n\r\n",69);
    // (omit flags arg in send and hope 0 will be assumed)

    read(f,b,998);          // Get first 998 bytes of response; same as recv(...,0)

    // Loop through the top 3 & print country names:
    // (p is re-used as a char* now)
    for(f=3;f--;puts(p))        // Loop and print:
        p=strstr(++b,"_n")+9,   //  Find "country_name": "
        b=strchr(p,34),         //  Jump to closing "
        *b=0;                   //  Set the closing " to \0
}

This isn't very nice for the server since we don't send Connection: close\r\n as part of the HTTP request. It also omits the Accept header since medalbot.com doesn't seem to be using compression in any case, and misses the space after Host: (again, the server seems to be OK with this). It doesn't seem as though anything else can be removed though.


Once the olympics end, the most likely behaviour for this program is to segfault trying to read memory location 9. Unless an evil hacker takes over the domain, in which case the most likely behaviour is for it to set some byte to 0 in the address info structs, which probably isn't too dangerous actually. But who can tell with these evil hackers?


PowerShell v4+, 88 69 bytes

(ConvertFrom-Json(iwr medalbot.com/api/v1/medals))[0..2].country_name

Uses iwr (the alias for Invoke-WebRequest) to grab the API. We feed that as the input parameter to the ConvertFrom-Json built-in that pulls the JSON text into a custom object array. We encapsulate that object array in parens, take the first three elements [0..2], and take the .country_name of each thereof.

Requires at least v4+ for the multiple-object-properties, else we'd need to use something like |Select "country_name" instead. Requires at least v3+ for the ConvertFrom-Json built-in.

PS C:\Tools\Scripts\golfing> .\olympics-gold-leader.ps1
United States
Great Britain
China

bash + w3m + grep + cut, 65 59 58 54 bytes

w3m medalbot.com/api/v1/medals|grep -m3 m|cut -d\" -f4
  • 6 bytes less thanks to @Joe's suggestions.
  • 1 byte less thanks to @YOU.
  • 4 bytes less thanks to @manatwork's suggestion that the medalbot API seems to work without www. subdomain too.