-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
from datetime import datetime, timedelta | ||
|
||
import requests | ||
|
||
HF_API_URL = "https://huggingface.co/api/daily_papers" | ||
ARXIV_URL = "https://arxiv.org/abs/{paper_id}" | ||
|
||
|
||
# Function to parse the date string | ||
def parse_date(date_str): | ||
return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ") | ||
|
||
|
||
def fetch_papers(top_n=5): | ||
try: | ||
response = requests.get(f"{HF_API_URL}?limit=100") | ||
response.raise_for_status() | ||
items = response.json() | ||
items.sort(key=lambda x: x.get("paper", {}).get("upvotes", 0), reverse=True) | ||
items = [ | ||
|
||
# Calculate the date 3 days ago from now | ||
three_days_ago = datetime.now() - timedelta(days=3) | ||
|
||
# Filter items from the last 3 days | ||
recent_items = [ | ||
item | ||
for item in items | ||
if parse_date(item.get("publishedAt")) >= three_days_ago | ||
] | ||
|
||
recent_items.sort( | ||
key=lambda x: x.get("paper", {}).get("upvotes", 0), reverse=True | ||
) | ||
output_items = [ | ||
{ | ||
"title": item.get("paper", {}).get("title"), | ||
"url": ARXIV_URL.format(paper_id=item.get("paper", {}).get("id")), | ||
"upvotes": item.get("paper", {}).get("upvotes"), | ||
} | ||
for item in items[:top_n] | ||
for item in recent_items[:top_n] | ||
] | ||
except Exception as e: | ||
print(e) | ||
return [] | ||
|
||
return items | ||
return output_items |