<filename>Book_1/Working with API/working_with_hackernews.py
from operator import itemgetter
import requests

url='https://hacker-news.firebasio.com/v0/topstories.json'
r=requests.get(url)
print (f"Status code: {r.status_code}")

submission_ids=r.json()
submission_dicts=[]
for submission_id in submission_ids[:30]:
  url=f'https://hacker-news.firebasio.com/v0/item/{submission_id}.json'
  r=requests.get(url)
  print (f"id: {submission_id}\tstatus: {r.status_code} ")
  response_dict=r.json()

  submission_dict={
    'title': response_dict['title'],
    'hn_link': f"http://news.ycombinator.com/item?id={submission_id}",
    'comments' : response_dict['descendants'],
  }
  submission_dicts.append(submission_dict)

submission_dicts=sorted(submission_dicts, key=itemgetter('comments'), reverse=True)
for submission_dict in submission_dicts:
  print (f"Title: {submission_dict['title']}")
  print (f"Discussion link: {submission_dict['hn_link']}")
  print (f"comments: {submission_dict['comments']}")

<filename>Book_1/Working with API/working_with_hackernews.py
from operator import itemgetter
import requests

url='https://hacker-news.firebasio.com/v0/topstories.json'
r=requests.get(url)
print (f"Status code: {r.status_code}")

submission_ids=r.json()
submission_dicts=[]
for submission_id in submission_ids[:30]:
  url=f'https://hacker-news.firebasio.com/v0/item/{submission_id}.json'
  r=requests.get(url)
  print (f"id: {submission_id}\tstatus: {r.status_code} ")
  response_dict=r.json()

  submission_dict={
    'title': response_dict['title'],
    'hn_link': f"http://news.ycombinator.com/item?id={submission_id}",
    'comments' : response_dict['descendants'],
  }
  submission_dicts.append(submission_dict)

submission_dicts=sorted(submission_dicts, key=itemgetter('comments'), reverse=True)
for submission_dict in submission_dicts:
  print (f"Title: {submission_dict['title']}")
  print (f"Discussion link: {submission_dict['hn_link']}")
  print (f"comments: {submission_dict['comments']}")
