Created
April 9, 2011 06:37
-
-
Save yingtai/911192 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import random | |
import re | |
import string | |
import sys | |
import time | |
import tweepy | |
class Maze: | |
def __init__(self,width,height): | |
self.width=width;self.height=height | |
self.WALL=1;self.ROAD=0 | |
self.startX=2;self.startY=2 | |
self.goalX=height-1; self.goalY=width-4 | |
def makeMazeData(self): | |
mazeData=[[[] for j in range(0,self.height)] for i in range(0,self.width)] | |
for j in range(self.height): | |
for i in range(self.width): | |
mazeData[i][j]=self.ROAD | |
for l in range(1,self.height-1): | |
for k in range(1,self.width-1): | |
mazeData[k][l]=self.WALL | |
self.dig(self.startX,self.startY,mazeData) | |
mazeData[self.startX][self.startY-1]=self.ROAD; mazeData[self.goalX][self.goalY]=self.ROAD | |
return mazeData | |
def dig(self,px,py,mazeData): | |
f=True | |
i=0 | |
while f: | |
vector=[[0,1,0,-1],[1,0,-1,0]] | |
r=random.randint(0,3) | |
dx=vector[0][r];dy=vector[1][r] | |
if(mazeData[px+dx*2][py+dy*2]!=self.ROAD): | |
mazeData[px+dx][py+dy]=self.ROAD | |
mazeData[px+dx*2][py+dy*2]=self.ROAD | |
self.dig(px+dx*2,py+dy*2,mazeData) | |
i+=1 | |
if i>100: | |
f=False | |
def makeMazeString(self): | |
mazeData=self.makeMazeData() | |
mazeString="" | |
for j in range(self.height-2): | |
for i in range(self.width-2): | |
if mazeData[i+1][j+1]==self.ROAD: | |
mazeString+=u"□" | |
if mazeData[i+1][j+1]==self.WALL: | |
mazeString+=u"■" | |
mazeString+=u"\n" | |
return mazeString | |
def solveMaze(self,id): | |
mazeString=api.get_status(id).text | |
maze=[[[] for j in range(0,self.width-2)] for i in range(0,self.height-2)] | |
splited=re.split("(\W+)",mazeString) | |
mazeString=splited[3].split() | |
for k in range(self.height-2): | |
for l in range(self.width-2): | |
if mazeString[k][l]=="■": | |
maze[k][l]=str(self.WALL) | |
else: | |
maze[k][l]=str(self.ROAD) | |
maze1=self.solve(maze) | |
maze1String="" | |
for m in range(self.height-2): | |
for n in range(self.width-2): | |
if maze1[m][n]==str(self.WALL): | |
maze1String+="■" | |
elif maze[m][n]==str(self.ROAD): | |
maze1String+="□" | |
else: | |
maze1String+="◎" | |
maze1String+="\n" | |
return maze1String | |
#===@catupperが作った分======================= | |
def solve(self,maze): | |
INF=100 | |
t=[1,0,-1,0] | |
y=[0,1,0,-1] | |
abesi=[[INF for a in range(11)] for b in range(9)] | |
abesi[0][1]=nau=0 | |
que=[(0,1)] | |
kyori=0 | |
while 1: | |
i=len(que) | |
for a in range(i): | |
p=que.pop(0) | |
for b in range(4): | |
x=(p[0]+t[b],p[1]+y[b]) | |
if 9>x[0]>=0 and 11>x[1]>=0 and abesi[x[0]][x[1]]==INF and maze[x[0]][x[1]]=='0': | |
abesi[x[0]][x[1]]=nau+1 | |
que.append((x[0],x[1])) | |
#print que | |
if abesi[8][9]!=INF: | |
kyori=abesi[8][9] | |
maze[8][9]='2' | |
break | |
nau+=1 | |
basyo=(8,9) | |
while 1: | |
for a in range(4): | |
x=map(lambda a,b:a+b,basyo,(t[a],y[a])) | |
if 9>x[0]>=0 and 11>x[1]>=0 and abesi[x[0]][x[1]]==nau: | |
basyo=x | |
nau-=1 | |
maze[x[0]][x[1]]='2' | |
break | |
if nau==0: | |
break | |
maze[0][1]='2' | |
return maze | |
#===ここまで================================== | |
class Twitter: | |
def __init__(self): | |
self.newMentions=[-1]*20 | |
self.newTL=[-1]*20 | |
self.unstop=True | |
def refollow_remove(self): | |
fol=set(api.followers_ids()) | |
fri=set(api.friends_ids()) | |
print "follower:"+fol,"friends:"+fri | |
followlist=fol-fri | |
for i in range(len(followlist)): | |
api.create_friendship(list(followlist)[i]) | |
removelist=fri-fol | |
for j in range(len(removelist)): | |
api.destroy_friendship(list(removelist)[j]) | |
def research(self): | |
self.researchMentions() | |
self.researchTL() | |
def monitoringMentions(self): | |
for k in range(20): | |
self.newMentions[k]=-1 | |
mentions=api.mentions() | |
mPath=u'C:\Twitter_bot\Database\mentions.txt' | |
readMentions = open(mPath) | |
readMentionid=readMentions.readlines() | |
readMentions.close() | |
if long(readMentionid[0])!=mentions[0].id: | |
for i in range(len(mentions)): | |
if long(readMentionid[0])==mentions[i].id: | |
for j in range(i): | |
self.newMentions[j]=mentions[j].id | |
writeMentions=open(mPath,'w') | |
for k in range(len(mentions)): | |
writeMentions.write(str(mentions[k].id)) | |
writeMentions.write("\n") | |
writeMentions.close() | |
def monitoringTimeLine(self): | |
TL=api.friends_timeline() | |
tPath='C:\Twitter_bot\Database\\timeline.txt' | |
readTL=open(tPath) | |
readTLid=readTL.readlines() | |
readTL.close() | |
if long(readTLid[0])!=TL[0].id: | |
for i in range(len(TL)): | |
if long(readTLid[0])==TL[i].id: | |
for j in range(i): | |
self.newTL[j]==TL[j].id | |
writeTL=open(tPath,'w') | |
for k in range(len(TL)): | |
writeTL.write(str(TL[k].id)) | |
writeTL.write("\n") | |
writeTL.close() | |
def researchMentions(self): | |
self.monitoringMentions() | |
newMentionsString=[""]*20 | |
for i in range(len(self.newMentions)): | |
if self.newMentions[i]!=-1: | |
statusi=api.get_status(self.newMentions[i]) | |
newMentionsString[i]=statusi.text | |
print u'mentions調べてる:' ,newMentionsString[i] | |
# "迷路を作れ" | |
if re.search(u"迷路.*(作|書|出力)",newMentionsString[i]) != None: | |
print u'迷路を生成します' | |
api.update_status(u"@"+statusi.author.screen_name+"\n"+maze.makeMazeString(),self.newMentions[i]) | |
# "迷路解け" | |
if re.search(u".*解(け|いて|くんだ|こう)",newMentionsString[i]) != None: | |
if statusi.in_reply_to_status_id != None: | |
print u'迷路を解きます' | |
#print maze.solveMaze(statusi.in_reply_to_status_id) | |
try: | |
api.update_status(u"@"+statusi.author.screen_name+"\n"+maze.solveMaze(statusi.in_reply_to_status_id),self.newMentions[i]) | |
except IndexError: | |
api.update_status(u"@"+statusi.author.screen_name+" それ迷路やない",self.newMentions[i]) | |
else: | |
print u'解く対象が見つかりません' | |
api.update_status(u"@"+statusi.author.screen_name+" どれを?",self.newMentions[i]) | |
# "@_toppoに100個ふぁぼを贈って" | |
if re.search(u"@yingtai_bot @[a-zA-Z0-9_]+に[0-9]+個の?(fav|ふぁぼ)",newMentionsString[i])!=None: | |
print 'favを送ります(未実装)' | |
self.favbomb(newMentionsString[i],self.newMentions[i],statusi.author.screen_name) | |
# "停止しろ" | |
if re.search(u".*(停止|止ま(れ|って))",newMentionsString[i]) != None: | |
if statusi.author.screen_name=="_toppo": | |
print u'botを停止します' | |
self.unstop=False | |
def researchTL(self): | |
self.monitoringTimeLine() | |
newTLString=[""]*20 | |
for i in range(len(self.newTL)): | |
if self.newTL[i]!=-1: | |
statusi=api.get_status(self.newTL[i]) | |
newTLString[i]=statusi.text | |
print u'timeline調べてる:' ,newTLString[i] | |
# "寝る" | |
if re.search(u"([ね,寝](る|ます)|[n,N](ell|eru))",newTLString[i])!=None and not re.search(u"(し|死|[2,2]ちゃん)ね(る|ます)")!=None: | |
api.update_status(u"@"+statusi.author.screen_name+"おやすみなさい") | |
if statusi.author.screen_name=="_toppo": | |
print u'botを停止します' | |
self.unstop=False | |
# "起きた" | |
if re.search(u"(おはよ|ohayo|good morning|むくり|mkr|起きた)",newTLString[i]) != None and not re.search(u"@")!=None: | |
api.update_status(u"@"+statusi.author.screen_name+"おはようございます") | |
if re.search(u"離脱,[P,P][C,C]閉じる,出かける",newTLString[i])!=None: | |
api.update_status(u"@"+statusi.author.screen_name+"いってらっしゃい") | |
if re.search(u"(きたく|帰宅|ただいま)",newTLString[i])!=None: | |
api.update_status(u"@"+statusi.author.screen_name+"おかえり") | |
def favbomb(self,newMentionsString,in_reply_to_id,sponsor): | |
splited=re.split(u'(\W+)',newMentionsString) | |
userid=splited[4];number=splited[6] | |
print userid,"に",number,"個" | |
usersTL=api.user_timeline | |
for p in tweepy.Cursor(usersTL,id=userid).items(number): | |
api.create_favorite(p.id) | |
print p.id,'faved' | |
api.update_status(sponser+"の代行として"+number+"個のfavを@"+userid+"に贈りました") | |
#===================================== | |
consumer_key = '' | |
consumer_secret = '' | |
access_key = '' | |
access_secret = '' | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_key, access_secret) | |
api = tweepy.API(auth_handler=auth) | |
#===================================== | |
maze=Maze(13,11) | |
twit=Twitter() | |
""" | |
sleepsec…通常処理の間隔(秒) | |
refollowmin…フォロー、リムーブ返し処理の間隔(分) | |
""" | |
sleepsec=120 | |
refollowmin=60 | |
count=0 | |
if __name__ == "__main__": | |
while twit.unstop: | |
sys.stdout.write(u'スリープ中') | |
for i in range(5): | |
time.sleep(sleepsec/10);sys.stdout.write(u'.');time.sleep(sleepsec/10) | |
print('') | |
print u'起動' | |
print u'新着ツイートチェック' | |
try: | |
twit.research() | |
except tweepy.error.TweepError: | |
print'API制限にかかっているようです(tweepy.error.TweepError)' | |
count=count+1 | |
if count>=refollowmin*60/sleepsec: | |
print u'フォロー、リムーブ返し処理' | |
twit.refollow_remove();count=0 | |
print u'終了します' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment