11from bs4 import BeautifulSoup
22
3- import requests
3+ import requests , json
44
55
66class UsernameError (Exception ):
@@ -49,7 +49,7 @@ def contests_details_get():
4949 'global rank' : rating_table_rows [2 ].a .hx .text ,
5050 'country rank' : rating_table_rows [3 ].a .hx .text }
5151
52- cook_off = {'name' : 'Cook-off' ,'rating' : rating_table_rows [5 ].text ,
52+ cook_off = {'name' : 'Cook-off' , 'rating' : rating_table_rows [5 ].text ,
5353 'global rank' : rating_table_rows [6 ].a .hx .text ,
5454 'country rank' : rating_table_rows [7 ].a .hx .text }
5555
@@ -66,6 +66,73 @@ def contests_details_get():
6666
6767 return details
6868
69+ def __codeforces (self ):
70+ url = 'https://codeforces.com/api/user.info?handles={}' .format (self .__username )
71+
72+ page = requests .get (url )
73+
74+ if page .status_code != 200 :
75+ raise UsernameError ('User not Found' )
76+
77+ details_api = page .json ()
78+
79+ if details_api ['status' ] != 'OK' :
80+ raise UsernameError ('User not Found' )
81+
82+ details_api = details_api ['result' ][0 ]
83+
84+ details = {'status' : 'Success' , 'username' : self .__username , 'platform' : 'Codeforces' ,
85+ 'rating' : details_api ['rating' ], 'max rating' : details_api ['maxRating' ],
86+ 'rank' : details_api ['rank' ], 'max rank' : details_api ['maxRank' ]}
87+
88+ return details
89+
90+ def __spoj (self ):
91+ url = 'https://www.spoj.com/users/{}/' .format (self .__username )
92+
93+ page = requests .get (url )
94+
95+ soup = BeautifulSoup (page .text , 'html.parser' )
96+ details_container = soup .find_all ('p' )
97+
98+ points = details_container [2 ].text .split ()[3 ][1 :]
99+ rank = details_container [2 ].text .split ()[2 ][1 :]
100+ join_date = details_container [1 ].text .split ()[1 ] + ' ' + details_container [1 ].text .split ()[2 ]
101+ institute = ' ' .join (details_container [3 ].text .split ()[1 :])
102+
103+ def get_solved_problems ():
104+ table = soup .find ('table' , class_ = 'table table-condensed' )
105+
106+ rows = table .findChildren ('td' )
107+
108+ solved_problems = []
109+ for row in rows :
110+ if row .a .text :
111+ solved_problems .append (row .a .text )
112+
113+ return solved_problems
114+
115+ def get_todo ():
116+ try :
117+ table = soup .find_all ('table' , class_ = 'table' )[1 ]
118+ except :
119+ return None
120+
121+ rows = table .findChildren ('td' )
122+
123+ todo_problems = []
124+ for row in rows :
125+ if row .a .text :
126+ todo_problems .append (row .a .text )
127+
128+ return todo_problems
129+
130+ details = {'status' : 'Success' , 'username' : self .__username , 'platform' : 'SPOJ' ,
131+ 'points' : points , 'rank' : rank , 'solved' : get_solved_problems (),
132+ 'todo' : get_todo (), 'join data' : join_date , 'institute' : institute }
133+
134+ return details
135+
69136 def __interviewbit (self ):
70137 url = 'https://www.interviewbit.com/profile/{}' .format (self .__username )
71138
@@ -86,18 +153,27 @@ def __interviewbit(self):
86153 return details
87154
88155 def get_details (self , platform ):
89- if platform == 'interviewbit' :
90- return self .__interviewbit ()
91-
92156 if platform == 'codechef' :
93157 return self .__codechef ()
94158
159+ if platform == 'codeforces' :
160+ return self .__codeforces ()
161+
162+ if platform == 'spoj' :
163+ try :
164+ return self .__spoj ()
165+ except AttributeError :
166+ raise UsernameError ('User not Found' )
167+
168+ if platform == 'interviewbit' :
169+ return self .__interviewbit ()
170+
95171 raise PlatformError ('Platform not Found' )
96172
97173
98174if __name__ == '__main__' :
99175 ud = UserData ('abhijeet_ar' )
100176
101- ans = ud .get_details ('codechef ' )
177+ ans = ud .get_details ('spoj ' )
102178
103179 print (ans )
0 commit comments