Ideone APIを使ってコマンドラインからコードを公開

Ideone.comでAPIが公開されてるから使ってみようと思ってたので試してみる.

Ideone.com APIはこちら → http://ideone.com/api

最後のコードを綺麗にしたやつ → http://d.hatena.ne.jp/nise_nabe/20101216/1292431545
サンプルとしていくつかあったのでPythonを選択.

これがサンプルコード.

#!/usr/bin/env python 
"""
ideone.com
API sample

This script shows how to use ideone api.
"""

from SOAPpy import WSDL

# creating wsdl client
wsdlObject = WSDL.Proxy('http://ideone.com/api/1/service.wsdl')

# calling test method
response = wsdlObject.testFunction('test','test')

# printing returned values
for item in response['item']:
        print item

WSDLという何かを用いているらしい.これが何なのかは良く分かってない.
http://ideone.com/api/1/service.wsdlの中では
・createSubmission
・getSubmissoinStatus
・getSubmissionDetails
・getLanguages
・testFunction
というメソッド群が定義されている.
で,上記のサンプルコードではtestFunctionというものが使われている.これはユーザ名とAPIのパスワードを引数として与えてIdeone.comのAPIが正しく使えるかどうかを試すメソッドのようで.これが

<SOAPpy.Types.structType item at 146251820>: {'value': 'OK', 'key': 'error'}
<SOAPpy.Types.structType item at 146252268>: {'value': 'ideone.com', 'key': 'moreHelp'}
<SOAPpy.Types.structType item at 146252300>: {'value': 3.1400000000000001, 'key': 'pi'}
<SOAPpy.Types.structType item at 146252716>: {'value': 42, 'key': 'answerToLifeAndEverything'}
<SOAPpy.Types.structType item at 146252684>: {'value': 1, 'key': 'oOok'}

というような出力を取れば成功.それ以外ならユーザ名やパスワードが間違ってるかとかネットにつながってないとかideone.comが落ちてるとかいろいろあるのでチェックする.pythonの場合はSOAPpyというライブラリを用いられているようなのでインストールしておく.Ubuntuならば「sudo apt-get install python-soappy」でインストールできるはず.

それぞれのメソッドが何ができるかを軽く解説.
・createSubmission コードを送信できる.パラメータで使用言語とか入力とか走らせるかとかを指定.コードが閲覧できるURLの一部が返ってくる.
・getSubmissoinStatus 上記のcreateSubmissionではただ送っただけなのでコンパイルが成功したかとか走らせるのが成功したかMLEとかTLEとかを取得するのがこれ.
・getSubmissionDetails Web上でideone.comから閲覧できる情報を大体取得できる.
・getLanguages 使える言語のリストが帰ってくる.
・testFunction APIテスト用.

使用できる言語のリストを番号について昇順にして表示.

#!/usr/bin/env python 
from SOAPpy import WSDL

wsdlObject = WSDL.Proxy('http://ideone.com/api/1/service.wsdl')

langs = wsdlObject.getLanguages('test','test')
langs = sorted(langs.item[1][1].item,key=lambda x:int(x[0]))
for index,lang in langs:
        print index,lang

実行結果

