-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain.py
597 lines (418 loc) · 22.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
import os
import json
import aiofiles
from pandas import array
from src import filters
from fastapi import FastAPI, HTTPException, Request
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
# Initiating a FastAPI application
API_METADATA = {
"title": "College API",
"description": "Fetch the details of Indian Colleges",
"version": "1",
"contact": {
"name": "Clueless Community",
"url": "https://www.clueless.tech/",
"email": "https://www.clueless.tech/contact-us",
},
"license_info": {
"name": "GPL-3.0 license",
"url": "https://github.com/Clueless-Community/collegeAPI/blob/main/LICENSE.md",
}
}
app = FastAPI(**API_METADATA)
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# Homepage
@app.get('/', tags=['home'])
@limiter.limit("5/minute")
async def home(request: Request):
return API_METADATA
# Filter Function
def filter_colleges(field, region, region_list):
response = []
for i in region_list:
region_req = i.replace(" ", "").lower()
response.extend(filters.get_colleges(field, region, region_req))
return response
def paginate(data: array, page, limit):
return data[(page-1)*limit:page*limit]
@app.get('/all', description="All NIRF listed colleges.")
@limiter.limit("5/minute")
async def allNirf(request: Request, page: int = 1, limit: int = 50):
file_name = "college_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF listed colleges.'")
return paginate(json.loads(output), page, limit)
# All Colleges Nirf Ranking
@app.get('/all/nirf', description='All NIRF listed colleges ranking', tags=['all_NIRF_colleges_ranking'])
@limiter.limit("5/minute")
async def allNirf(request: Request, page: int = 1, limit: int = 50):
file_name = "college_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF Ranked colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/all/nirf/state={state}', description='Filter all NIRF listed colleges by state', tags=['all_NIRF_colleges_ranking'])
@limiter.limit("5/minute")
async def allNirfByState(request: Request, state: str or None = None):
response = filters.get_response("nirf", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of all NIRF listed colleges by '{state}'.")
return response
@app.get('/all/nirf/city={city}', description='Filter all NIRF listed colleges by city', tags=['all_NIRF_colleges_ranking'])
@limiter.limit("5/minute")
async def allNirfByCity(request: Request, city: str or None = None):
response = filters.get_response("nirf", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of all NIRF listed colleges by '{city}'.")
return response
# Engineering Colleges
@app.get('/engineering_colleges', description='All engineering colleges', tags=['engineering_colleges'])
@limiter.limit("5/minute")
async def engineeringColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "engineering_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All Engineering colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/engineering_colleges/nirf', description='All NIRF listed engineering colleges', tags=['engineering_colleges'])
@limiter.limit("5/minute")
async def engineeringCollegesNirf(request: Request, page: int = 1, limit: int = 50):
file_name = "engineering_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF Engineering colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/engineering_colleges/state={state}', description='Filter engineering colleges by state', tags=['engineering_colleges'])
@limiter.limit("5/minute")
async def engineeringCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("engineering", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Engineering colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/engineering_colleges/city={city}', description='Filter engineering colleges by city', tags=['engineering_colleges'])
@limiter.limit("5/minute")
async def engineeringCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("engineering", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Engineering colleges for '{city}'.")
return paginate(response, page, limit)
# Medical Colleges
@app.get('/medical_colleges', description='All medical colleges', tags=['medical_colleges'])
@limiter.limit("5/minute")
async def medicalColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "medical_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All Medical colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/medical_colleges/nirf', description='All NIRF listed medical colleges', tags=['medical_colleges'])
@limiter.limit("5/minute")
async def nirfMedicalColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "medical_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF Medical colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/medical_colleges/state={state}', description='Filter medical colleges by state', tags=['medical_colleges'])
@limiter.limit("5/minute")
async def medicalCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("medical", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Medical colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/medical_colleges/city={city}', description='Filter medical colleges by city', tags=['medical_colleges'])
@limiter.limit("5/minute")
async def medicalCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("medical", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Medical colleges for '{city}'.")
return paginate(response, page, limit)
# Management Colleges
@app.get('/management_colleges', description='All management colleges', tags=['management_colleges'])
@limiter.limit("5/minute")
async def managementColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "management_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All Management colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/management_colleges/nirf', description='All NIRF listed colleges', tags=['management_colleges'])
@limiter.limit("5/minute")
async def managementCollegesNirf(request: Request, page: int = 1, limit: int = 50):
file_name = "management_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF Management colleges.'")
return paginate(output, page, limit)
@app.get('/management_colleges/state={state}', description='Filter management colleges by state', tags=['management_colleges'])
@limiter.limit("5/minute")
async def managementCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("management", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Management colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/management_colleges/city={city}', description='Filter management colleges by city', tags=['management_colleges'])
@limiter.limit("5/minute")
async def managementCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("management", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Management colleges for '{city}'.")
return paginate(response, page, limit)
# Colleges
@app.get('/colleges', description='All colleges listed by NIRF', tags=['colleges'])
@limiter.limit("5/minute")
async def allColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "college_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF listed colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/colleges/nirf', description='Ranking of all NIRF listed colleges', tags=['colleges'])
@limiter.limit("5/minute")
async def nirfCollegesRanked(request: Request, page: int = 1, limit: int = 50):
file_name = "college_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF Ranked colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/colleges/state={state}', description='Filter all colleges by state', tags=['colleges'])
@limiter.limit("5/minute")
async def collegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("nirf", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of all colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/colleges/city={city}', description='Filter all colleges by city', tags=['colleges'])
@limiter.limit("5/minute")
async def collegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("nirf", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of all colleges for '{city}'.")
return paginate(response, page, limit)
# Pharmacy Colleges
@app.get('/pharmacy_colleges', description='All pharmacy colleges', tags=['pharmacy_colleges'])
@limiter.limit("5/minute")
async def allParticipatedPharmacyCollege(request: Request, page: int = 1, limit: int = 50):
file_name = "pharmacy_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All Pharmacy colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/pharmacy_colleges/nirf', description='All NIRF listed pharmacy colleges', tags=['pharmacy_colleges'])
@limiter.limit("5/minute")
async def pharmacyCollegesNirf(request: Request, page: int = 1, limit: int = 50):
file_name = "pharmacy_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data for 'All NIRF Pharmacy colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/pharmacy_colleges/state={state}', description='Filter all pharmacy colleges by state', tags=['pharmacy_colleges'])
@limiter.limit("5/minute")
async def pharmacyCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("pharmacy", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Pharmacy colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/pharmacy_colleges/city={city}', description='Filter all pharmacy colleges by city', tags=['pharmacy_colleges'])
@limiter.limit("5/minute")
async def pharmacyCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("pharmacy", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Pharmacy colleges for '{city}'.")
return paginate(response, page, limit)
# Dental Colleges
@app.get("/dental_colleges", description='List all dental colleges', tags=['dental_colleges'])
@limiter.limit("5/minute")
async def participating_dental_colleges(request: Request, page: int = 1, limit: int = 50):
file_name = "dental_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All Dental colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/dental_colleges/nirf', description='List all NIRF listed dental colleges', tags=['dental_colleges'])
@limiter.limit("5/minute")
async def nirf_dental_colleges(request: Request, page: int = 1, limit: int = 50):
file_name = "dental_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All NIRF Dental colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/dental_colleges/state={state}', description='Filter dental colleges by given state', tags=['dental_colleges'])
@limiter.limit("5/minute")
async def dentalCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("dental", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Dental colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/dental_colleges/city={city}', description='Filter dental colleges by given city', tags=['dental_colleges'])
@limiter.limit("5/minute")
async def dentalCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("dental", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Dental colleges for '{city}'.")
return paginate(response, page, limit)
# Law Colleges
@app.get('/law_colleges/nirf', description='All NIRF listed law colleges', tags=['law_colleges'])
@limiter.limit("5/minute")
async def nirf_law_colleges(request: Request, page: int = 1, limit: int = 50):
file_name = "law_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All NIRF Law colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/law_colleges', description='All the participated law institutes', tags=['law_colleges'])
@limiter.limit("5/minute")
async def law_colleges(request: Request, page: int = 1, limit: int = 50):
file_name = "law_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All participated Law institutes.'")
return paginate(json.loads(output), page, limit)
@app.get('/law_colleges/state={state}', description='Filter law colleges by given state', tags=['law_colleges'])
@limiter.limit("5/minute")
async def dentalCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("law", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Law colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/law_colleges/city={city}', description='Filter law colleges by given city', tags=['law_colleges'])
@limiter.limit("5/minute")
async def dentalCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("law", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Law colleges for '{city}'.")
return paginate(response, page, limit)
# Architecture Colleges
@app.get('/architecture_colleges', description='List all architecture colleges', tags=['architecture_colleges'])
@limiter.limit("5/minute")
async def researchColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "architecture_participated.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All Architecture colleges'.")
return paginate(json.loads(output), page, limit)
@app.get('/architecture_colleges/nirf', description='All NIRF listed architecture colleges', tags=['architecture_colleges'])
@limiter.limit("5/minute")
async def architectureCollegesNirf(request: Request, page: int = 1, limit: int = 50):
file_name = "architecture_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All NIRF Architecture colleges'.")
return paginate(json.loads(output), page, limit)
@app.get('/architecture_colleges/state={state}', description='Filter architecture colleges by state', tags=['architecture_colleges'])
@limiter.limit("5/minute")
async def architectureCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("architecture", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Architecture collegs for '{state}'.")
return paginate(response, page, limit)
@app.get('/architecture_colleges/city={city}', description='Filter architecture colleges by city', tags=['architecture_colleges'])
@limiter.limit("5/minute")
async def architectureCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("architecture", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of Architecture collegs for '{city}'.")
return paginate(response, page, limit)
# Research Colleges
@app.get('/research_colleges', description='All research colleges', tags=['research_colleges'])
@limiter.limit("5/minute")
async def researchColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "research_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All research colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/research_colleges/nirf', description='All NIRF listed research colleges', tags=['research_colleges'])
@limiter.limit("5/minute")
async def nirfResearchColleges(request: Request, page: int = 1, limit: int = 50):
file_name = "research_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All NIRF research colleges.'")
return paginate(json.loads(output), page, limit)
@app.get('/research_colleges/state={state}', description='Filter research colleges by state', tags=['research_colleges'])
@limiter.limit("5/minute")
async def researchCollegesByState(request: Request, state: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("research", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of research colleges for '{state}'.")
return paginate(response, page, limit)
@app.get('/research_colleges/city={city}', description='Filter research colleges by city', tags=['research_colleges'])
@limiter.limit("5/minute")
async def researchCollegesByCity(request: Request, city: str or None = None, page: int = 1, limit: int = 50):
response = filters.get_response("research", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch data of research colleges for '{city}'.")
return paginate(response, page, limit)
# University Endpoints
@app.get('/universities', description="List all NIRF universities", tags=['universities'])
@limiter.limit("5/minute")
async def universities(request: Request, page: int = 1, limit: int = 50):
file_name = "university_ranking.json"
output = filters.get_field_data(file_name)
if output == -1:
raise HTTPException(
status_code=404, detail="Unable to fetch data for 'All NIRF Universities.'")
return paginate(json.loads(output), page, limit)
@app.get('/universities/state={state}', description="Filter universities by state.", tags=['universities'])
@limiter.limit("5/minute")
async def universitiesbyState(request: Request, state, page: int = 1, limit: int = 50):
response = filters.get_response("universities", state=state)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch Universities data for '{state}'.")
return paginate(response, page, limit)
@app.get('/universities/city={city}', description='Filter universities by city', tags=['universities'])
@limiter.limit("5/minute")
async def universitiesByCity(request: Request, city, page: int = 1, limit: int = 50):
response = filters.get_response("universities", city=city)
if response == -1:
raise HTTPException(
status_code=404, detail=f"Unable to fetch Universities data for '{city}'.")
return paginate(response, page, limit)