8
\$\begingroup\$

Inspired by this Youtube Video from Tom Scott.

Write a program or function that prints the current amount of views this question has.

That's it.

Your program may only access the stack exchange api, any other websites are disallowed.

Output

An integer with the exact amount of views this question has at the moment. Outputs with the k for thousand are disallowed.

\$\endgroup\$
9
  • \$\begingroup\$ It's eventually going to pop the maximum value of the views buffer. By the time we reach that this challenge is going to be extremely trivial to solve. \$\endgroup\$
    – user92069
    Apr 16 '20 at 13:40
  • \$\begingroup\$ is directly fetching the stack exchange website allowed? (codegolf.stackexchange.com/questions/203590/…) \$\endgroup\$ Apr 16 '20 at 13:55
  • \$\begingroup\$ @CommandMaster Sure. \$\endgroup\$
    – izlin
    Apr 16 '20 at 14:00
  • \$\begingroup\$ @petStorm yeah but the challenge remains the same. Even if the amount of views stays the same you can't hardcode the value in \$\endgroup\$ Apr 16 '20 at 15:08
  • \$\begingroup\$ This question is half of this closed question which was closed for being a dupe of a question asking only for the score of the question. The answers to those questions seem very similar to the ones here. For that reason I've closed this for now. \$\endgroup\$ Apr 16 '20 at 16:29
5
\$\begingroup\$

05AB1E, 46 42 36 bytes

Reading from the question-URL: 36 bytes

’ƒËŠˆ.‚‹º.ŒŒ/q/’žYì•3X&•«.w'Ž‡¡1èþ¨

-6 bytes thanks to @CommandMaster, by reading from this question's URL instead of the API

No direct TIO, since the .w builtin (to access a website) is disabled on TIO. But:

’ƒËŠˆ.‚‹º.ŒŒ/q/’žYì•3X&•«

Try this part online to see the generation of the URL.

'Ž‡¡1èþ¨

