Skip to content

Commit e886b0d

Browse files
committed
Fetch data from Gmail API with time range
1 parent 64eebdc commit e886b0d

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

gmail_fisher/api/gateway.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,21 @@ def __authenticate(cls) -> Credentials:
6565

6666

6767
class GmailGateway:
68-
"""Maximum number of workers for thread pool executor"""
68+
@staticmethod
69+
def _get_search_query(sender_emails, keywords, start_time, end_time):
70+
if start_time is None or end_time is None:
71+
return f"from:{sender_emails} {keywords}"
72+
else:
73+
return f"from:{sender_emails} {keywords} after:{start_time} before:{end_time}"
6974

7075
@classmethod
7176
def list_message_ids(
72-
cls, sender_emails: str, keywords: str, max_results: int
77+
cls,
78+
sender_emails: str,
79+
keywords: str,
80+
max_results: int,
81+
start_time: str = None,
82+
end_time: str = None,
7383
) -> Iterable[str]:
7484
"""
7585
For a given sender email and comma-separated keywords, retrieve the matching
@@ -82,7 +92,7 @@ def list_message_ids(
8292
.messages()
8393
.list(
8494
userId="me",
85-
q=f"from:{sender_emails} {keywords}",
95+
q=cls._get_search_query(sender_emails, keywords, start_time, end_time),
8696
maxResults=max_results,
8797
)
8898
.execute(http=GmailClient.auth_http_request())

gmail_fisher/parsers/food.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
logger = get_logger(__name__)
2323

2424

25-
def apply_restaurant_filter(func):
25+
def apply_restaurant_filter(method):
26+
"""
27+
Decorator method that wraps the 'get_restaurant' method from the food parsers
28+
and applies all restaurant filters to the output of that method.
29+
30+
:param method: Pass the function that is being wrapped
31+
:return: Filtered restaurant name
32+
"""
33+
2634
def wrapper(*args, **kwargs):
27-
return FoodExpenseParser.apply_restaurant_filters(func(*args, **kwargs))
35+
return FoodExpenseParser.apply_restaurant_filters(method(*args, **kwargs))
2836

2937
return wrapper
3038

gmail_fisher/utils/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
AUTH_PATH: Final[Path] = Path("auth/")
1616

1717
# CONCURRENCY
18+
# Maximum number of workers for thread pool executor
1819
THREAD_POOL_MAX_WORKERS: Final[int] = 200

tests/test_gmail_gateway.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from gmail_fisher.api.gateway import GmailGateway
2+
3+
4+
def test_get_email_messages():
5+
messages = GmailGateway.get_email_messages(sender_emails="portugal-food@bolt.eu", keywords="Delivery", max_results=10)
6+
print(len(messages))

0 commit comments

Comments
 (0)