1 C++ (gcc-4.3.4)
2 Pascal (gpc) (gpc 20070904)
3 Perl (perl 5.12.1)
4 Python (python 2.6.4)
5 Fortran (gfortran-4.3.4)
6 Whitespace (wspace 0.3)
7 Ada (gnat-4.3.2)
8 Ocaml (ocamlopt 3.10.2)
9 Intercal (c-intercal 28.0-r1)
10 Java (sun-jdk-1.6.0.17)
11 C (gcc-4.3.4)
12 Brainf**k (bff-1.0.3.1)
13 Assembler (nasm-2.07)
14 CLIPS (clips 6.24)
15 Prolog (swi) (swipl 5.6.64)
16 Icon (iconc 9.4.3)
17 Ruby (ruby-1.9.2)
19 Pike (pike 7.6.86)
21 Haskell (ghc-6.8.2)
22 Pascal (fpc) (fpc 2.2.0)
23 Smalltalk (gst 3.1)
25 Nice (nicec 0.9.6)
26 Lua (luac 5.1.4)
27 C# (mono-2.8)
28 Bash (bash 4.0.35)
29 PHP (php 5.2.11)
30 Nemerle (ncc 0.9.3)
32 Common Lisp (clisp) (clisp 2.47)
33 Scheme (guile) (guile 1.8.5)
34 C99 strict (gcc-4.3.4)
35 JavaScript (rhino) (rhino-1.6.5)
36 Erlang (erl-5.7.3)
38 Tcl (tclsh 8.5.7)
39 Scala (scala-2.8.0.final)
45 Assembler (gcc-4.3.4)
54 Perl 6 (rakudo-2010.08)
62 Text (text 6.10)
101 Visual Basic .NET (mono-2.4.2.3)
102 D (dmd) (dmd-2.042)
104 AWK (gawk) (gawk-3.1.6)
105 AWK (mawk) (mawk-1.3.3)
106 COBOL 85 (tinycobol-0.65.9)
107 Forth (gforth-0.7.0)
108 Prolog (gnu) (gprolog-1.3.1)
110 bc (bc-1.06.95)
111 Clojure (clojure 1.1.0)
112 JavaScript (spidermonkey) (spidermonkey-1.7)
114 Go (gc-2010-07-14)
115 Unlambda (unlambda-2.0.0)
116 Python 3 (python-3.1.2)
117 R (R-2.11.1)
118 COBOL (open-cobol-1.0)
119 Oz (mozart-1.4.0)
121 Groovy (groovy-1.7)
122 Nimrod (nimrod-0.8.8)
123 Factor (factor-0.93)
124 F# (fsharp-2.0.0)
125 Falcon (falcon-0.9.6.6)

引数のファイルを送信.

#!/usr/bin/env python 
import sys
import os.path
from SOAPpy import WSDL

#第一引数にソースコードのパス,第二引数に入力ファイルのパスを渡されているものとする
argv=sys.argv
argc=len(argv)

user = 'test'
paswd = 'test'

exttype = {'.cpp':1, '.java':10, '.c':11, '.tcl':38, '.go':114}

srcfile = argv[1]
if argc>2:
	inputfile = argv[2]
else:
	inputfile = ''

srcext = os.path.splitext(srcfile)[1]
if srcext in exttype:
	langId = exttype[srcext]
else:
	langId = 62 # langtype :text

srcstr = open(srcfile).read()
if os.path.isfile(inputfile):
	inputstr = open(inputfile).read()
else:
	inputstr = ''

wsdlObject = WSDL.Proxy('http://ideone.com/api/1/service.wsdl')
# createSubmission(ユーザ名, APIパスワード,ソースコード, 使用言語(上の結果の数値部分), 入力, 走らせるか, プライベートか)
# 62はただのText
response = wsdlObject.createSubmission(user,paswd,srcstr,langId,inputstr,True,True)

link = response.item[1][1]

print 'http://ideone.com/'+ link

# ステータスがdone==0になるまで待ち.(ドキュメントによると3s〜5s待てとのこと)
# wating for compilation <0, done 0, compilation 1, running 3
status = 1
while status:
	print 'wating...'
	response = wsdlObject.getSubmissionStatus(user,paswd,link)
	status = response.item[1][1]
	result = response.item[2][1]

print 'finish'
resulttype = {0:'not running', 11:'compilation error', 12:'runtime error', 13:'time limit exceeded', 15:'success', 17:'memory lmit exceeded', 19:'illegal system call', 20:'internal error'}
print resulttype[result]

# 標準出力,標準エラー出力を取得
response = wsdlObject.getSubmissionDetails(user,paswd,link,False,False,True,True,False)

print "stdout:"
print response.item[11].value
print "stderr:"
print response.item[12].value

現状はコメントつけられないタグつけられない使えたらいいですね終わり.