-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.py
executable file
·213 lines (182 loc) · 6.65 KB
/
tweet.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
#!/usr/bin/env python3
import argparse
import base64
import datetime
from get_tweepy import get_api
from io import BytesIO
import json
from PIL import Image
import requests
from selenium.webdriver import PhantomJS
from selenium.webdriver.common.action_chains import ActionChains
import time
import tweepy
def get_weekday(day):
return '月火水木金土日'[day.weekday()]
def get_latest_data_daily():
url = 'https://skrm.ch/prettyrhythm/kinpri-box-office/api/v1/mimorin/daily.json'
r = requests.get(url)
j = json.loads(r.text)
latest = j[-3]
previous_day = j[-4]
last_week = j[-10]
daily_show_diff = latest[2] - previous_day[2]
daily_sell_diff = latest[1] - previous_day[1]
weekly_show_diff = latest[2] - last_week[2]
weekly_sell_diff = latest[1] - last_week[1]
# 先週比を計算
if last_week[2] != 0:
weekly_show_percent = round((latest[2] / last_week[2]) * 100)
else:
weekly_show_percent = '?'
if last_week[1] != 0:
weekly_sell_percent = round((latest[1] / last_week[1]) * 100)
else:
weekly_sell_percent = '?'
return {
'date': latest[0],
'sell': latest[1],
'show': latest[2],
'daily_show_diff': daily_show_diff,
'daily_sell_diff': daily_sell_diff,
'weekly_show_diff': weekly_show_diff,
'weekly_sell_diff': weekly_sell_diff,
'weekly_show_percent': weekly_show_percent,
'weekly_sell_percent': weekly_sell_percent,
}
def get_latest_data_weekly():
url = 'https://skrm.ch/prettyrhythm/kinpri-box-office/api/v1/mimorin/weekly.json'
r = requests.get(url)
j = json.loads(r.text)
latest = j[-2]
previous = j[-3]
show_diff = latest[2] - previous[2]
sell_diff = latest[1] - previous[1]
# 先週比を計算
if previous[2] != 0:
show_percent = round((latest[2] / previous[2]) * 100)
else:
show_percent = '?'
if previous[1] != 0:
sell_percent = round((latest[1] / previous[1]) * 100)
else:
sell_percent = '?'
return {
'date': latest[0].replace('\n', ''),
'sell': latest[1],
'show': latest[2],
'show_diff': show_diff,
'sell_diff': sell_diff,
'show_percent': show_percent,
'sell_percent': sell_percent,
}
def kinpri2_daily():
# go to the site
url = 'https://skrm.ch/prettyrhythm/kinpri-box-office/'
br = PhantomJS()
br.maximize_window()
br.get(url)
time.sleep(20)
# move to the chart location
h3 = br.find_elements_by_css_selector('h3')[1]
ActionChains(br).move_to_element(h3).perform()
h3 = br.find_elements_by_css_selector('h3')[0]
ActionChains(br).move_to_element(h3).perform()
# hover on the latest day's bar
# css = ('#daily-chart > div > div:nth-child(1) > div > svg > '
# 'g:nth-child(4) > g:nth-child(2) > g:nth-child(2) > rect')
# bar = br.find_elements_by_css_selector(css)[-3]
# bar.click()
# time.sleep(1)
# crop & save the chart area of the screenshot image
img = Image.open(BytesIO(br.get_screenshot_as_png()))
chart = br.find_elements_by_css_selector('svg')[0]
x, y = chart.location['x'], chart.location['y']
h, w = chart.size['height'], chart.size['width']
crop = img.crop((x + 100, y + 50, x + w - 100, y + h - 50))
crop.save('/tmp/knpr_box_office_chart.png')
# tweet the chart image
data = get_latest_data_daily()
yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
status = '''{date}の結果は
上映回数 {show} 回 (先週比{weekly_show_percent}% / {weekly_show_diff:+d}回)
販売座席数 {sell} 席 (先週比{weekly_sell_percent}% / {weekly_sell_diff:+d}席)
でした! #kinpri #prettyrhythm
📈キンプラ 販売座席数グラフ📊
https://skrm.ch/prettyrhythm/kinpri-box-office/'''.format(
date=data['date'],
show=data['show'],
sell=data['sell'],
daily_show_diff=data['daily_show_diff'],
daily_sell_diff=data['daily_sell_diff'],
weekly_show_diff=data['weekly_show_diff'],
weekly_sell_diff=data['weekly_sell_diff'],
weekly_show_percent=data['weekly_show_percent'],
weekly_sell_percent=data['weekly_sell_percent'],
)
api.update_with_media('/tmp/knpr_box_office_chart.png',
status=status)
br.quit()
def kinpri2_weekly():
# go to the site
url = 'https://skrm.ch/prettyrhythm/kinpri-box-office/'
br = PhantomJS()
br.maximize_window()
br.get(url)
time.sleep(20)
# move to the chart location
h3 = br.find_elements_by_css_selector('h3')[3]
ActionChains(br).move_to_element(h3).perform()
h3 = br.find_elements_by_css_selector('h3')[1]
ActionChains(br).move_to_element(h3).perform()
# hover on the latest day's bar
# css = ('#daily-chart > div > div:nth-child(1) > div > svg > '
# 'g:nth-child(4) > g:nth-child(2) > g:nth-child(2) > rect')
# bar = br.find_elements_by_css_selector(css)[-3]
# bar.click()
# time.sleep(1)
# crop & save the chart area of the screenshot image
img = Image.open(BytesIO(br.get_screenshot_as_png()))
chart = br.find_elements_by_css_selector('svg')[1]
x, y = chart.location['x'], chart.location['y']
h, w = chart.size['height'], chart.size['width']
crop = img.crop((x + 100, y + 50, x + w - 100, y + h - 50))
crop.save('/tmp/knpr_box_office_chart.png')
# tweet the chart image
data = get_latest_data_weekly()
yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
status = '''{date}の結果は、
上映回数 {show} 回(先週比{show_percent}%/{show_diff:+d}回)
販売座席数 {sell} 席(先週比{sell_percent}%/{sell_diff:+d}席)
でした! #kinpri #prettyrhythm
📈キンプラ 販売座席数グラフ📊
https://skrm.ch/prettyrhythm/kinpri-box-office/'''.format(
date=data['date'],
show=data['show'],
sell=data['sell'],
show_diff=data['show_diff'],
sell_diff=data['sell_diff'],
show_percent=data['show_percent'],
sell_percent=data['sell_percent'],
)
api.update_with_media('/tmp/knpr_box_office_chart.png',
status=status)
br.quit()
if __name__ == '__main__':
# prepare the args
parser = argparse.ArgumentParser()
parser.add_argument('type')
parser.add_argument('-d', '--debug', action='store_true')
args = parser.parse_args()
# get tweepy api
if args.debug:
api = get_api('sakuramochi_pre')
else:
api = get_api('knpr_box_office')
# tweet according to the type
if args.type == 'kinpri2_daily':
kinpri2_daily()
elif args.type == 'kinpri2_weekly':
kinpri2_weekly()
else:
raise ArgumentError('invalid argument')