Try this part online to see the extraction of the amount of views from the HTML content. (Input doesn't contain the entire HTML content, because the TIO URL would extend 65k+ characters and be too big to post, haha.. xD)

Reading from the API: 46 42 bytes

•3X&•’¸¸.‚‹º.ŒŒ/…é/ÿ?€¼=ƒËŠˆ’žYì.w'š‹¡Åsþ

No direct TIO, since the .w builtin (to access a website) is disabled on TIO. But:

•3X&•’¸¸.‚‹º.ŒŒ/…é/ÿ?€¼=ƒËŠˆ’žYì

Try this part online to see the generation of the API-URL.

'š‹¡Åsþ

Try this part online to see the extraction of the amount of views from the JSON.

Explanation:

’ƒËŠˆ.‚‹º.ŒŒ/q/’
              # Push dictionary string "codegolf.stackexchange.com/q/"
 žY           # Push builtin "https://"
   ì          # And prepend it in front of the url
    •3X&•     # Push compressed integer 203590 (the id of this question)
         «    # Append it to the url
          .w  # Access this URL and read its contents
'Ž‡          '# Push dictionary string "wed"
   ¡          # Split the HTML on this word
    1è        # Get the second item (at index 1)
              #  (i.e. ` 119 times">\n                    <span class="fc-light mr2">Vie`)
      þ       # Only leave the digits (i.e. 1992)
       ¨      # And remove the last digit (2), that was part of the class
              # (after which it is output implicitly as result)

•3X&•         # Push compressed integer 203590 (the id of this question)
 ’¸¸.‚‹º.ŒŒ/…é/ÿ?€¼=ƒËŠˆ’
              # Push dictionary string "api.stackexchange.com/questions/ÿ?site=codegolf",
              # where the `ÿ` is automatically filled with this integer
  žY          # Push builtin "https://"
    ì         # And prepend it in front of the url
     .w       # Access this URL and read its contents
'š‹          '# Push dictionary string "count"
   ¡          # Split the JSON on this word:
              #  (i.e. `...,"view_count":14,"answer_count":2,...` will be split to
              #   [`...,"view_`, `":14,"answer_`, `":2,...`])
    Ås        # Pop and only leave the middle item (i.e. `":14,"answer_`)
      þ       # Pop and only leave its digits (i.e. 14)
              # (after which it is output implicitly as result)

See this 05AB1E tip of mine (sections How to use the dictionary? and How to compress large integers?) to understand why ’ƒËŠˆ.‚‹º.ŒŒ/q/’ is "codegolf.stackexchange.com/q/"; •3X&• is 203590; 'Ž‡ is "wed"; ’¸¸.‚‹º.ŒŒ/…é/ÿ?€¼=ƒËŠˆ’ is "api.stackexchange.com/questions/ÿ?site=codegolf"; and 'š‹ is "count".

\$\endgroup\$
8
  • \$\begingroup\$ Does it actually need the https:// schema on the url with .w? Don't have 05AB1E installed on this machine \$\endgroup\$ Apr 16 '20 at 14:03
  • \$\begingroup\$ Is there anything I need to configure to run it locally? when I try to run it the .w returns the string unchanged \$\endgroup\$ Apr 16 '20 at 14:07
  • \$\begingroup\$ @ExpiredData I think it indeed does need it, although I'm not sure. I've only installed 05AB1E locally for the first time a month ago when TIO was having issues for 2 days straight. Without the https:// it doesn't return anything, but it seems it also doesn't seem to work with for some reason.. :/ If I access a website like google (i.e. "www.google.com".w) it reads it HTML-contents just fine, but if I use "https://api.stackexchange.com/questions/203590?site=codegolf"=.w, it seems there is some kind of encoding issue.. :S Here is the output. \$\endgroup\$ Apr 16 '20 at 14:53
  • \$\begingroup\$ Unfortunately I only ever programmed once in Elixir, so I'm not entirely sure why normal HTML websites are read fine, but that JSON api url is having encoding issues.. In the get_url method it uses HTTPoison.get!(url).body to get the website contents (and the url is already casted to a string earlier). \$\endgroup\$ Apr 16 '20 at 14:54
  • 1
    \$\begingroup\$ @KevinCruijssen Thanks for clarifying \$\endgroup\$
    – Luis Mendo
    Apr 16 '20 at 16:44
2
\$\begingroup\$

Google Sheets, 99 95 bytes

=Mid(Index(ImportData("https://api.stackexchange.com/questions/203590?site=codegolf"),10),12,99

Sheets will add the trailing parentheses automatically.

ImportData will treat the result as a CSV and split the text into columns.
Index(~,10) pulls the 10th entry from that split result.
Mid(~,12,99) starts at the 12th character and pulls up to the next 99.

So long as the view count doesn't get to 10^99, this will work fine.

Here's a screenshot showing the three steps:

Screenshot

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

JavaScript (browser), 129 121 129 117 bytes

-12 bytes thanks to Kevin Cruijssen, Command Master!

await fetch`//api.stackexchange.com/questions/203590?site=codegolf`.then(x=>x.json()).then(x=>x.items[0].view_count)

Another version, not using await (136 128 136 bytes):

fetch('https://api.stackexchange.com/2.2/questions/203590?site=codegolf').then(x=>x.json()).then(x=>console.log(x.items[0].view_count))
\$\endgroup\$
9
  • \$\begingroup\$ I think you can save bytes by removing the 2.2/ in the API-URL. \$\endgroup\$ Apr 16 '20 at 13:12
  • \$\begingroup\$ You can also save 3 bytes by replacing https:// with http: (note this won't work in chrome or most other browsers if you loaded the page via https in the first place) \$\endgroup\$ Apr 16 '20 at 13:52
  • \$\begingroup\$ 106 bytes - (await(await fetch`http:api.stackexchange.com/questions/203590?site=codegolf`).json()).items[0].view_count \$\endgroup\$ Apr 16 '20 at 13:56
  • 1
    \$\begingroup\$ You can drop the https:, and only use `//api...``` \$\endgroup\$ Apr 16 '20 at 15:53
  • 1
    \$\begingroup\$ if you can assume it's run from a specific domain you can (await(await fetch`/q/203590`).text()).split('wed')[1].split(' ')[1] for 68 bytes \$\endgroup\$ Apr 16 '20 at 16:17
1
\$\begingroup\$

Python 2, 125 117 bytes

-6 bytes thanks to Kaddath

Using only the standard library, unlike the other python answer.

from urllib import*
x=urlopen("http://codegolf.stackexchange.com/q/203590").read()
print x[x.find("wed"):].split()[1]

Python 3, 149 142 bytes

-6 bytes thanks to Kaddath

from urllib.request import*
x=urlopen("http://codegolf.stackexchange.com/q/203590").read().decode("utf-8")
print(x[x.find("wed"):].split()[1])
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Do you know that the trailing /70761 is your user id to attribute the link share to you when someone clicks it (for badge I think), but it can be removed from the URL? \$\endgroup\$
    – Kaddath
    Apr 16 '20 at 15:30
1
\$\begingroup\$

sh, 96 bytes

wget -qO- http://codegolf.stackexchange.com/questions/203590|sed -n 's/.*\(V.*mes\).*/\1/p;540q'

This assumes the presence of wget and sed. We can save 5 bytes (the ;540q part) if we can assume there's no more match of V.*mes on the page -- as of this writing, there isn't, but that may not be true after more submissions. This also assumes the first part of the page is fairly static, and the Viewed XXX times sentence appears uniquely before line 540 (which it currently does).

sh, 101 bytes

wget -qO- http://codegolf.stackexchange.com/questions/203590|sed -n 's/.*\(V.*mes\).*/\1/p;/V.*mes/q

Similar to the above, but this variation does not depend on which line the Viewed XXX times appears -- it terminates after the finding the first one.

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

PHP, 96 89 bytes

<?=split(' ',strstr(join(file('http://codegolf.stackexchange.com/q/203590')),'wed '))[1];

Inspired by @newbie's answer. I think I can do better, still a first try. I strangely only got malformed characters from the JSON api :/

EDIT gained 7 bytes using split and strstr instead of preg_match

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

Python 2 with requests, 113 bytes

from requests import*
t=get('http://codegolf.stackexchange.com/q/203590').text
print t[t.find('wed'):].split()[1]

A pair of () is needed for Python 3 (114 bytes).

\$\endgroup\$
5
  • \$\begingroup\$ You might want to mention that you're using a 3rd party library(requests) \$\endgroup\$ Apr 16 '20 at 14:52
  • \$\begingroup\$ Done, thanks. @SuperStormer \$\endgroup\$
    – newbie
    Apr 16 '20 at 14:53
  • \$\begingroup\$ polyglot with python3 \$\endgroup\$ Apr 16 '20 at 15:47
  • \$\begingroup\$ oh the print format doesn't work in python3 \$\endgroup\$ Apr 16 '20 at 15:50
  • \$\begingroup\$ so for python3 it's +1, with () \$\endgroup\$ Apr 16 '20 at 15:51
0
\$\begingroup\$

T-SQL, 43 bytes

T-SQL executed directly against the Stack Exchange Data Explorer:

SELECT ViewCount FROM Posts WHERE Id=203590

Based on my answer to this similar question.

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

Bash + common tools, 67

lynx -dump codegolf.stackexchange.com/q/203590|grep -Po 'wed \K\d+'
\$\endgroup\$

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