Last active
December 15, 2015 08:39
-
-
Save penguin2716/5232390 to your computer and use it in GitHub Desktop.
mikutterでPythonなプラグインを作ろう
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 python | |
#-*- coding: utf-8 -*- | |
import sys | |
class Message: | |
def to_bool(self, string): | |
if string == 'True': | |
return True | |
elif string == 'False': | |
return False | |
else: | |
raise 'This string is not exactly "True" or "False"' | |
def __init__(self, message, idname, favorited): | |
self.exec_code = '' | |
self.message = message | |
self.idname = idname | |
self.favorited = self.to_bool(favorited) | |
def add_code(self, code): | |
self.exec_code += code + "\n" | |
def favorite(self, status = True): | |
if status == True and self.favorited == False: | |
self.add_code('m.favorite(true)') | |
elif status == False and self.favorited == True: | |
self.add_code('m.favorite(false)') | |
def reply(self, message): | |
code = 'Service.primary.post :message => \'%s\', :replyto => m' % ('@' + self.idname + ' ' + message) | |
self.add_code(code) | |
def retweet(self): | |
if status == True: | |
self.add_code('m.retweet') | |
def post(self, message): | |
self.add_code("Service.primary.post :message => '" + message + "'") | |
if __name__ == '__main__': | |
m = Message(sys.argv[1], sys.argv[2], sys.argv[3]) | |
m.favorite() | |
m.reply("★") | |
print m.exec_code, |
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
# -*- coding: utf-8 -*- | |
Plugin.create :exec_python do | |
command(:exec_python, | |
name: 'Pythonのコードを実行', | |
icon: File.expand_path(File.join(File.dirname(__FILE__), 'python-icon.png')), | |
condition: lambda{ |opt| opt.messages.size == 1 }, | |
visible: true, | |
role: :timeline) do |opt| | |
begin | |
m = opt.messages.first | |
python_source = File.expand_path(File.join(File.dirname(__FILE__), 'message.py')) | |
message = m[:message] | |
idname = m.idname | |
favorited = m.favorite? ? 'True' : 'False' | |
exec_code = "python #{python_source} '#{message}' #{idname} #{favorited}" | |
Kernel.instance_eval(`#{exec_code}`) | |
rescue Exception => e | |
Plugin.call(:update, nil, [Message.new(message: "むりでした", system: true)]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment