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\$

27 Answers 27

14
\$\begingroup\$

Bash, 21 17 11 9 7 bytes

curl ai
\$\endgroup\$
  • 1
    \$\begingroup\$ curl is not a tool that already exists? :) \$\endgroup\$ – Abel Tom May 10 '17 at 17:25
  • 1
    \$\begingroup\$ @AbelTom this is "your language's API" \$\endgroup\$ – programmer5000 May 10 '17 at 17:27
  • 7
    \$\begingroup\$ curl ai no need for http:// \$\endgroup\$ – opatut May 10 '17 at 22:13
  • 1
    \$\begingroup\$ @SIGSEGV: Because that does not output to stdout. \$\endgroup\$ – Lightness Races with Monica May 11 '17 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\$ – Rɪᴋᴇʀ May 11 '17 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\$
  • 1
    \$\begingroup\$ Now to convince them to run an FTP server... \$\endgroup\$ – user253751 May 11 '17 at 9:31
  • 1
    \$\begingroup\$ How is that url valid? \$\endgroup\$ – Iulian Onofrei May 11 '17 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 '17 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 '17 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\$ – Engineer Toast May 11 '17 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\$
5
\$\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\$
  • 1
    \$\begingroup\$ You can remove the double quotes " around GET /. \$\endgroup\$ – Digital Trauma May 11 '17 at 18:31
  • \$\begingroup\$ Amazing! This is very good, even if it is not the shortest. \$\endgroup\$ – John Hathwood May 12 '17 at 20:01
4
\$\begingroup\$

Mathematica 18 Bytes

URLDownload@"v.ht"
\$\endgroup\$
  • \$\begingroup\$ I think FetchURL works \$\endgroup\$ – ngenisis May 11 '17 at 3:34
  • \$\begingroup\$ Think you mean URLFetch which requires a Mathematica version before 11. \$\endgroup\$ – Ian Miller May 11 '17 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\$ – Kelly Lowder May 11 '17 at 13:30
4
\$\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\$
  • \$\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\$ – TessellatingHeckler May 11 '17 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 '17 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 '17 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\$ – TessellatingHeckler May 11 '17 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 '17 at 15:37
4
\$\begingroup\$

C#, 96 93 bytes

async()=>Console.Write(await new System.Net.Http.HttpClient().GetStringAsync("http://3.ly"));
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Martin Ender May 11 '17 at 17:54
  • \$\begingroup\$ Thanks! I lurk occasionally but I needed some fun today c: \$\endgroup\$ – Ceshion May 11 '17 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\$
  • 5
    \$\begingroup\$ Until we reach a consensus on Promises, I'd say you'll need to alert this. \$\endgroup\$ – Shaggy May 10 '17 at 17:20
  • \$\begingroup\$ @Shaggy I think the community agrees that this way is OK, see vote counts on your answer and my meta q. \$\endgroup\$ – programmer5000 May 10 '17 at 17:23
  • 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 '17 at 17:24
  • \$\begingroup\$ Does text return a promise or does ...then(x=>alert(x.text())) work? \$\endgroup\$ – user253751 May 11 '17 at 9:32
  • 1
    \$\begingroup\$ @Shaggy it's acceptable now. The consensus is +5 and 2:1 ratio of up to downvotes. \$\endgroup\$ – Rɪᴋᴇʀ May 11 '17 at 23:31
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\$
  • 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 '17 at 20:42
  • \$\begingroup\$ @ovs Huh, thanks! \$\endgroup\$ – totallyhuman May 10 '17 at 21:48
  • \$\begingroup\$ You need to remove the trailing brackets. It would be a snippet instead oft an function otherwise \$\endgroup\$ – ovs May 11 '17 at 4:22
  • \$\begingroup\$ IOError: [Errno socket error] [Errno 8] nodename nor servname provided, or not known \$\endgroup\$ – Iulian Onofrei May 11 '17 at 10:47
  • 1
    \$\begingroup\$ @IulianOnofrei Are you using TIO? Code run in TIO doesn't have access to the internet. \$\endgroup\$ – totallyhuman May 11 '17 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\$
  • \$\begingroup\$ nc ai 80<<<GET\ / works for me for 18B \$\endgroup\$ – Kevin May 11 '17 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 '17 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 '17 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 '17 at 22:33
1
\$\begingroup\$

Racket, 71 bytes

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

HTML, 24 bytes

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

This technically fails as it renders the result...

\$\endgroup\$
  • 2
    \$\begingroup\$ Is that not STDOUT for HTML? 15 bytes: <embed src=//ai \$\endgroup\$ – Shaggy May 11 '17 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\$
  • \$\begingroup\$ Aren't shortened URLs usually disallowed? \$\endgroup\$ – Shaggy May 10 '17 at 18:38
  • 3
    \$\begingroup\$ Really, that's Python + requests, since requests is (sadly) not in the standard library. \$\endgroup\$ – L3viathan May 10 '17 at 19:33
  • \$\begingroup\$ from request import* is 2 bytes shorter \$\endgroup\$ – ovs May 10 '17 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\$ – HyperNeutrino May 11 '17 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\$
  • \$\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 '17 at 17:24
  • \$\begingroup\$ @Kevin Works on my system. Sounds like you're on FreeBSD or macOS. \$\endgroup\$ – dkudriavtsev May 11 '17 at 17:25
  • \$\begingroup\$ Yes, macOS Sierra \$\endgroup\$ – Kevin May 11 '17 at 17:26
  • \$\begingroup\$ @Kevin I'm on Linux. Try a VM. \$\endgroup\$ – dkudriavtsev May 11 '17 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 '17 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\$

Perl, 41 bytes

$ perl -MLWP::UserAgent -e 'getstore($url)'
\$\endgroup\$
  • \$\begingroup\$ Hi, welcome on Programing Puzzle & Code-golf, nice to see new Perl golfers around. You are not allowed to suppose that the url is inside a variable (can't find you a reference link about that part, sorry). Furthermore, you don't have to count perl -e'' inside your bytecount. About how to count flags & co, see this post. See you around I hope! \$\endgroup\$ – Dada May 13 '17 at 21:41
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\$
  • \$\begingroup\$ Welcome to PPCG! Pretty good, but "the url must count to byte count". \$\endgroup\$ – Jonathan Allan May 12 '17 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\$

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\$

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.