Skip to content

Commit d832c1b

Browse files
Add timeframe filter for company questions
Refactor functions to include a timeframe parameter when fetching questions by company. Update parsing logic to handle the new `--timeframe` argument. Document usage examples for fetching questions based on the specified timeframe.
1 parent a58397a commit d832c1b

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

.github/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ Or without Docker:
9999
poetry run python main.py --plans leetcode-75 top-interview-150
100100
```
101101

102+
### Want to fetch the most asked questions for a specific company in the last N months or days?
103+
104+
```sh
105+
docker compose run leetcode-scraper --company google --timeframe 3m
106+
```
107+
108+
Or without Docker:
109+
110+
```sh
111+
poetry run python main.py --company google --timeframe 3m
112+
```
113+
114+
This will fetch the most asked questions at Google in the last 3 months.
115+
102116
## Running Tests
103117

104118
Run the tests with the following command:

leetcode/leetcode.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ def get_problem(self, slug: str) -> Problem:
131131
"""
132132
return self.problems[slug] if slug in self.problems else None
133133

134-
def fetch_and_store_company_problems(self, company: str) -> List[Problem]:
134+
def fetch_and_store_company_problems(
135+
self, company: str, timeframe: str
136+
) -> List[Problem]:
135137
"""
136138
Fetch problems from LeetCode by the company tag and store them in the companies' dictionary.
137139
138140
:param company: The company tag.
141+
:param timeframe: The timeframe for questions (e.g., '30d', '3m', '6m').
139142
:return: A dictionary of fetched Problem objects with the company tag as the key.
140143
"""
141144
with self.companies_lock:
@@ -153,8 +156,20 @@ def fetch_and_store_company_problems(self, company: str) -> List[Problem]:
153156

154157
company_problems = []
155158

159+
# convert 30d, 3m, 6m to 'last-30-days', 'three-months', 'six-months'
160+
if timeframe == "30d":
161+
timeframe = "last-30-days"
162+
elif timeframe == "3m":
163+
timeframe = "three-months"
164+
elif timeframe == "6m":
165+
timeframe = "six-months"
166+
else:
167+
timeframe = "six-months"
168+
156169
questions = _fetch_with_retries(
157-
lambda: self.client.get_recent_questions_for_company(company)
170+
lambda: self.client.get_recent_questions_for_company(
171+
company, timeframe=timeframe
172+
),
158173
)
159174

160175
if not questions:

main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
load_dotenv()
1414

1515

16-
def main(csrf_token, leetcode_session, plans, company):
16+
def main(args):
17+
[csrf_token, leetcode_session, plans, company, timeframe] = args
18+
1719
if not leetcode_session and not csrf_token:
1820
print(
1921
"Using Non-Premium LeetCode account. Some premium only data will not be returned."
@@ -42,9 +44,11 @@ def main(csrf_token, leetcode_session, plans, company):
4244
print(e)
4345

4446
if company:
45-
print(f"Fetching company related problems: {company}")
47+
print(f"Fetching company related problems: {company} for {timeframe}")
4648
try:
47-
company_problems = leetcode.fetch_and_store_company_problems(company)
49+
company_problems = leetcode.fetch_and_store_company_problems(
50+
company, timeframe
51+
)
4852
print(
4953
"====================================================================================================="
5054
)
@@ -91,7 +95,13 @@ def main(csrf_token, leetcode_session, plans, company):
9195
default=None,
9296
help="Company name to filter problems by",
9397
)
98+
parser.add_argument(
99+
"--timeframe",
100+
type=str,
101+
default="6m",
102+
help="The timeframe for questions (e.g., 'last-30-days', 'three-months', 'six-months')",
103+
)
94104

95105
args = parser.parse_args()
96106

97-
main(args.csrf_token, args.leetcode_session, args.plans, args.company)
107+
main(args)

0 commit comments

Comments
 (0)