LoginSignup
0
0

More than 5 years have passed since last update.

Mastodonで呟いたソースコードをwandboxで実行できるようにしてみた

Posted at

はじめに

TwitterでPaizaさんが提供されてるソースコードを実行するものをMastodonでもやってみたくて試したものになります。

なお、C++のソースコードを実行したパターンしか試していないので他な言語の場合は想定していないです。

実際のコード

Gemfile
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'mastodon-api', git: 'https://github.com/tootsuite/mastodon-api.git', ref: '189deb8'
gem 'dotenv'
gem 'wandbox'
wandbox.rb
require 'bundler/setup'
Bundler.require(:default)

require 'open3'
require 'mastodon'

stream = Mastodon::Streaming::Client.new(base_url: <MastodonURL>, bearer_token: <アクセストークン>)
client = Mastodon::REST::Client.new(base_url: <MastodonURL>, bearer_token: <アクセストークン>)

URL = <MastodonURL>
name = <MastodonのアカウントID>

stream.user() do |toot|
    if toot.content.to_s =~ /wandbox/ then
        str = toot.content.to_s
        str.gsub!(/<\/p><p>/, '<br>')
        str.gsub!(/<p>|<\/p>|wandbox|/, '')
        str.gsub!(/<a href=\"<MastodonのURL>\/tags\/include\" class=\"mention hashtag\" rel=\"tag\">|<span>|<\/span>|<\/a>/, '')
        str.gsub!(/<br>|<br \/>/, '\n')
        str.gsub!(/&lt;/, '<')
        str.gsub!(/&gt;/, '>')
        str.gsub!(/&quot;/, '"')
        str.gsub!(/&amp;/, '&')

        source = str.split('\n')

        File.open("wandbox.cpp", "w+") do |file|
            source.each do |line|
                file.puts(line)
            end
        end

        s = Open3.capture3("wandbox run wandbox.cpp --compiler=clang-head")
        p s[0]

        response = client.create_status("[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ\n#{s[0]}")
    end
end

ソースコードはこれだけ。

やったことといえばTootの成型とOpen3.capture3でwandboxでの実行結果を受け取ってるくらい。

おわりに

これうまいこと使えばソースコードを実行できるBotとか作れそうとか思った。
そこまでの気力はないので、だれか作らないかなぁ......|д゚)

やるならQiitadon辺りで実装すると面白いんではないかな?

参考資料

CLI から Wandbox を利用してソースファイルを実行する gem をつくった
Rubyで外部コマンドを実行して結果を受け取る方法あれこれ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0