21
\$\begingroup\$

Introduction

Yes, it's as simple as that. Download any file from the internet!

Rules

You must use your language's API.

You must output the file you downloaded to STDOUT, the console, a file, etc.

Input should be empty, or the URL you wish to download, but the url must count to byte count..

Good luck!

\$\endgroup\$
0

29 Answers 29

16
\$\begingroup\$

Bash, 21 17 11 9 7 bytes

curl ai
\$\endgroup\$
10
  • 1
    \$\begingroup\$ curl is not a tool that already exists? :) \$\endgroup\$
    – Abel Tom
    May 10, 2017 at 17:25
  • 2
    \$\begingroup\$ @AbelTom this is "your language's API" \$\endgroup\$
    – user58826
    May 10, 2017 at 17:27
  • 7
    \$\begingroup\$ curl ai no need for http:// \$\endgroup\$
    – opatut
    May 10, 2017 at 22:13
  • 1
    \$\begingroup\$ @SIGSEGV: Because that does not output to stdout. \$\endgroup\$ May 11, 2017 at 21:46
  • 2
    \$\begingroup\$ This doesn't work for me? What's ai supposed to be? Gives curl: (6) Could not resolve host: ai here. \$\endgroup\$
    – Riker
    May 11, 2017 at 23:00
13
\$\begingroup\$

Röda, 29 bytes

{saveResource"http://ai","a"}

This is an anonymous function that creates a file a containing the HTML code of http://ai.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Now to convince them to run an FTP server... \$\endgroup\$
    – user253751
    May 11, 2017 at 9:31
  • 1
    \$\begingroup\$ How is that url valid? \$\endgroup\$ May 11, 2017 at 10:45
  • 1
    \$\begingroup\$ @IulianOnofrei Why wouldn't it be valid? It has a protocol, a hostname, and no port, path or query string but those are all optional. Are you asking why ai is a valid hostname? \$\endgroup\$
    – user253751
    May 11, 2017 at 11:49
  • 1
    \$\begingroup\$ @IulianOnofrei ai is a TLD. Usually TLDs don't resolve to addresses that host websites, but this one apparently does. \$\endgroup\$
    – user253751
    May 11, 2017 at 11:52
  • 4
    \$\begingroup\$ For those who - like me - the URL does not resolve, you can probably view it by using the IP address directly: 209.59.119.34 \$\endgroup\$ May 11, 2017 at 13:13
6
\$\begingroup\$

MATL, 8 bytes

'v.ht'Xi

The URL provided to Xi (urlread) is prepended with http:// if it isn't already. Additionally the output of urlread contains the contents of the response and this is implicitly printed when the program exits.

Unfortunately this does not work for the online compilers since loading data from an arbitrary URL is disallowed in online-mode, so here's a GIF. enter image description here

\$\endgroup\$
6
\$\begingroup\$

Bash, 45 43 bytes

exec 3<>/dev/tcp/ai/80;echo GET />&3;cat<&3

opens a tcp socket with ai site on file descriptor 3, issues a get, and cats the result. perhaps can be golfed further.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can remove the double quotes " around GET /. \$\endgroup\$ May 11, 2017 at 18:31
  • \$\begingroup\$ Amazing! This is very good, even if it is not the shortest. \$\endgroup\$ May 12, 2017 at 20:01
5
\$\begingroup\$

PowerShell, 7 Bytes

Since we're all using www.ai...

irm ai.

uses Invoke-RestMethod - prints the result to StdOut

alternate answer, saves to file named 'a' in run directory, using Invoke-WebRequest and -OutFile param.

iwr ai. -OutF a
\$\endgroup\$
5
  • \$\begingroup\$ I'm going to nitpick that irm won't download any file, as requested in the question, because if the server returns json or xml, it will be turned into a PSObject instead. And irm ai. doesn't work for me - it says name cannot be resolved... I get "$(irm g.cn)" as a suggestion. \$\endgroup\$ May 11, 2017 at 15:25
  • \$\begingroup\$ good point regarding irm - @John Hathwood - does this still fit the challenge or need to be changed? i'm a little unclear on 'any file' - it outputs a 'parsed' copy of any json or xml, all other files are returned raw. \$\endgroup\$
    – colsw
    May 11, 2017 at 15:30
  • \$\begingroup\$ @TessellatingHeckler what version of PS are you using? do you get anything running nslookup ai. or nslookup www.ai 8.8.8.8? \$\endgroup\$
    – colsw
    May 11, 2017 at 15:31
  • \$\begingroup\$ Aye, it works querying against Google DNS, it's a local Windows DNS server that can't resolve it. OK. PSv4 as well, but that's not involved, same at a cmd prompt. \$\endgroup\$ May 11, 2017 at 15:35
  • \$\begingroup\$ @TessellatingHeckler always something simple aha, i'll wait for the challenge author to confirm if the above is ok, but most likely the 15-byte iwr answer will be the correct one. \$\endgroup\$
    – colsw
    May 11, 2017 at 15:37
