Skip to content

Instantly share code, notes, and snippets.

@vim13
Last active September 23, 2015 19:18
Show Gist options
  • Save vim13/603434 to your computer and use it in GitHub Desktop.
Save vim13/603434 to your computer and use it in GitHub Desktop.
'''機能一覧(Replyに反応)'''
おやすみ
oyasu = re.compile(u'おやすみ?|(寝|ね)る|グ(ン|ッド?)(ナイト?)|(眠|ねむ)い|[スヤリ]{3}|バタリ')
おはよう
oha = re.compile(u'おっはー|おはよ?う?|(起|お)き(まし|た)|起床|[ムクリ]{3}|朝')
エロイ質問
shimo = re.compile(u'(下|シモ)ネタ|エロ|(質問|しつもん)あ(る|り|ん)(.*?)(\?|?)|セク(シャル)?ハラ')
ネコ語
nya = re.compile(u'にゃ(ー|ん)|ニャ(ー|ン)|(みゃー|ミャー)|(猫|ぬこ|ネコ|ねこ)|(∧|\^)(_|_)+(∧|\^)|(マー?オ|まー?お)')
大丈夫だ、問題ない
elsha = re.compile(u'(大丈夫|だいじょう?ぶ)か|トルノデス|トルノデス|(話|はなし)をしよう|イーノ(ック)?|今回も(ダメ|だめ|駄目)')
おすすめ記事
gugure = re.compile(u'ページ|記事|UR(L|I)|サイト', re.I)
おすすめレシピ
cook = re.compile(u'(腹)(へ|減)|空腹|(食|喰)(わ|べ)|お(腹|なか)(空|す|へ|減)|献立|レシピ')
おすすめ映画
cinema = re.compile(u'映画|ムービー|(洋|邦)画|金曜ロード|(TSUTAYA|ツタヤ)|DVD|ビデオ', re.I)
平仮名変換
elif u'ぁ' <= hiragana <= u'ん':
その他
悪口
#!/usr/lib/python
#vim:fileencoding=utf-8
import time
import re
import random
import twitter
import yaml
import moyashi
import cookpad
import cinema
import google
class Sasazuka:
def __init__(self, consumer_key, consumer_secret, access_token_key, access_token_secret,
id_file, reply_file, reply2_file, oyasu_file, oha_file, nya_file,
elsha_file, gugure_file,
appid):
self.api = twitter.Api(consumer_key, consumer_secret, access_token_key, access_token_secret)
self.id_file = id_file
self.reply_file = reply_file
self.reply2_file = reply2_file
self.oyasu_file = oyasu_file
self.oha_file = oha_file
self.nya_file = nya_file
self.elsha_file = elsha_file
self.gugure_file = gugure_file
self.appid = appid
def readFile(self):
idfile = open(self.id_file, 'r')
latest_id = idfile.readline()
idfile.close()
return latest_id
def writeFile(self, status_id):
idfile = open(self.id_file, 'w')
idfile.write(str(status_id))
idfile.close()
def judgeReply(self, text):
oyasu = re.compile(u'おやすみ?|(寝|ね)る|グ(ン|ッド?)(ナイト?)|(眠|ねむ)い|[スヤリ]{3}|バタリ')
oha = re.compile(u'おっはー|おはよ?う?|(起|お)き(まし|た)|起床|[ムクリ]{3}')
shimo = re.compile(u'(下|シモ)ネタ|エロ|(質問|しつもん)あ(る|り|ん)(.*?)(\?|?)|セク(シャル)?ハラ')
nya = re.compile(u'にゃ(ー|ん)|ニャ(ー|ン)|(みゃー|ミャー)|(猫|ぬこ|ネコ|ねこ)|(∧|\^)(_|_)+(∧|\^)|(マー?オ|まー?お)')
elsha = re.compile(u'(大丈夫|だいじょう?ぶ)か|トルノデス|トルノデス|(話|はなし)をしよう|イーノ(ック)?|今回も(ダメ|だめ|駄目)')
gugure = re.compile(u'ページ|記事|UR(L|I)|サイト', re.I)
cook = re.compile(u'(朝|昼|夕|夜)|(食|飯|メシ|めし)|(腹)(へ|減)|空腹|(食|喰)(い|わ|べ)|お(腹|なか)(空|す|へ|減)|献立|レシピ|肴|つまみ|ツマミ')
cinema = re.compile(u'映画|ムービー|(洋|邦)画|金曜ロード|(TSUTAYA|ツタヤ)|DVD|ビデオ', re.I)
hiraga = re.compile(u'^[ぁ-ん]+$')
hiragana = re.sub(u',|、|。|\.|@(.*?) ', '', text)
if oyasu.search(text) != None:
return 'oyasu'
elif oha.search(text) != None:
return 'oha'
elif shimo.search(text) != None:
return 'shimo'
elif nya.search(text) != None:
return 'nya'
elif elsha.search(text) != None:
return 'elsha'
elif gugure.search(text) != None:
return 'gugure'
elif cook.search(text) != None:
return 'cook'
elif cinema.search(text) != None:
return 'cinema'
elif hiraga.search(hiragana) != None:
return 'hiraga'
else:
return None
def makeSentence(self, text, name):
flag = self.judgeReply(text)
tweet = ''
if flag == 'oyasu':
tweet = self.makeGreet(self.oyasu_file)
elif flag == 'oha':
tweet = self.makeGreet(self.oha_file)
elif flag == 'shimo':
tweet = self.makeShimo()
elif flag == 'nya':
tweet = self.makeNya()
elif flag == 'elsha':
tweet = self.makeGreet(self.elsha_file)
elif flag == 'gugure':
tweet = self.makeURI(self.gugure_file, name)
elif flag == 'cook':
tweet = self.makeCook()
elif flag == 'cinema':
tweet = self.makeCinema()
elif flag == 'hiraga':
tweet = self.makeHenkan(re.sub('\.|@(.*?) ', '',text))
else:
tweet = self.makeNormal()
return tweet
def makeNormal(self):
usrfile = open(self.reply_file, 'r').readlines()
usrfile2 = open(self.reply2_file, 'r').readlines()
sentence1 = [user[:-1] for user in usrfile]
sentence2 = [user[:-1] for user in usrfile2]
tweet = random.choice(sentence1) + random.choice(sentence2)
return tweet
def makeGreet(self, greetFile):
usrfile = open(greetFile, 'r').readlines()
sentence = [user[:-1] for user in usrfile]
tweet = random.choice(sentence)
return tweet
def makeURI(self, greetFile, name):
usrfile = open(greetFile, 'r').readlines()
sentence = [user[:-1] for user in usrfile]
uri = ['http://www.google.co.jp/?q={0}%20{1}',
'http://search.yahoo.co.jp/search?p={0}%20{1}',
'http://www.bing.com/search?q={0}%20{1}']
tweet = random.choice(uri).format(name, random.choice(sentence))
return tweet
def makeNya(self):
usrfile = open(self.nya_file, 'r').readlines()
sentence = [user[:-1] for user in usrfile]
tweet = ''
rand = random.randint(1,10)
for n in range(0, rand):
tweet += random.choice(sentence)
return tweet
def makeShimo(self):
shimo = moyashi.markovTweet(self.api, self.appid)
return shimo.main()
def makeHenkan(self, text):
api = google.Api()
res = api.ja(text.encode('utf-8'))
tweet = ''
for n in res:
tweet += random.choice(n[1])
return tweet.encode('utf-8')
def makeCook(self):
obj = cookpad.Cookpad()
url, recipe = obj.recipe()
return '{1}でも作って食ってろカス{0}'.format(url, recipe)
def makeCinema(self):
obj = cinema.Cinema()
url, title = obj.getTitle()
return '{1}でも観てろカス{0}'.format(url, title)
def makeReply(self):
latest_id = self.readFile()
replies = self.api.GetReplies(since_id = latest_id)
messages = []
if replies:
status_id = replies[0].id
self.writeFile(status_id)
for s in replies:
tweet = self.makeSentence(s.text, s.user.screen_name)
messages.append(u'.@' + s.user.screen_name.decode('utf-8') + u' ' + tweet.decode('utf-8'))
return messages
def main(self):
messages = self.makeReply()
if messages:
for s in messages:
self.api.PostUpdates(s.encode('utf-8'))
time.sleep(5)
if __name__ == '__main__':
yaml_file = open('bot.yaml', 'r').read()
yam = yaml.load(yaml_file)
consumer_key = yam['twitter.com']['consumer_key']
consumer_secret = yam['twitter.com']['consumer_secret']
access_token_key = yam['twitter.com']['access_token_key']
access_token_secret = yam['twitter.com']['access_token_secret']
id_file = yam['file']['id_file']
reply_file = yam['file']['reply_file']
reply2_file = yam['file']['reply2_file']
oyasu_file = yam['file']['oyasu_file']
oha_file = yam['file']['oha_file']
nya_file = yam['file']['nya_file']
elsha_file = yam['file']['elsha_file']
gugure_file = yam['file']['gugure_file']
appid = yam['yahoo.co.jp']['appid']
obj = Sasazuka(consumer_key, consumer_secret, access_token_key, access_token_secret,
id_file, reply_file, reply2_file, oyasu_file, oha_file, nya_file,
elsha_file, gugure_file,
appid)
obj.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment