Download a file from the Internet

21

2

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!

John Hathwood

Posted 2017-05-10T17:04:34.573

Reputation: 283

Answers

14

Bash, 21 17 11 9 7 bytes

curl ai

programmer5000

Posted 2017-05-10T17:04:34.573

Reputation: 7 828

1curl is not a tool that already exists? :) – Abel Tom – 2017-05-10T17:25:57.240

1@AbelTom this is "your language's API" – programmer5000 – 2017-05-10T17:27:53.310

7curl ai no need for http:// – opatut – 2017-05-10T22:13:38.470

could you please explain what curl 3.ly does? – bolov – 2017-05-11T11:10:23.627

@bolov this uses the command line program curl to download the page at http://3.ly – jaapz – 2017-05-11T11:42:08.083

Why not wget ai? – Matthew Roh – 2017-05-11T13:56:37.137

1@SIGSEGV: Because that does not output to stdout. – Lightness Races with Monica – 2017-05-11T21:46:05.673

2This doesn't work for me? What's ai supposed to be? Gives curl: (6) Could not resolve host: ai here. – Rɪᴋᴇʀ – 2017-05-11T23:00:08.517

@Riker It's the ccTLD for Anguilla. curl ai gave me http://209.59.119.34

– martin – 2017-05-12T02:41:26.633

@martin yea, it only works on some machines. It's confirmed to work on some linux distros, but not mac and not all windows machines. – Rɪᴋᴇʀ – 2017-05-12T03:43:35.697

13

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.

fergusq

Posted 2017-05-10T17:04:34.573

Reputation: 4 867

1Now to convince them to run an FTP server... – user253751 – 2017-05-11T09:31:38.493

1How is that url valid? – Iulian Onofrei – 2017-05-11T10:45:06.060

1@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? – user253751 – 2017-05-11T11:49:35.430

@immibis Isn't the TLD required? – Iulian Onofrei – 2017-05-11T11:50:44.437

1@IulianOnofrei ai *is* a TLD. Usually TLDs don't resolve to addresses that host websites, but this one apparently does. – user253751 – 2017-05-11T11:52:52.637

@immibis, So ai is the hostname and the TLD at the same time? What do you mean by "this one apparently does"? Do you mean that you can access it in a browser? – Iulian Onofrei – 2017-05-11T11:54:15.803

Let us continue this discussion in chat.

– user253751 – 2017-05-11T11:55:35.357

4

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

– Engineer Toast – 2017-05-11T13:13:11.763

Apparently this URL requires a specific DNS server. Which one are you using? – Andrea Lazzarotto – 2017-05-12T00:12:13.813

6

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

Suever

Posted 2017-05-10T17:04:34.573

Reputation: 10 257

6

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.

marcosm

Posted 2017-05-10T17:04:34.573

Reputation: 986

1You can remove the double quotes " around GET /. – Digital Trauma – 2017-05-11T18:31:03.577

Amazing! This is very good, even if it is not the shortest. – John Hathwood – 2017-05-12T20:01:14.413

4

Mathematica 18 Bytes

URLDownload@"v.ht"

Kelly Lowder

Posted 2017-05-10T17:04:34.573

Reputation: 3 225

I think FetchURL works – ngenisis – 2017-05-11T03:34:49.677

Think you mean URLFetch which requires a Mathematica version before 11. – Ian Miller – 2017-05-11T12:32:40.403

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 https://reference.wolfram.com/language/ref/URLFetch.html

– Kelly Lowder – 2017-05-11T13:30:27.047

4

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

colsw

Posted 2017-05-10T17:04:34.573

Reputation: 3 195

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. – TessellatingHeckler – 2017-05-11T15:25:26.437

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. – colsw – 2017-05-11T15:30:00.553

@TessellatingHeckler what version of PS are you using? do you get anything running nslookup ai. or nslookup www.ai 8.8.8.8? – colsw – 2017-05-11T15:31:48.060

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. – TessellatingHeckler – 2017-05-11T15:35:08.790

@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. – colsw – 2017-05-11T15:37:30.500

4

C#, 96 93 bytes

