1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# paizaio.py |
4
|
|
|
# |
5
|
|
|
|
6
|
|
|
import requests |
7
|
|
|
import json |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TooLongException(Exception): |
11
|
|
|
pass |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
# |
15
|
|
|
# |
16
|
|
|
class PaizaIO: |
17
|
|
|
"""wandbox api class""" |
18
|
|
|
api_url = 'http://api.paiza.io/' |
19
|
|
|
parameter = {'source_code': '', 'language': 'cpp', 'api_key': 'guest'} |
20
|
|
|
session_id = '' |
21
|
|
|
|
22
|
|
|
def create(self): |
23
|
|
|
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
24
|
|
|
payload = json.dumps(self.parameter) |
25
|
|
|
r = requests.post(self.api_url + 'runners/create/', data=payload, headers=headers) |
26
|
|
|
r.raise_for_status() |
27
|
|
|
result = r.json() |
28
|
|
|
if 'error' in result: |
29
|
|
|
if 'longpoll timeout' not in result['error']: |
30
|
|
|
#print(result['error']) |
31
|
|
|
#print(result) |
32
|
|
|
if "Too long" in result['error']: |
33
|
|
|
raise TooLongException(result['error']) |
34
|
|
|
else: |
35
|
|
|
raise Exception(result['error']) |
36
|
|
|
|
37
|
|
|
if 'id' in result: |
38
|
|
|
self.session_id = result['id'] |
39
|
|
|
else: |
40
|
|
|
print(result) |
41
|
|
|
raise |
42
|
|
|
return result |
43
|
|
|
|
44
|
|
|
def get_status(self): |
45
|
|
|
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
46
|
|
|
parameter = {'id': self.session_id, 'api_key': self.parameter['api_key']} |
47
|
|
|
payload = json.dumps(parameter) |
48
|
|
|
r = requests.get(self.api_url + 'runners/get_status/', data=payload, headers=headers) |
49
|
|
|
r.raise_for_status() |
50
|
|
|
return r.json() |
51
|
|
|
|
52
|
|
|
def get_details(self): |
53
|
|
|
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
54
|
|
|
parameter = {'id': self.session_id, 'api_key': self.parameter['api_key']} |
55
|
|
|
payload = json.dumps(parameter) |
56
|
|
|
r = requests.get(self.api_url + 'runners/get_details/', data=payload, headers=headers) |
57
|
|
|
r.raise_for_status() |
58
|
|
|
return r.json() |
59
|
|
|
|
60
|
|
|
def is_completed(self, r): |
61
|
|
|
if 'status' in r: |
62
|
|
|
return r['status'] == 'completed' |
63
|
|
|
return False |
64
|
|
|
|
65
|
|
|
def wait_complete(self): |
66
|
|
|
r = self.get_status() |
67
|
|
|
while not self.is_completed(r): |
68
|
|
|
r = self.get_status() |
69
|
|
|
|
70
|
|
|
def run(self): |
71
|
|
|
r = self.create() |
72
|
|
|
while not self.is_completed(r): |
73
|
|
|
r = self.get_status() |
74
|
|
|
return self.get_details() |
75
|
|
|
|
76
|
|
|
def language(self, str): |
77
|
|
|
self.parameter.update({'language': str}) |
78
|
|
|
|
79
|
|
|
def source_code(self, str): |
80
|
|
|
self.parameter.update({'source_code': str}) |
81
|
|
|
|
82
|
|
|
def code(self, str): |
83
|
|
|
self.parameter.update({'source_code': str}) |
84
|
|
|
|
85
|
|
|
def input(self, str): |
86
|
|
|
self.parameter.update({'input': str}) |
87
|
|
|
|
88
|
|
|
def stdin(self, str): |
89
|
|
|
self.parameter.update({'input': str}) |
90
|
|
|
|
91
|
|
|
def longpoll(self, b): |
92
|
|
|
self.parameter.update({'longpoll': b}) |
93
|
|
|
|
94
|
|
|
def longpoll_timeout(self, t): |
95
|
|
|
self.parameter.update({'longpoll_timeout': t}) |
96
|
|
|
|
97
|
|
|
def dump(self): |
98
|
|
|
print(self.parameter) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
if __name__ == '__main__': |
102
|
|
|
paiza = PaizaIO() |
103
|
|
|
paiza.code('#include <iostream>\nint main() { int x = 0; ::std::cout << "hoge" << ::std::endl; }') |
104
|
|
|
print(paiza.run()) |
105
|
|
|
|