What's the most popular question right now?

4

Your task is to print the titles of the questions with the five highest scores (upvotes minus downvotes) here on CodeGolf.SE, while skipping over any closed, locked and on-hold questions.

Currently these are:

Produce the number 2014 without any numbers in your source code
Showcase your language one vote at a time
American Gothic in the palette of Mona Lisa: Rearrange the pixels
Images with all colors
Build a Compiler Bomb

Remember, this is , so the code with the smallest number of bytes wins.

Oliver Ni

Posted 2016-10-30T18:10:13.997

Reputation: 9 650

15Nitpick: "most popular right now" would better describe what's at the top of the "hot" list ;) – ETHproductions – 2016-10-30T18:14:19.857

Would using a URL shortener be cheating? – Artyer – 2016-10-30T22:20:25.637

3@Artyer Yes. It violates a standard loophole. – Oliver Ni – 2016-10-30T22:35:44.247

Answers

8

JavaScript + JQuery, 178 bytes

Quite a simple approach that makes the StackExchange API do all the work.

$.ajax('//api.stackexchange.com/search/advanced?sort=votes&closed=False&wiki=False&notice=False&pagesize=5&site=codegolf').pipe(x=>x.items.map(x=>document.write(x.title+'<br>')))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

a=x=>x.items.map(x=>document.write(x.title+'<br>'))
document.write('<script src="//api.stackexchange.com/search/advanced?sort=votes&closed=False&wiki=False&pagesize=5&site=codegolf&callback=a"><\/script>')
<!-- No dependencies, Pure JS. 204 bytes. -->

http://api.stackexchange.com/search/advanced?sort=votes&closed=False&wiki=False&pagesize=5&site=codegolf is the StackExchange API page that gives a JSON. This JSON data is the questions on codegolf.stackexchange.com sorted by votes where they haven't been closed and are not a wiki page and don't have a closed notice.
This is called with JQuery's $.ajax, which works JSON-P. It adds a &callback=functionName to the url so the API returns some javascript for a function that calls the data, so it can be used with the function that logs it.

<!-- For fun, do this for any site -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>StackExchange&nbsp;Site:&nbsp;<input value="codegolf"> <button onclick="$.ajax({success:x=>{try{document.getElementById('pre').innerHTML=x.items.map(x=>x.title).join('\n')+'\n\nQueries left: ' + x.quota_remaining}catch(e){try{document.getElementById('pre').textContent='Queries left: ' + (x.quota_remaining || 'Error')}catch(e){document.getElementById('pre').textContent='Error'}}},url:'//api.stackexchange.com/search/advanced?sort='+document.getElementById('s').value+'&closed=False&wiki=False&notice=False&pagesize=5&site=' + this.previousElementSibling.value})">Go</button> Sort&nbsp;by:&nbsp;<select id='s'><option value="activity">Active</option><option value="votes" selected="selected">Score</option><option value="creation">New</option><!--option value="hot">Hot</option><option value="week">Week</option><option value="month">Month</option--></select><pre id="pre"></pre>

Some caveats while answering this question:

  1. StackExchange API always trys to returns gzipped data. In JS, the browser un-gzips for you.
  2. The API only seems to be able to return the top 100 at maximum, so if you were to filter out the closed questions programatically, you must assume that less than 96 of the top 100 questions are closed, which you can't so you have to do everything in the APIs query.
  3. The things in the API are HTML encoded, so you must also decode those. In JS, I let the browser decode it, but in something like Python, you must employ an external library, or else hard-code the dozens of entities.

That said, here is my Python solution that has to jump all of those hoops:

Python 3, 259 bytes

import urllib.request as r,gzip,html,json
for i in json.loads(gzip.decompress(r.urlopen('http://api.stackexchange.com/search/advanced?sort=votes&closed=False&wiki=False&notice=False&pagesize=5&site=codegolf').read()))['items']:print(html.unescape(i['title']))

Artyer

Posted 2016-10-30T18:10:13.997

Reputation: 1 697

1

Bash + curl + gunzip + recode + jq, 165 bytes

(Newline for clarity)

curl "api.stackexchange.com/search/advanced?sort=votes&closed=False&wiki=False&notice=False&pagesize=5&site=codegolf"
|gunzip|jq -r ".items[].title"|recode html:ascii

Or if you don't want any output on stderr for 4 more bytes :

curl -s "api.stackexchange.com/search/advanced?sort=votes&closed=False&wiki=False&notice=False&pagesize=5&site=codegolf"
|gunzip|jq -r ".items[].title"|recode html..ascii

I thought a bash solution using @Artyer's url would be shorter, turns out it's not that much.

Problems that make it longer :

  • The stackexchange API responds with compressed data. Using curl's --compressed flag, gzip -d or gunzip solve that, gunzip being the shortest
  • We need to translate HTML entities
  • jq outputs JSON data, where strings are enclosed in quotes. Using -r makes it output raw data.

Aaron

Posted 2016-10-30T18:10:13.997

Reputation: 3 689