壊れた図bot をつくった
図bot という最高の手動bot がありますが これがグリッチしたら最高じゃんと思ってつくって運用していつのまにかフォロワーが500を超えたので紹介します
まあふつうに twitter の bot + glitch という感じなので特に言うことないのですが
グリッチに関しては以下を
安全に glitch できるようにした - >>> (trs is snobbishness) == True
自動投稿に関してはよくわからなかったので crontab をつかいました
本体
#! usr/bin/python # -*- coding: utf-8 -*- from config import * import glitch.glitch as glitch import twitter import urllib import base64 import pickle import os import glob import random from replyPattern import replyPattern class TwitterClient(object): def __init__(self): # config に置いたキーから oauth 設定 self.t = twitter.Twitter( auth=twitter.OAuth( OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET )) # 図bot から画像取得 def get_user_tweets(self, screen_name="zu__bot", counts=1): ut = self.t.statuses.user_timeline(screen_name=screen_name, counts=counts) try: media_url = ut[0]["entities"]["media"][0]["media_url"] status_id = ut[0]["id_str"] return (media_url, status_id) except KeyError: return ut[0]["text"] def get_timeline(self): return self.t.statuses.home_timeline()[0]["text"] def post_tweet(self, msg=None): self.t.statuses.update(status=msg) #データ付きの投稿 def post_with_media(self, data=None, msg="", reply=None): try: params = {"media[]": data, "status": msg, "in_reply_to_status_id": reply } self.t.statuses.update_with_media(**params) return True except: return False def latest_status_id(self, latestid): with open("latestStatusID", "rb") as f: try: lastid = pickle.load(f) except (pickle.UnpicklingError, EOFError): lastid = None if latestid != lastid: with open("latestStatusID", "wb") as w: pickle.dump(latestid, w) return True else: return False #再破壊 def get_reply(self, screen_name="kowaretazu_bot", counts=1): ut = self.t.statuses.mentions_timeline(screen_name=screen_name, counts=counts) if ut: status = ut[0]["text"] reply_id = ut[0]["id_str"] return (status, reply_id) else: return None def reply_checker(self, status): status = status.lstrip("@kowaretazu_bot").lstrip().lstrip("@zu__bot").lstrip() if status in replyPattern: return True else: return False def latest_reply_id(self, replyid): with open("latestReplyID", "rb") as f: try: lastid = pickle.load(f) except (pickle.UnpicklingError, EOFError): lastid = None if replyid != lastid: with open("latestReplyID", "wb") as w: pickle.dump(replyid, w) return True else: return False #破壊 class GlitchClient(object): def __init__(self, url): self.savepath = url.lstrip("http://bs.twimg.com/media/") urllib.request.urlretrieve(url, self.savepath) self.glitched_image = "glitched{0}.jpg".format(random.randint(0, 62)) def glitch(self): glitch.glitch(self.savepath, max=True) def image_cleanupper(self): names = glob.glob("*.jpg") names.remove(self.savepath) names.remove(self.glitched_image) list(map(os.remove, names)) def b64_to_utf8_decoder(self, filename): with open(filename, "rb") as f: return base64.b64encode(f.read()).decode("utf8", "ignore") #キーワードきたら破壊 def onemore(t=None): reglitch = t.get_reply() try: if t.reply_checker(reglitch[0]): return t.latest_reply_id(reglitch[1]) except (TypeError): pass return False def glitch_machine(zubot=None, t=None): g = GlitchClient(zubot[0]) g.glitch() data = g.b64_to_utf8_decoder(g.glitched_image) if t.post_with_media(data, "@zu__bot", zubot[1]): g.image_cleanupper() def main(): try: t = TwitterClient() zubot = t.get_user_tweets() if onemore(t): glitch_machine(zubot, t) return if not t.latest_status_id(zubot[1]): return glitch_machine(zubot, t) #なんかエラー起きたら投稿 except: t = TwitterClient() t.post_tweet("Sorry, 壊れた図_bot has been broken.") if __name__ == '__main__': main()
replyPattern = {"one more", "もっかい", "壊して" }
よければ楽しんでください