-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
273 lines (198 loc) · 8.91 KB
/
views.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
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import GFGProfile, CodeChefProfile, LeetCodeProfile, HackerRankProfile
from .serializers import GFGProfileSerializer, CodeChefProfileSerializer, LeetCodeProfileSerializer, HackerRankProfileSerializer
from .scrapping import getCodeChefProfile, getHackerRankProfile, getGFGProfile, getLeetCodeProfile
def home(request):
return render(request, 'api/home.html', {})
def getDataFromSerializerObject(serializerOb):
'''
Returns data from the serializer object if user found by adding status key to dictionary object
param:
serializerOb -> false or serializer object
return:
Dict -> not found -> message with false status
found -> data with true status
'''
if serializerOb:
data = serializerOb.data
data['status'] = True
return data
return {
'message': 'user not found',
'status': False,
}
def GFGProfileOperation(username:str, platform:str):
'''
Gets scarpped profile details for GFG, performs db operations and returns serializer object for api
param:
username -> username of the user to get details
platfrom -> platform to get details from
return:
serializer -> API serializer object
'''
data = {}
# Get GeeksForGeeks profile
profileDetail = getGFGProfile(username=username, platform=platform)
data['status'] = profileDetail['status'] # is user found?
# if profile is not found, return http 404 with status as False
if(not profileDetail['status']):
data['response'] = Response(profileDetail, status=status.HTTP_404_NOT_FOUND)
return data
# If profile is found, create/update it in db
newProfile = GFGProfile(
username=profileDetail['username'],
platform=profileDetail['platform'],
problems=profileDetail['problems'],
)
newProfile.save()
# Get serialized data from the profile
data['serializer'] = GFGProfileSerializer(newProfile)
return data
def CodeChefProfileOperation(username:str, platform:str):
'''
Gets scarpped profile details for CodeChef, performs db operations and returns serializer object for api
param:
username -> username of the user to get details
platfrom -> platform to get details from
return:
serializer -> API serializer object
'''
data = {}
# Get CodeChef profile
profileDetail = getCodeChefProfile(username=username, platform=platform)
data['status'] = profileDetail['status'] # is user found?
# if profile is not found, return http 404 with status as False
if(not profileDetail['status']):
data['response'] = Response(profileDetail, status=status.HTTP_404_NOT_FOUND)
return data
# If profile is found, create/update it in db
newProfile = CodeChefProfile(
username=profileDetail['username'],
platform=profileDetail['platform'],
stars=profileDetail['stars'],
rating=profileDetail['rating'],
icon=profileDetail['icon'],
)
newProfile.save()
# Get serialized data from the profile
data['serializer'] = CodeChefProfileSerializer(newProfile)
return data
def LeetCodeProfileOperation(username:str, platform:str):
'''
Gets scarpped profile details for LeetCode, performs db operations and returns serializer object for api
param:
username -> username of the user to get details
platfrom -> platform to get details from
return:
serializer -> API serializer object
'''
data = {}
# Get LeetCode profile
profileDetail = getLeetCodeProfile(username=username, platform=platform)
data['status'] = profileDetail['status'] # is user found?
# if profile is not found, return http 404 with status as False
if(not profileDetail['status']):
data['response'] = Response(profileDetail, status=status.HTTP_404_NOT_FOUND)
return data
# If profile is found, create/update it in db
newProfile = LeetCodeProfile(
username=profileDetail['username'],
platform=profileDetail['platform'],
problems=profileDetail['problems'],
)
newProfile.save()
# Get serialized data from the profile
data['serializer'] = LeetCodeProfileSerializer(newProfile)
return data
def HackerRankProfileOperation(username:str, platform:str):
'''
Gets scarpped profile details for HackerRank, performs db operations and returns serializer object for api
param:
username -> username of the user to get details
platfrom -> platform to get details from
return:
serializer -> API serializer object
'''
data = {}
# Get HackerRank profile
profileDetail = getHackerRankProfile(username=username, platform=platform)
data['status'] = profileDetail['status'] # is user found?
# if profile is not found, return http 404 with status as False
if(not profileDetail['status']):
data['response'] = Response(profileDetail, status=status.HTTP_404_NOT_FOUND)
return data
# If profile is found, create/update it in db
newProfile = HackerRankProfile(
username=profileDetail['username'],
platform=profileDetail['platform'],
badges=profileDetail['badges'],
)
newProfile.save()
# Get serialized data from the profile
data['serializer'] = HackerRankProfileSerializer(newProfile)
return data
class GetDetails(APIView):
def get(self, request):
# Retrieve data from URL params
username = str(request.GET['username'])
platform = str(request.GET['platform']).lower()
# data holder for details
profileDetail = {}
# Check for GeeksForGeeks profile, and create/update it in db
if(platform=='gfg'):
data = GFGProfileOperation(username, platform)
if(not data['status']): return data['response']
# Get serialized data from the profile
serializerData = data['serializer'].data
# Check for LeetCode profile, and create/update it in db
elif(platform=='leetcode'):
data = LeetCodeProfileOperation(username, platform)
if(not data['status']): return data['response']
# Get serialized data from the profile
serializerData = data['serializer'].data
# Check for CodeChef profile, and create/update it in db
elif(platform=='codechef'):
data = CodeChefProfileOperation(username, platform)
if(not data['status']): return data['response']
# Get serialized data from the profile
serializerData = data['serializer'].data
# Check for HackerRank profile, and create/update it in db
elif(platform=='hackerrank'):
data = HackerRankProfileOperation(username, platform)
if(not data['status']): return data['response']
# Get serialized data from the profile
serializerData = data['serializer'].data
# Check for all platform presence and create/update it in db
elif(platform=='all'):
serializerData = {}
# Get data from all platform
gfgData = GFGProfileOperation(username, 'gfg')
leetcodeData = LeetCodeProfileOperation(username, 'leetcode')
codechefData = CodeChefProfileOperation(username, 'codechef')
hackerrankData = HackerRankProfileOperation(username, 'hackerrank')
# If user not found in any of the platform
if(not (gfgData['status'] or leetcodeData['status'] or codechefData['status'] or hackerrankData['status'])):
return Response({
'status': False,
'message': 'user not found in any of the platforms'
}, status=status.HTTP_404_NOT_FOUND)
# Get serialized data from the profile
serializerData['platform'] = {
'gfg': getDataFromSerializerObject(gfgData.get('serializer', False)),
'leetcode': getDataFromSerializerObject(leetcodeData.get('serializer', False)),
'codechef': getDataFromSerializerObject(codechefData.get('serializer', False)),
'hackerrank': getDataFromSerializerObject(hackerrankData.get('serializer', False)),
}
# Return 404 in case of none above
else:
return Response({
'status': False,
'message': "incorrect value of platform parameter",
}, status=status.HTTP_404_NOT_FOUND)
# Add status code to response
serializerData['status'] = True
# return the API response as json
return Response(serializerData, status=status.HTTP_302_FOUND)