fork download
  1. import requests
  2. import json
  3.  
  4. def getAuthorHistory(author):
  5. history = []
  6. try:
  7. author_url = f"https://j...content-available-to-author-only...k.com/api/article_users?author={author}"
  8. author_response = requests.get(author_url)
  9. author_data = author_response.json()
  10. if 'data' in author_data and len(author_data['data']) > 0:
  11. about_field = author_data['data'][0].get('about')
  12. if about_field and about_field.strip():
  13. history.append(about_field)
  14. articles_base_url = f"https://j...content-available-to-author-only...k.com/api/articles?author={author}"
  15. current_page = 1
  16. total_pages = 1
  17. while current_page <= total_pages:
  18. articles_url = f"{articles_base_url}&page={current_page}"
  19. articles_response = requests.get(articles_url)
  20. articles_data = articles_response.json()
  21. total_pages = articles_data.get('total_pages', 1)
  22. if 'data' in articles_data:
  23. for article in articles_data['data']:
  24. title = None
  25. if 'title' in article and article['title'] and article['title'].strip():
  26. title = article['title']
  27. elif 'story_title' in article and article['story_title'] and article['story_title'].strip():
  28. title = article['story_title']
  29. if title:
  30. history.append(title)
  31. current_page += 1
  32. except Exception as e:
  33. print(f"Error: {e}")
  34. return history
  35. print(getAuthorHistory('apaga'))
Success #stdin #stdout 1s 36424KB
stdin
Standard input is empty
stdout
Error: HTTPSConnectionPool(host='jsonmock.hackerrank.com', port=443): Max retries exceeded with url: /api/article_users?author=apaga (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x155456a0e4b0>: Failed to resolve 'jsonmock.hackerrank.com' ([Errno -3] Temporary failure in name resolution)"))
[]