4
\$\begingroup\$

Mathematica 18 Bytes

URLDownload@"v.ht"
\$\endgroup\$
3
  • \$\begingroup\$ I think FetchURL works \$\endgroup\$
    – user61980
    May 11, 2017 at 3:34
  • \$\begingroup\$ Think you mean URLFetch which requires a Mathematica version before 11. \$\endgroup\$
    – Ian Miller
    May 11, 2017 at 12:32
  • \$\begingroup\$ URLFetch still works in the current version but at some point will probably be fully deprecated, and there did use to be a FetchURL in UtilitiesURLTools reference.wolfram.com/language/ref/URLFetch.html \$\endgroup\$ May 11, 2017 at 13:30
4
\$\begingroup\$

C#, 96 93 bytes

async()=>Console.Write(await new System.Net.Http.HttpClient().GetStringAsync("http://3.ly"));
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ May 11, 2017 at 17:54
  • \$\begingroup\$ Thanks! I lurk occasionally but I needed some fun today c: \$\endgroup\$
    – Ceshion
    May 11, 2017 at 17:55
3
\$\begingroup\$

PHP, 22 bytes

If allow_url_include=1 is in your ini file:

<?=include"http://ai";   
\$\endgroup\$
3
\$\begingroup\$

JS (ES6), 38 36 31 29 bytes

fetch`//ai`.then(x=>x.text())

Depending on the promise consensus, (41 bytes)

fetch`//ai`.then(x=>x.text()).then(alert)
\$\endgroup\$
9
  • 5
    \$\begingroup\$ Until we reach a consensus on Promises, I'd say you'll need to alert this. \$\endgroup\$
    – Shaggy
    May 10, 2017 at 17:20
  • 1
    \$\begingroup\$ Do 10 votes a consensus make? (Genuine question that; how many votes do we need for it to considered accaptable?) \$\endgroup\$
    – Shaggy
    May 10, 2017 at 17:24
  • \$\begingroup\$ Does text return a promise or does ...then(x=>alert(x.text())) work? \$\endgroup\$
    – user253751
    May 11, 2017 at 9:32
  • \$\begingroup\$ @immibis, It returns a Promise, unfortunately. \$\endgroup\$
    – Shaggy
    May 11, 2017 at 11:32
  • 1
    \$\begingroup\$ @Shaggy it's acceptable now. The consensus is +5 and 2:1 ratio of up to downvotes. \$\endgroup\$
    – Riker
    May 11, 2017 at 23:31
3
\$\begingroup\$

C (Clang, ARMv7-a Linux/Android, AT&T Wireless), 162 bytes

Since we're hardcoding URLs, why not hardcode ISPs?

main(s){int b[500]={5<<28|2,**((int***)gethostbyname("ai"))[4]};connect(s=socket(2,1,0),b,16);write(s,"GET / HTTP/1.0\n\n",16);read(s,b,-1);read(s,b,-1);puts(b);}

I can't show a demo because none of the demos have online enabled :(

This abuses two things. One is that I assume http://ai is smaller than 2000 bytes and just casually read a max of 4GB onto the stack (what could possibly go wrong), and two is that I take advantage of how I discovered that AT&T Wireless converts \n to \r\n in HTTP requests for some reason (new data saving hack?).

Termux screenshot, showing how it works on data (AT&T via Cricket) but not on my Wi-Fi (Xfinity).

The only thing AT&T is good for?

Ungolfed and commented:

note: I have removed the punning and buffer reusing for this.

#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
    // Look up the IP address of http://ai
    // Note that I mess around with type punning and the documented
    // layout of the structure in the golfed version.
    struct hostent *ent = gethostbyname("ai");
    // Extract the first IP address (already in network order)
    uint32_t ip;
    memcpy(&ip, ent->h_addr_list[0], sizeof(uint32_t));
    // Create the sockaddr struct
    struct sockaddr_in header = (struct sockaddr_in) {
        .sin_family = AF_INET,
        .sin_port = htons(80),
        .sin_addr = { ip }
    };
    // Copy over to the pun
    struct sockaddr_storage header_raw;
    memcpy(&header_raw, &header, sizeof(header));

    // Create an IPv4 socket
    int soc = socket(AF_INET, SOCK_STREAM, 0);
    // Connect the socket to the interwebs
    connect(soc, (const struct sockaddr *)&header_raw, sizeof(header_raw));

    // Send an HTTP GET request
    // Note that because It Works™ on AT&T, I use \n instead of the proper \r\n. Two newlines are still required though.
