-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_func.py
226 lines (186 loc) · 8.79 KB
/
tweet_func.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- coding: utf-8 -*-
import tweepy
import datetime
import config
import urllib
import info_convert
import MySQLdb
import traceback
import utils
from PIL import Image, ImageDraw, ImageFont
# TwitterのAPI_TOKEN
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
AS = config.ACCESS_TOKEN_SECRET
TWITTER_ID = config.TWITTER_ID
# タイムゾーン指定
JST = datetime.timezone(datetime.timedelta(hours=+9), 'JST')
conn = MySQLdb.connect(
host='localhost', user=config.DATABASE_USER, passwd=config.DATABASE_PASS, db=config.DATABASE_NAME,
charset='utf8mb4')
# TwitterAPI認証用関数
def auth_twitter():
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)
api = tweepy.API(auth, wait_on_rate_limit=True) # API利用制限にかかった場合、解除まで待機する
return api
# 運動記録をツイッター上から検索し、データベースに追加する, フォローしてくれている人にはリプライする。
def search_exercise_data(api, max_number=300,interrupt=True):
cur = conn.cursor()
# フォローしてくれている人を取得
follower_id = api.followers_ids()
for tweet in tweepy.Cursor(api.search, q=f'#リングフィットアドベンチャー -filter:retweets filter:images',tweet_mode="extended").items(max_number):
print(tweet.full_text)
# idが重複していたら、すでにそこまで検索してあるので中断
cur.execute("select count(*) from Exercise where tweet_id = %s", (tweet.id,))
if int(cur.fetchone()[0]) :
if interrupt: return
else: continue
# imgがリングフィットのものでなければcontinue
image_type = fetch_image(tweet)
if image_type is None: continue
try:
# 画像から運動記録を読み取る
exercise_data = info_convert.image_to_data(image_type)
print(exercise_data.cal)
# DBに運動記録を追加
params = (exercise_data.cal,tweet.user.name,tweet.user.screen_name,tweet.id,tweet.created_at+ datetime.timedelta(hours=9))
cur.execute(
"insert into Exercise (kcal,user_name,user_screen_name,tweet_id,tweeted_time) "
"values (%s,%s,%s,%s,%s) ",params
)
conn.commit()
# もしフォローしてくれている人なら、順位を呟く
if tweet.user.id in follower_id:
print(tweet.user.screen_name," さんにお返事します")
reply_exercise_result(api,cur,exercise_data,tweet)
except tweepy.error.TweepError:
traceback.print_exc()
utils.send_mail("Tweepy has occurred.", traceback.format_exc())
# @{TWITTER_ID}へのリプに対し、順位を返信する。
# TODO:開発中/まだ使えません
def reply_ranking(api,item_num=100):
for tweet in tweepy.Cursor(api.search, q=f'@{TWITTER_ID}',tweet_mode="extended").items(item_num):
print(tweet.full_text)
# ツイートに順位 が含まれているなら、順位をリプライする
if not "順位" in tweet.full_text:
return
cur = conn.cursor()
now = datetime.datetime.now(JST)
ranking_timestamp = now - datetime.timedelta(hours=4)
params = (tweet.user.id,ranking_timestamp)
# user_idからカロリーを抽出
cur.execute("SELECT kcal FROM Exercise WHERE user_id == %s AND CAST(tweeted_time - INTERVAL 4 HOUR AS DATE)=CAST(%s AS DATE)",params)
def make_ranking_picture(exercise_data_list):
# ベース画像を読み込む
im = Image.open('ranking_template.png')
draw = ImageDraw.Draw(im)
# 枠幅
W_song = 390
# ユーザ名のフォントサイズとフォント種類
font_size_ranking = 35
font_ranking = ImageFont.truetype(config.RANKING_FONT,font_size_ranking)
# 消費カロリーのフォントサイズとフォント種類
font_size_point = 25
font_point = ImageFont.truetype(config.KCAL_FONT,font_size_point)
# 標準のフォントカラー
font_color = (200, 0, 0)
# 消費カロリー用のフォントカラー
font_color_point = (150, 0, 0)
# ランキング
for i, exercise_data in enumerate(exercise_data_list):
user_name = exercise_data[0]
kcal = f"{exercise_data[1]}kcal"
# 1位から5位、6位から10位でポジションを変更
w_pos_s = 15 if i < 5 else 610
w_pos_p = 0 if i < 5 else 605
h_pos = i if i < 5 else i - 5
# 消費カロリーを描画
draw.font = font_point
draw.text((490 + w_pos_p, 170 + 99 * h_pos),
kcal, font_color_point)
# ユーザ名を描画
draw.font = font_ranking
w_song, h_song = draw.textsize(user_name)
# 枠をはみ出す場合は縮小する。
if w_song > W_song:
font_size = int(font_size_ranking * W_song / w_song)
draw.font = ImageFont.truetype(config.RANKING_FONT,font_size)
w_song_n, h_song_n = draw.textsize(user_name)
draw.text(
(85 + w_pos_s, 165 + 97 * h_pos +
(h_song - h_song_n) * h_song_n / h_song * 1.1),
user_name, font_color)
else:
draw.text((85 + w_pos_s, 165 + 97 * h_pos), user_name, font_color)
im.save("ranking_picture.png")
# 運動記録のランキングをツイートする
def tweet_ranking(api):
cur = conn.cursor()
# DBから前日分の運動結果を抽出し、消費カロリーの多い順でソート
# 昨日の04:00:00 から 今日の03:59:59まで
now = datetime.datetime.now(JST)
yesterday = now - datetime.timedelta(days=1)
yesterday = yesterday.strftime("%Y-%m-%d")
params = (yesterday, )
cur.execute("SELECT user_name,kcal "
"FROM (SELECT *, RANK() OVER(PARTITION BY user_screen_name ORDER BY kcal DESC, tweeted_time ASC) AS rnk FROM Exercise WHERE CAST(tweeted_time - INTERVAL 4 HOUR AS DATE)=CAST(%s AS DATE)) tmp "
"WHERE rnk = 1 ORDER BY kcal DESC, tweeted_time ASC LIMIT 10;",params)
exercise_data_list = cur.fetchall()
make_ranking_picture(exercise_data_list)
tweet = "今日のランキング発表!\n"
for i, exercise_data in enumerate(exercise_data_list):
tweet += f"{i + 1}位 {exercise_data[0]} {exercise_data[1]}kcal\n"
if i + 1 >= 3: break
print(tweet)
api.update_with_media(status=tweet, filename='./ranking_picture.png')
# 運動結果の画像を取得出来たらimagetype,できなかったらNoneを返す
def fetch_image(status):
# mediaがなければ飛ばす
if not hasattr(status, 'extended_entities'):
print("Media not found.")
return None
for i in range(len(status.extended_entities['media'])):
# mediaを保存する
media_url = status.extended_entities['media'][i]['media_url']
try:
urllib.request.urlretrieve(media_url + ':orig', 'temp.jpg')
except IOError:
# 保存に失敗したら飛ばす
print("save miss")
continue
# 運動結果の画像でなければ飛ばす
image_type = info_convert.is_result_image()
if image_type is None:
print("Media is not exercise image.")
continue
else:
return image_type
return None
def reply_exercise_result(api,cur,exercise_data,status):
now = datetime.datetime.now(JST)
ranking_datetime = now - datetime.timedelta(hours=4)
ranking_datetime = ranking_datetime.strftime("%Y-%m-%d")
params=(ranking_datetime,)
# DBから今日の順位分のデータを抽出し、消費カロリー順でソート
cur.execute("select kcal from Exercise "
"WHERE CAST(tweeted_time - INTERVAL 4 HOUR AS DATE)=CAST(%s AS DATE) ORDER BY kcal DESC ;",params)
exercise_data_list = cur.fetchall()
print(exercise_data)
# 消費カロリーの順位を計算する
params = (exercise_data.cal,ranking_datetime)
cur.execute("select count(*) from Exercise WHERE Exercise.kcal > %s "
"AND CAST(tweeted_time - INTERVAL 4 HOUR AS DATE)=CAST(%s AS DATE)", params)
cal_ranking = int(cur.fetchone()[0])
print(cal_ranking)
tweet = "@" + str(status.user.screen_name) + '\n'
tweet += str(exercise_data.cal) + "kcal消費 いい汗かいたね!お疲れ様!\n"
tweet += f"今日の順位 {cal_ranking + 1}位/{len(exercise_data_list)}人中"
print(exercise_data_list)
info_convert.datalist_to_histogram(info_convert.convert_datatuple_to_list(exercise_data_list),
cal_ranking)
api.update_with_media(status=tweet, in_reply_to_status_id=status.id, filename='./hist.png')
if __name__ == '__main__':
api = auth_twitter()
tweet_ranking(api)