How to View Latest HN Story Comments?
When I check the comments on a post, I'm quite curious to see the latest comments but have to traverse through each branch structure. Is there an easier way to do this?
It would have been cooler to make a bookmarklet in javascript, but I didn't want to learn javascript for this.
import re
import sys
import tempfile
import urllib2
import webbrowser
try:
url = sys.argv[1]
page = urllib2.urlopen(url)
except Exception as e:
print 'How to run: python hnlatest.py url'
sys.exit(2)
items = re.findall('<a href="item[^>]+>', page.read())
items = sorted(set([x.split('=')[2].split('"')[0] for x in items]))
items = ['https://news.ycombinator.com/item?id=' + x for x in items]
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as tfile:
for i in items:
tfile.write('<a href="' + i + '">' + i + '</a>' + '<br/>\n')
webbrowser.open_new_tab('file://' + tfile.name)