#ifdef ATT
    const char get_request[] = "GET / HTTP/1.0\n\n";
#else
    const char get_request[] = "GET / HTTP/1.0\r\n\r\n";
#endif
    write(soc, get_request, strlen(get_request));

    char buf[2000] = {0};
    // Note: I use -1 to just read the entire thing. Hopefully it is all shorter than
    // 1999 chars :P
#ifdef BUF_HACK
    size_t len = -1;
#else
    size_t len = sizeof(buf) - 1;
#endif
    // Receive the HTTP response header
    read(soc, buf, len);
    // But ignore it because I don't care about parsing HTTP stuff
    // Just read again for the real data
    read(soc, buf, len);
    // Print to stdout (assuming the HTTP header was shorter)
    puts(buf);
    // I don't actually bother closing the socket in the golfed code, sue me.
#ifndef RESOURCE_HOG
    close(soc);
#endif
}

I just decided to see how painful this was, I knew before I started that it didn't have a chance. 😏

\$\endgroup\$
2
\$\begingroup\$

R, 24 bytes

readLines('http://g.co')

prints the output to console in the usual R format -- vector of strings, one element per line of the site.

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 13 bytes

URLFetch@"ai"
\$\endgroup\$
2
\$\begingroup\$

Python 2, 55 49 47 bytes

Not shorter but I really thought I could go further.

from urllib import*
urlopen('http://ai').read
\$\endgroup\$
10
  • 2
    \$\begingroup\$ I think from urllib import*\nurlopen('http://g.co').read is a valid submission as the second line is an unnamed function (just like an unnamed lambda function) \$\endgroup\$
    – ovs
    May 10, 2017 at 20:42
  • \$\begingroup\$ You need to remove the trailing brackets. It would be a snippet instead oft an function otherwise \$\endgroup\$
    – ovs
    May 11, 2017 at 4:22
  • \$\begingroup\$ IOError: [Errno socket error] [Errno 8] nodename nor servname provided, or not known \$\endgroup\$ May 11, 2017 at 10:47
  • 1
    \$\begingroup\$ @IulianOnofrei Are you using TIO? Code run in TIO doesn't have access to the internet. \$\endgroup\$ May 11, 2017 at 10:49
  • \$\begingroup\$ I don't know what TIO is. \$\endgroup\$ May 11, 2017 at 10:49
1
\$\begingroup\$

Vim Ex command, 14 bytes

e http://3.ly/

Opens the URL as a new buffer. Netrw seriously suffers from a restrictive URL format.

\$\endgroup\$
1
\$\begingroup\$

nc -v v.ht 80 < file - 31 bytes

Where file contains:

GET / HTTP/1.1\r\n
Host: v.ht\r\n
\r\n

I profess ignorance about how I should score this. The file is 31 bytes and contains the URL I want, the -v flag value decides if I get the file I want or an error response.

\$\endgroup\$
4
  • \$\begingroup\$ nc ai 80<<<GET\ / works for me for 18B \$\endgroup\$
    – Kevin
    May 11, 2017 at 17:29
  • \$\begingroup\$ I saw other people using that but it doesn't resolve on my machine. I might be able to find a more permissive server but this one needs the Host value. I can maybe remove the last two \r\n couplets \$\endgroup\$
    – JoshRagem
    May 11, 2017 at 21:39
  • \$\begingroup\$ Odd, my machine wouldn't resolve ai for curl or wget but does for nc. try ai. (trailing dot), or 3.ly. It's an error page, but that's still within the bounds of the challenge. \$\endgroup\$
    – Kevin
    May 11, 2017 at 21:43
  • \$\begingroup\$ Hmm, if the error page is acceptable then I could send a body of just \n. I'll try some more stuff when I get home \$\endgroup\$
    – JoshRagem
    May 11, 2017 at 22:33
1
\$\begingroup\$

Racket, 71 bytes

(require net/url)(port->string(get-pure-port(string->url"http://ai")))
\$\endgroup\$
1
  • \$\begingroup\$ Don't see too many Racket answers around here. \$\endgroup\$ May 12, 2017 at 22:54
1
\$\begingroup\$

Kdb+, 34 15 bytes

KDB+ >= 3.4

 q).Q.hg`http://ai

from here.

KDB+ < 3.4

q)`:http://ai"GET / HTTP/1.0",4#"\n"
"HTTP/1.1 200 OK\r\nDate: Thu, 11 May 2017 21:45:01 GMT\r\nServer: Apache/1.3..
\$\endgroup\$
1
\$\begingroup\$

J, 33 bytes

Sadly, this only works on the J shell.

J HTTP demo

load 'web/gethttp'
gethttp 'g.co'
\$\endgroup\$
0
\$\begingroup\$

HTML, 24 bytes

<iframe src="http://ai">