async()=>Console.Write(await new System.Net.Http.HttpClient().GetStringAsync("http://3.ly"));

Ceshion

Posted 2017-05-10T17:04:34.573

Reputation: 201

Welcome to PPCG! – Martin Ender – 2017-05-11T17:54:50.137

Thanks! I lurk occasionally but I needed some fun today c: – Ceshion – 2017-05-11T17:55:47.527

3

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)

programmer5000

Posted 2017-05-10T17:04:34.573

Reputation: 7 828

5Until we reach a consensus on Promises, I'd say you'll need to alert this. – Shaggy – 2017-05-10T17:20:03.173

@Shaggy I think the community agrees that this way is OK, see vote counts on your answer and my meta q. – programmer5000 – 2017-05-10T17:23:07.097

1Do 10 votes a consensus make? (Genuine question that; how many votes do we need for it to considered accaptable?) – Shaggy – 2017-05-10T17:24:19.847

Does text return a promise or does ...then(x=>alert(x.text())) work? – user253751 – 2017-05-11T09:32:26.843

@immibis, It returns a Promise, unfortunately. – Shaggy – 2017-05-11T11:32:24.750

@programmer5000, shouldn't the first version be a function (+3 bytes) in order to return the promise? – Shaggy – 2017-05-11T11:33:38.163

1@Shaggy it's acceptable now. The consensus is +5 and 2:1 ratio of up to downvotes. – Rɪᴋᴇʀ – 2017-05-11T23:31:57.393

Do you have a source for that, @Riker? – Shaggy – 2017-05-11T23:33:46.807

@Shaggy it used to be in the meta itself, it was accidentally edited out. Martin has confirmed in chat a couple times that's what it is. (iirc the loopholes post is the same) – Rɪᴋᴇʀ – 2017-05-11T23:34:39.420

Excellent. Thanks for the info, @Riker. – Shaggy – 2017-05-11T23:35:41.800

3

PHP, 22 bytes

If allow_url_include=1 is in your ini file:

<?=include"http://ai";   

Jared Chmielecki

Posted 2017-05-10T17:04:34.573

Reputation: 149

2

Python 2, 55 49 47 bytes

Not shorter but I really thought I could go further.

from urllib import*
urlopen('http://ai').read

totallyhuman

Posted 2017-05-10T17:04:34.573

Reputation: 15 378

2I 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) – ovs – 2017-05-10T20:42:33.430

@ovs Huh, thanks! – totallyhuman – 2017-05-10T21:48:13.333

You need to remove the trailing brackets. It would be a snippet instead oft an function otherwise – ovs – 2017-05-11T04:22:19.923

IOError: [Errno socket error] [Errno 8] nodename nor servname provided, or not known – Iulian Onofrei – 2017-05-11T10:47:23.367

1@IulianOnofrei Are you using TIO? Code run in TIO doesn't have access to the internet. – totallyhuman – 2017-05-11T10:49:20.097

I don't know what TIO is. – Iulian Onofrei – 2017-05-11T10:49:58.260

Can you access http://ai in a regular browser?

– totallyhuman – 2017-05-11T10:51:43.667

@totallyhuman What is http://ai ? Is this a valid address? I cannot open it in the browser. – bolov – 2017-05-11T11:10:13.460

It works for me. Might be restricted..? – totallyhuman – 2017-05-11T11:13:02.830

@totallyhuman nvm. Found this: http://stackoverflow.com/questions/26152106/why-do-these-urls-without-a-tld-resolve-to-a-webpage and I could access it from the phone.

– bolov – 2017-05-11T11:16:56.980

May be helpful: https://codegolf.stackexchange.com/questions/54356/golfing-with-user-agents

– Beta Decay – 2017-05-11T11:46:58.400

2

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.

Giuseppe

Posted 2017-05-10T17:04:34.573

Reputation: 21 077

2

Mathematica, 13 bytes

URLFetch@"ai"

alephalpha

Posted 2017-05-10T17:04:34.573

Reputation: 23 988

1

Vim Ex command, 14 bytes

e http://3.ly/

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

Brian McCutchon

Posted 2017-05-10T17:04:34.573

Reputation: 503

1

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.

