Spaces:
Running
Running
Update news.py
Browse files
news.py
CHANGED
@@ -1,30 +1,11 @@
|
|
1 |
import requests
|
2 |
from time import time as t
|
3 |
-
import shelve
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
def get_cached_news():
|
8 |
-
with shelve.open(CACHE_FILE) as cache:
|
9 |
-
if "news" in cache:
|
10 |
-
last_update_time = cache["last_update_time"]
|
11 |
-
current_time = t()
|
12 |
-
if current_time - last_update_time < 12 * 60 * 60: # 12 hours in seconds
|
13 |
-
return cache["news"]
|
14 |
-
|
15 |
-
return None
|
16 |
-
|
17 |
-
def cache_news(news):
|
18 |
-
with shelve.open(CACHE_FILE) as cache:
|
19 |
-
cache["news"] = news
|
20 |
-
cache["last_update_time"] = t()
|
21 |
|
22 |
def News(KEY,cache=True):
|
23 |
-
if
|
24 |
-
|
25 |
-
if cached_news:
|
26 |
-
return cached_news, None, 0 # Return cached news
|
27 |
-
|
28 |
C = t()
|
29 |
main_url = f'https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey={KEY}'
|
30 |
main_page = requests.get(main_url).json()
|
@@ -38,7 +19,7 @@ def News(KEY,cache=True):
|
|
38 |
temp.append(f"today's {day[i]} news is: {head[i]}\n")
|
39 |
result = "".join(temp)
|
40 |
|
41 |
-
|
42 |
return result, None, t() - C
|
43 |
|
44 |
if __name__ == "__main__":
|
|
|
1 |
import requests
|
2 |
from time import time as t
|
|
|
3 |
|
4 |
+
NEWSSS=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def News(KEY,cache=True):
|
7 |
+
if NEWSSS:
|
8 |
+
return NEWSSS, None, t()
|
|
|
|
|
|
|
9 |
C = t()
|
10 |
main_url = f'https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey={KEY}'
|
11 |
main_page = requests.get(main_url).json()
|
|
|
19 |
temp.append(f"today's {day[i]} news is: {head[i]}\n")
|
20 |
result = "".join(temp)
|
21 |
|
22 |
+
NEWSSS=result # Cache the news
|
23 |
return result, None, t() - C
|
24 |
|
25 |
if __name__ == "__main__":
|