This technically fails as it renders the result...

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Is that not STDOUT for HTML? 15 bytes: <embed src=//ai \$\endgroup\$
    – Shaggy
    May 11, 2017 at 15:16
0
\$\begingroup\$

Vim, 2 + 12 = 14 bytes

gf

Expects the URL http://3.ly/ as input. gf opens the current file under the cursor. Thanks to netrw, this works fine for URLs. I found this just now while trying to remember the command to open URLs in your browser (found it, it's gx).

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 62 bytes

PowerShell 5

-join[char[]][net.webclient]::new().downloaddata('http://ai.')

66 bytes for more older versions of PowerShell

-join[char[]](new-object net.webclient).downloaddata('http://ai.')
\$\endgroup\$
0
\$\begingroup\$

Python + requests, 55 53 50 bytes

from requests import*
print(get('http://ai').text)

-2 bytes by switching URLs -3 bytes thanks to ovs

\$\endgroup\$
4
  • \$\begingroup\$ Aren't shortened URLs usually disallowed? \$\endgroup\$
    – Shaggy
    May 10, 2017 at 18:38
  • 3
    \$\begingroup\$ Really, that's Python + requests, since requests is (sadly) not in the standard library. \$\endgroup\$
    – L3viathan
    May 10, 2017 at 19:33
  • \$\begingroup\$ from request import* is 2 bytes shorter \$\endgroup\$
    – ovs
    May 10, 2017 at 20:38
  • \$\begingroup\$ @Shaggy Um... Welp... pretty much every other answer used one rip. But I changed my URL to make it even shorter because apparently http://ai is a valid full URL.. \$\endgroup\$
    – hyper-neutrino
    May 11, 2017 at 12:34
0
\$\begingroup\$

AHK, 31 bytes

URLDownloadToFile,http://g.co,g

There's a built-in so it's not very exciting. Try to fashion, though, the function is fairly long.

\$\endgroup\$
0
\$\begingroup\$

Bash + wget, 7 bytes

wget ai

Downloads the page at http://ai

\$\endgroup\$
9
  • \$\begingroup\$ I get "Resolving ai... failed: nodename nor servname provided, or not known." with this, though wget ai. (with a trailing dot) works. \$\endgroup\$
    – Kevin
    May 11, 2017 at 17:24
  • \$\begingroup\$ @Kevin Works on my system. Sounds like you're on FreeBSD or macOS. \$\endgroup\$
    – anna328p
    May 11, 2017 at 17:25
  • \$\begingroup\$ Yes, macOS Sierra \$\endgroup\$
    – Kevin
    May 11, 2017 at 17:26
  • \$\begingroup\$ @Kevin I'm on Linux. Try a VM. \$\endgroup\$
    – anna328p
    May 11, 2017 at 17:26
  • \$\begingroup\$ Odd that it wouldn't work on macOS, since it should be the same code (modulo some os-specific compiler flags, which I wouldn't expect to change this) and nslookup, dig, and nc all find it fine. Oh well, not the first difference I've seen, and not worth installing a vm for. \$\endgroup\$
    – Kevin
    May 11, 2017 at 17:34
0
\$\begingroup\$

Ruby, 27 + 10 = 37 bytes

puts open('http://ai').read

+10 bytes for -ropen-uri flag (to require open-uri library)

\$\endgroup\$
0
\$\begingroup\$

Pyth, 4 + 11 = 15 bytes

jk'z

With URL http://v.ht as input. Takes an URL string as input, downloads the file, and prints its content.

Explanation:

jk'z
   z    Get the input string (URL)
  '     Download the file from the URL, return a list of lines
jk      Join the list on k which is the empty string

Note that we cannot use the function s for concatenating the list because of bytes types issues.

Slightly cheating, 2 + 11 = 13 bytes (it displays the list of the lines in the file instead of a single string for the whole file):

'z

You will need to install Pyth on your machine to test it (the online interpreter does not execute unsafe operations).

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! Pretty good, but "the url must count to byte count". \$\endgroup\$ May 12, 2017 at 10:22
0
\$\begingroup\$

C#, 76 bytes

Console.WriteLine(new System.Net.WebClient().DownloadString("http://3.ly"));
\$\endgroup\$
0
\$\begingroup\$

MATLAB, 20 bytes

urlread('http://ai')

Nothing fancy here...

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 4 bytes

.w

Beats all other answers. Takes ai as input.

\$\endgroup\$
2
  • \$\begingroup\$ > Input should be empty, or the URL you wish to download \$\endgroup\$
    – emanresu A
    Nov 27, 2022 at 2:04
  • \$\begingroup\$ @emanresuA It is the URL I wish to download. Also, see my post on Tips for golfing in 05AB1E. \$\endgroup\$
    – Makonede
    Nov 27, 2022 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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