Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jun 13, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
Latest commit
141 lines (123 loc) · 5.04 KB
/
Copy path__init__.py
File metadata and controls
141 lines (123 loc) · 5.04 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# TODO use swagger based autogenerated library for implementing these functions
# TODO use colors for heading and contests
from ...modelsimportJudge
from ...utils.loggingimportlogger
importclick
importrequests
importjson
from .utilsimportget_data, login_oauth, login_web
frombeautifultableimportBeautifulTable
importos
ALLOW_WEB=False
classCodechef(Judge):
def__init__(self, session_data=None):
# Init should not have any network requests
# do them in login, logout, check_running_contest
logger.debug("Initializing class Codechef with session_data:\n%s"
%session_data)
self.name="Codechef"
self.url="https://www.codechef.com"
self.api_url="https://api.codechef.com"
# In case main api is down, use the env CODECHEF_WEB
# as a feature switch to use the browser based Non
# authenticated API. This is very helpful for testing
self.CODECHEF_WEB= (
'TERMICODER_CODECHEF_WEB'inos.environand
os.environ['TERMICODER_CODECHEF_WEB'].lower() =='true'
andALLOW_WEB)
if(self.CODECHEF_WEB):
self.api_url="https://www.codechef.com/api"
self.session_data=session_data
if(session_dataisnotNone):
self._update_session()
defcheck_login(self):
logger.debug("Checking Login")
if(self.sessionisNone):
logger.debug("No session object initialized")
returnFalse
path='user/me'ifself.CODECHEF_WEBelse'users/me'
me_url=self._make_url(path)
r=self._request_api(me_url)
deflogin(self):
ifself.CODECHEF_WEB:
token=login_web(self)
else:
token=login_oauth()
self.session_data=token
self._update_session()
self.check_login()
deflogout(self):
logger.warning("Logout of CodeChef.")
click.confirm("Are you sure?", default=True, abort=True)
self.session_data=None
defget_running_contests(self):
logger.debug('get running contests')
contests=get_data.running_contests(self)
table=BeautifulTable()
table.width_exceed_policy=BeautifulTable.WEP_WRAP
# TODO: use map style.headers instead of str
# requires change with beautifultable. we may try dev version
table.column_headers=list(
map(str, ['code', 'name', 'end', 'start']))
forcontestincontests:
table.append_row(
[
contest['code'], contest['name'],
str(contest['startDate']), str(contest['endDate'])
]
)
returntable
# This method serves both as a problem getter as well as kind of factory
# for problem
defget_problem(self, problem_name, contest_name, problem_data=None):
# If problem data is passed, it should take precedence
# Method should call the respective Problem.__init__ method to create a
# problem instance and return it
raiseNotImplementedError
defget_contest(self, contest_name, contest_data=None):
# If contest data is passed, it should take precedence
# Method should call the respective Problem.__init__ method to create a
# problem instance and return it
pass
def_update_session(self):
self.session=requests.Session()
defdebug_url(r, *args, **kwargs):
logger.debug('Getting url %s'%r.url)
defdebug_data(r, *args, **kwargs):
try:
response=json.dumps(r.json(), indent=1)
exceptjson.JSONDecodeError:
response=r.text
logger.debug('Response %s'%response)
self.session.hooks['response'].append(debug_url)
self.session.hooks['response'].append(debug_data)
if(self.CODECHEF_WEBand'cookies'inself.session_data):
logger.debug(self.session_data['cookies'])
self.session.cookies=self.session_data['cookies']
elif('data'inself.session_data):
logger.debug('Token: '+self.session_data['data']['access_token'])
OAuth2_Header= {
'Authorization': 'Bearer '+
self.session_data['data']['access_token']
}
self.session.headers.update(OAuth2_Header)
def_make_url(self, rel_url):
rel_url=rel_url.strip('/')
api_url=self.api_url.strip('/')
return"/".join([api_url, rel_url])
def_request_api(self, url):
logger.debug('fetching url %s'%url)
withself.sessionass:
r=s.get(url)
logger.debug(r)
r.raise_for_status()
returnr.json()
def_refresh_token(self):
logger.debug('refreshing token')
url='http://termicoder.diveshuttam.me/refresh_token/'
raiseNotImplementedError
# TODO implement this on server side
r=requests.get(url, data=self.session_data)
logger.debug(r.json())