Skip to content

Instantly share code, notes, and snippets.

@kb10uy
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save kb10uy/2b32d8a4ecac912c317f to your computer and use it in GitHub Desktop.

Select an option

Save kb10uy/2b32d8a4ecac912c317f to your computer and use it in GitHub Desktop.
Kbtter4用 Base64関係プラグイン
# coding: utf-8
# Base64プラグイン
# ・Base64と思われるツイートの自動デコード
# ・Base64エンコード・デコード・ツイートコマンド
# ・それぞれ"b64enc","b64dec","b64tweet"で、
# ・textパラメータに文字列を指定することで使用できます
import clr
from System import *
from System.Collections.Generic import *
from System.Text import *
from System.Text.RegularExpressions import *
from System.IO import *
clr.AddReferenceByPartialName("CoreTweet")
from CoreTweet import *
from CoreTweet.Streaming import *
Name="Base64コマンド・Base64ツイート自動デコード"
k=Kbtter4.Instance
utf8=Encoding.GetEncoding("UTF-8")
sjis=Encoding.GetEncoding("shift-jis")
eucj=Encoding.GetEncoding("euc-jp")
def Decode(str,enc):
return enc.GetString(Convert.FromBase64String(str))
def OnStatusDestructive(s):
st=s.Status
match=Regex.Match(st.Text,"[a-zA-Z0-9\\+/=]+")
if match.Success==True :
os=st.Text
us=Decode(st.Text,utf8)
ss=Decode(st.Text,sjis)
es=Decode(st.Text,eucj)
st.Text="<<BASE64のツイートでした>>\n"
st.Text+="UTF-8:\""+us+"\"\n"
st.Text+="Shift-JIS:\""+ss+"\"\n"
st.Text+="EUC-JP:\""+es+"\"\n"
st.Text+="元ツイート:\n"+os
st=s.Status.RetweetedStatus
match=Regex.Match(st.Text,"[a-zA-Z0-9\\+/=]+")
if match.Success==True :
os=st.Text
us=Decode(st.Text,utf8)
ss=Decode(st.Text,sjis)
es=Decode(st.Text,eucj)
st.Text="<<BASE64のツイートでした>>\n"
st.Text+="UTF-8:\""+us+"\"\n"
st.Text+="Shift-JIS:\""+ss+"\"\n"
st.Text+="EUC-JP:\""+es+"\"\n"
st.Text+="元ツイート:\n"+os
return s
def Encode(str):
return Convert.ToBase64String(utf8.GetBytes(str))
def CommandBase64Encode(args):
return Encode(args["text"])
def CommandBase64Decode(args):
ret= "UTF-8 : "+Decode(args["text"],utf8)+"\n"
ret+= "Shift-JIS : "+Decode(args["text"],sjis)+"\n"
ret+= "EUC-JP : "+Decode(args["text"],eucj)+"\n"
return ret
def Base64Tweet(args):
str=args["text"]
et=Convert.ToBase64String(utf8.GetBytes(str))
if (et.Length>140):
et=Convert.ToBase64String(sjis.GetBytes(str))
prm=Dictionary[String,Object]()
prm["status"]=et
k.Token.Statuses.Update(prm)
b64enc=Kbtter4.CreateCommand()
b64enc.Name="b64enc"
b64enc.Description="Base64エンコードします。"
b64enc.Function=CommandBase64Encode
b64enc_text=Kbtter4.CreateCommandParameter("text",True)
b64enc.Parameters.Add(b64enc_text)
Kbtter4.AddCommand(b64enc)
b64dec=Kbtter4.CreateCommand()
b64dec.Name="b64dec"
b64dec.Description="Base64デコードします。"
b64dec.Function=CommandBase64Decode
b64dec_text=Kbtter4.CreateCommandParameter("text",True)
b64dec.Parameters.Add(b64dec_text)
Kbtter4.AddCommand(b64dec)
b64tweet=Kbtter4.CreateCommand()
b64tweet.Name="b64tweet"
b64tweet.Description="Base64エンコードしてつぶやきます。\n基本的にはUTF-8で、文字数が足らなかったらShift-JISでエンコードします。"
b64tweet.Function=Base64Tweet
b64tweet_text=Kbtter4.CreateCommandParameter("text",True)
b64tweet.Parameters.Add(b64tweet_text)
Kbtter4.AddCommand(b64tweet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment