Created
March 30, 2014 20:21
-
-
Save emad-elsaid/9879163 to your computer and use it in GitHub Desktop.
Execute code online and return output
this script is part of my quest to create a facebook bot for executing code and respond with code output in another comment, this is similar to a reddit bot. the first step is to submit code and get the output or error from a service online, i found that Eval.in is simple and free and supports json formatted…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Author : Emad Elsaid (https://github.com/blazeeboy) | |
require 'json' | |
require 'open-uri' | |
require 'uri' | |
require 'net/http' | |
CODE_LIMIT = 10 | |
$url = "https://eval.in/" | |
$languages = { | |
'c' => 'c/gcc-4.7.2', 'c++' => 'c++/gcc-4.7.2', | |
'fortran' => 'fortran/f95-4.4.3', 'haskell' => 'haskell/hugs98-sep-2006', | |
'io' => 'io/io-20110912', 'javascript' => 'javascript/node-0.10.26', | |
'lua' => 'lua/lua-5.2.1', 'ocaml' => 'ocaml/ocaml-4.00.1', | |
'php' => 'php/php-5.5.1', 'perl' => 'perl/perl-5.16.1', | |
'python' => 'python/cpython-3.2.3', 'ruby' => 'ruby/mri-2.1.0', | |
'slash' => 'slash/slash-head', 'assembly' => 'assembly/nasm-2.07' | |
} | |
def evalin code, language | |
return nil unless $languages.keys.include? language | |
return nil if code.length < CODE_LIMIT | |
params = { | |
code: code, lang: $languages[language], | |
execute: 'on', input: '' | |
} | |
postData = Net::HTTP.post_form(URI.parse($url), params) | |
location = postData['location'] | |
if location | |
evalin_data = JSON.parse open("#{location.gsub 'http://', 'https://'}.json").read | |
return "#{evalin_data['output']}\n#{evalin_data['status']}\n#{location}" | |
end | |
return nil | |
end | |
puts evalin('10.times{ puts "done."}', 'ruby') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you sure CODE_LIMIT is minimal length?