Skip to content

Commit 8626c56

Browse files
SamoedWauplin
andauthored
Add more parameters to daily papers (#3585)
* add more parameters to daily papers * fix dates tests * Apply suggestions from code review Co-authored-by: Lucain <[email protected]> * fix identation * lint --------- Co-authored-by: Lucain <[email protected]> Co-authored-by: Lucain <[email protected]>
1 parent d6f6e06 commit 8626c56

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/huggingface_hub/hf_api.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9890,6 +9890,12 @@ def list_daily_papers(
98909890
*,
98919891
date: Optional[str] = None,
98929892
token: Union[bool, str, None] = None,
9893+
week: Optional[str] = None,
9894+
month: Optional[str] = None,
9895+
submitter: Optional[str] = None,
9896+
sort: Optional[Literal["publishedAt", "trending"]] = None,
9897+
p: Optional[int] = None,
9898+
limit: Optional[int] = None,
98939899
) -> Iterable[PaperInfo]:
98949900
"""
98959901
List the daily papers published on a given date on the Hugging Face Hub.
@@ -9901,6 +9907,19 @@ def list_daily_papers(
99019907
token (Union[bool, str, None], *optional*):
99029908
A valid user access token (string). Defaults to the locally saved
99039909
token. To disable authentication, pass `False`.
9910+
week (`str`, *optional*):
9911+
Week in ISO format (YYYY-Www) for which to fetch daily papers. Example, `2025-W09`.
9912+
month (`str`, *optional*):
9913+
Month in ISO format (YYYY-MM) for which to fetch daily papers. Example, `2025-02`.
9914+
submitter (`str`, *optional*):
9915+
Username of the submitter to filter daily papers.
9916+
sort (`Literal["publishedAt", "trending"]`, *optional*):
9917+
Sort order for the daily papers. Can be either by `publishedAt` or by `trending`.
9918+
Defaults to `"publishedAt"`
9919+
p (`int`, *optional*):
9920+
Page number for pagination. Defaults to 0.
9921+
limit (`int`, *optional*):
9922+
Limit of papers to fetch. Defaults to 50.
99049923
99059924
Returns:
99069925
`Iterable[PaperInfo]`: an iterable of [`huggingface_hub.hf_api.PaperInfo`] objects.
@@ -9915,7 +9934,21 @@ def list_daily_papers(
99159934
```
99169935
"""
99179936
path = f"{self.endpoint}/api/daily_papers"
9918-
params = {"date": date} if date is not None else {}
9937+
9938+
params = {
9939+
k: v
9940+
for k, v in {
9941+
"p": p,
9942+
"limit": limit,
9943+
"sort": sort,
9944+
"date": date,
9945+
"week": week,
9946+
"month": month,
9947+
"submitter": submitter,
9948+
}.items()
9949+
if v is not None
9950+
}
9951+
99199952
r = get_session().get(path, params=params, headers=self._build_hf_headers(token=token))
99209953
hf_raise_for_status(r)
99219954
for paper in r.json():

tests/test_hf_api.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,6 +4299,51 @@ def test_list_daily_papers_default_date(self) -> None:
42994299
assert hasattr(papers[0], "id")
43004300
assert hasattr(papers[0], "title")
43014301

4302+
def test_list_daily_papers_week(self) -> None:
4303+
week = 44
4304+
papers = list(self.api.list_daily_papers(week=f"2025-W{week}"))
4305+
assert len(papers) > 0
4306+
first_paper = papers[0]
4307+
last_paper = papers[-1]
4308+
4309+
# friday of previous week
4310+
week_start = datetime.datetime.fromisocalendar(2025, week - 1, 5).replace(tzinfo=datetime.timezone.utc)
4311+
week_end = datetime.datetime.fromisocalendar(2025, week, 7).replace(tzinfo=datetime.timezone.utc)
4312+
assert week_start <= first_paper.submitted_at <= week_end
4313+
assert week_start <= last_paper.submitted_at <= week_end
4314+
4315+
def test_list_daily_papers_month(self) -> None:
4316+
month = 10
4317+
papers = list(self.api.list_daily_papers(month=f"2025-{month}"))
4318+
assert len(papers) > 0
4319+
first_paper = papers[0]
4320+
last_paper = papers[-1]
4321+
# last day of previous month
4322+
month_start = datetime.datetime(2025, month, 1, tzinfo=datetime.timezone.utc) - datetime.timedelta(days=1)
4323+
month_end = datetime.datetime(2025, month + 1, 1, tzinfo=datetime.timezone.utc) - datetime.timedelta(days=1)
4324+
assert month_start <= first_paper.submitted_at <= month_end
4325+
assert month_start <= last_paper.submitted_at <= month_end
4326+
4327+
def test_daily_papers_submitter(self) -> None:
4328+
papers = list(self.api.list_daily_papers(submitter="akhaliq"))
4329+
assert len(papers) > 0
4330+
assert papers[0].submitted_by.fullname == "AK"
4331+
4332+
def test_daily_papers_sort(self) -> None:
4333+
papers = list(self.api.list_daily_papers(date="2025-10-29", sort="trending"))
4334+
assert len(papers) > 0
4335+
first_paper = papers[0]
4336+
last_paper = papers[-1]
4337+
assert first_paper.upvotes >= last_paper.upvotes
4338+
4339+
def test_daily_papers_p(self) -> None:
4340+
papers = list(self.api.list_daily_papers(date="2025-10-29", p=100))
4341+
assert len(papers) == 0
4342+
4343+
def test_daily_papers_limit(self) -> None:
4344+
papers = list(self.api.list_daily_papers(date="2025-10-29", limit=10))
4345+
assert len(papers) == 10
4346+
43024347

43034348
class WebhookApiTest(HfApiCommonTest):
43044349
def setUp(self) -> None:

0 commit comments

Comments
 (0)