This repository was archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.py
119 lines (101 loc) · 3.06 KB
/
twitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MeCab
import tweepy
import sys
sys.path.append('./kana2boin.py')
import kana2boin
import urllib
import urllib2
import json
import random
import time
# 読みのみを抽出するためのMecabTaggerインスタンスの生成
mecab = MeCab.Tagger('-Oyomi')
# TWITTER API AUTH CLIENT
def get_oauth():
consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
return auth
def client_with_auth():
auth = get_oauth()
client = tweepy.API(auth_handler=auth)
return client
# Twitterのトレンドワードを10件取得
def get_trends(api, woeid):
place = api.trends_place(id=woeid)
value = place[0][u'trends']
trends = []
for word in value:
result = word[u'query'] #ワードの抽出
result = urllib2.unquote(result).encode('raw_unicode_escape').decode('utf-8') #日本語にデコード
result = result.replace("#","") #ハッシュタグ記号の削除
result = mecab.parse(result.encode("utf-8"))
trends.append(result)
return trends
TEXT = "Im Rhyme Bot n Vegitarian!! I eat salad n it\'s Better than Ramadan. バーガーばっかのお前はバーカー? なら俺たちはラッパーサーバー YEAH"
def tweet(text=TEXT):
client = client_with_auth()
try:
client.update_status(text)
except tweepy.error.TweepError as e:
print e
exit()
f = open('/home/s14178so/twitter_bot/dict.json', 'r')
dic = json.load(f)
wd = open('/home/s14178so/twitter_bot/word_dic.json', 'r')
word_dic = json.load(wd)
def search_index(boin):
ids = []
cuts = []
rest = boin
length = len(boin)
while length > 0:
if rest in dic:
random_i = random_index(dic[rest])
ids.append(random_i)
if len(cuts) > 0:
rest = ''.join(cuts)
length = len(rest)
cuts = []
else:
break
else:
cuts.insert(0, rest[len(rest)-1])
rest = rest[0:len(rest)-1]
length = len(rest)
return ids
def search_words(ids):
words = []
for i in ids:
words.append(word_dic[i]["word"])
text = ('').join(words)
return text
def random_index(arr_index):
i = random.choice(arr_index)
return i
def main():
auth = get_oauth()
api = tweepy.API(auth_handler=auth)
if len(sys.argv) >= 2:
woeid = int(sys.argv[1])
else:
woeid = 23424856
try:
trends = get_trends(api, woeid)
boins = kana2boin.boin_boin(trends)
for i, boin in enumerate(boins):
if len(boin) >= 1 :
ids = search_index(boin)
rhyme_word = search_words(ids)
text = '{0}と{1}で踏もうとする。やっちゃってる!?'.format(trends[i].strip(), rhyme_word.encode("utf-8"))
tweet(text)
time.sleep(1)
except tweepy.error.TweepError as e:
print e
exit()
main()