import requests from time import time as t NEWSSS="" def News(KEY,cache=True): global NEWSSS if NEWSSS: return NEWSSS, None, 0 C = t() main_url = f'https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey={KEY}' main_page = requests.get(main_url).json() articles = main_page["articles"] head = [] day = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"] for ar in articles: head.append(ar["title"]) temp = [] for i in range(len(day)): temp.append(f"today's {day[i]} news is: {head[i]}\n") result = "".join(temp) NEWSSS=result # Cache the news return result, None, t() - C if __name__ == "__main__": print(News("5b57a2e4baa74123b6db7dff6967881b"))