JoshRagem

Posted 2017-05-10T17:04:34.573

Reputation: 189

nc ai 80<<<GET\ / works for me for 18B – Kevin – 2017-05-11T17:29:23.827

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 – JoshRagem – 2017-05-11T21:39:50.720

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. – Kevin – 2017-05-11T21:43:47.113

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 – JoshRagem – 2017-05-11T22:33:54.000

1

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..

streetster

Posted 2017-05-10T17:04:34.573

Reputation: 3 635

1

Racket, 71 bytes

(require net/url)(port->string(get-pure-port(string->url"http://ai")))

rnso

Posted 2017-05-10T17:04:34.573

Reputation: 1 635

Don't see too many Racket answers around here. – David Conrad – 2017-05-12T22:54:41.217

0

Python + requests, 55 53 50 bytes

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

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

HyperNeutrino

Posted 2017-05-10T17:04:34.573

Reputation: 26 575

Aren't shortened URLs usually disallowed? – Shaggy – 2017-05-10T18:38:15.880

3Really, that's Python + requests, since requests is (sadly) not in the standard library. – L3viathan – 2017-05-10T19:33:41.647

from request import* is 2 bytes shorter – ovs – 2017-05-10T20:38:28.357

@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.. – HyperNeutrino – 2017-05-11T12:34:48.863

0

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

Brian McCutchon

Posted 2017-05-10T17:04:34.573

Reputation: 503

0

HTML, 24 bytes

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

This technically fails as it renders the result...

Jared Chmielecki

Posted 2017-05-10T17:04:34.573

Reputation: 149

2Is that not STDOUT for HTML? 15 bytes: <embed src=//ai – Shaggy – 2017-05-11T15:16:41.517

0

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.')

Andrei Odegov

Posted 2017-05-10T17:04:34.573

Reputation: 939

0

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.

Engineer Toast

Posted 2017-05-10T17:04:34.573

Reputation: 5 769

0

Bash + wget, 7 bytes

wget ai

Downloads the page at http://ai

dkudriavtsev

Posted 2017-05-10T17:04:34.573

Reputation: 5 781

I get "Resolving ai... failed: nodename nor servname provided, or not known." with this, though wget ai. (with a trailing dot) works. – Kevin – 2017-05-11T17:24:32.543

@Kevin Works on my system. Sounds like you're on FreeBSD or macOS. – dkudriavtsev – 2017-05-11T17:25:37.387

Yes, macOS Sierra – Kevin – 2017-05-11T17:26:19.590

@Kevin I'm on Linux. Try a VM. – dkudriavtsev – 2017-05-11T17:26:47.383

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. – Kevin – 2017-05-11T17:34:00.810

@Kevin I think DNS resolution works slightly differently on BSD systems – dkudriavtsev – 2017-05-11T17:34:45.287

w3m ai is probably valid too – Digital Trauma – 2017-05-11T18:34:00.403

@DigitalTrauma What is w2m? – dkudriavtsev – 2017-05-11T18:34:23.810

@Mendeleev https://en.wikipedia.org/wiki/W3m

– Digital Trauma – 2017-05-11T19:06:03.757

0

Ruby, 27 + 10 = 37 bytes

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

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

dkudriavtsev

Posted 2017-05-10T17:04:34.573

Reputation: 5 781

0

Perl, 41 bytes

$ perl -MLWP::UserAgent -e 'getstore($url)'

Chankey Pathak

Posted 2017-05-10T17:04:34.573

Reputation: 101

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!

– Dada – 2017-05-13T21:41:41.977

0

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

Jim

Posted 2017-05-10T17:04:34.573

Reputation: 1 442

Welcome to PPCG! Pretty good, but "the url must count to byte count". – Jonathan Allan – 2017-05-12T10:22:34.283

0

C#, 76 bytes

Console.WriteLine(new System.Net.WebClient().DownloadString("http://3.ly"));

Ark-kun

Posted 2017-05-10T17:04:34.573

Reputation: 101

0

MATLAB, 20 bytes

urlread('http://ai')

Nothing fancy here...

DimP

Posted 2017-05-10T17:04:34.573

Reputation: 251