-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaika.py
312 lines (270 loc) · 9.97 KB
/
aika.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import json
import logging
import os
import re
from urllib.parse import urlparse
import ffmpy
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from flask import Flask, redirect, request, url_for
from videoprops import get_video_properties
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)
load_dotenv()
app = Flask(__name__)
env = os.environ
telegram_bot_token = env.get("telegram_bot_token")
debugging = False
def get_media_url(account, vid):
header = {
"Accept": "application/json;pk=BCpkADawqM3T47dRzTl5mbQrsSen6Irw0V0_IJkbfWomd5pq9d-QFF9qEEqIx8riJ1F93W8T74JPmcI3J_Mb1vRFbx3kjvIVhoJnjaSu9J3z7FhaSSgoChrjoZu63Wf_q3j4XfYoi5dJOZKr"
}
j = json.loads(
requests.get(
"https://edge.api.brightcove.com/playback/v1/accounts/"
+ account
+ "/videos/"
+ vid,
headers=header,
).text
)
try:
return j["sources"][5]["src"]
except:
return j["sources"][1]["src"]
def download_m3u8(m3u8, filename):
ff = ffmpy.FFmpeg(
global_options="-y -hide_banner -loglevel warning",
inputs={m3u8: None},
outputs={filename: "-c copy"},
)
ff.run()
def send_telegram(input, type, bot=telegram_bot_token, notification=False):
global debugging
if debugging == True:
return
notification = not notification
if type == "text":
r = requests.post(
"https://api.telegram.org/" + bot + "/sendMessage",
params={
"chat_id": "1024110161",
"text": input,
"disable_notification": notification,
},
)
elif type == "voice":
r = requests.post(
"https://api.telegram.org/" + bot + "/sendVoice",
params={"chat_id": "1024110161", "disable_notification": notification},
files={"voice": open(input, "rb")},
)
elif type == "animation":
r = requests.post(
"https://api.telegram.org/" + bot + "/sendAnimation",
params={"chat_id": "1024110161", "disable_notification": notification},
files={"animation": open(input, "rb")},
)
elif type == "photo":
r = requests.post(
"https://api.telegram.org/" + bot + "/sendPhoto",
params={
"chat_id": "1024110161",
"photo": input,
"disable_notification": notification,
},
)
elif type == "video_file":
width = get_video_properties(input)["width"]
height = get_video_properties(input)["height"]
r = requests.post(
"https://api.telegram.org/" + bot + "/sendVideo",
params={
"chat_id": "1024110161",
"width": width,
"height": height,
"disable_notification": notification,
},
files={"video": open(input, "rb")},
)
elif type == "video_url":
r = requests.post(
"https://api.telegram.org/" + bot + "/sendVideo",
params={
"chat_id": "1024110161",
"video": input,
"disable_notification": notification,
},
)
return r
@app.route("/1nichi1aika")
def _1nichi1aika():
# 發出登入requests
session = requests.session()
session.post(
"https://fc.kobayashiaika.jp/s/n85/login",
data={"idpwLgid": env.get("email"), "idpwLgpw": env.get("pw"), "mode": "LOGIN"},
)
r = session.get("https://fc.kobayashiaika.jp/s/n85/lot/top_uranai")
r2 = session.get("https://fc.kobayashiaika.jp/s/n85/diary/fc_1nichi1aika/list")
# 用beautifulsoup處理網頁
uranai_page = BeautifulSoup(r.text, "html.parser")
diary_page = BeautifulSoup(r2.text, "html.parser")
# 取得日期
date = (
diary_page.find("div", class_="textBox")
.find_all("p")[0]
.string.replace(" ", "")
.replace("\n", "")
)
# 取得文字內容
diary = (
diary_page.find("div", class_="textBox").find_all("p")[1].text.replace(" ", "")
) # .replace('\n','')
# 判斷媒體類別
image_or_movie = (
diary_page.find("li", class_="item").find_all("div")[1].get("class")[0]
)
# 根據媒體類別取得網址
if image_or_movie == "image":
content_url = (
str(diary_page.find("li", class_="item").find("div", class_="image").img)
.replace('<img src="', "https://fc.kobayashiaika.jp")
.replace('"/>', "")
)
elif image_or_movie == "movie":
account = diary_page.find("li", class_="item").find("video")["data-account"]
vid = diary_page.find("li", class_="item").find("video")["data-video-id"]
content_url = get_media_url(account, vid)
# 取得占卜點數和gif
point = uranai_page.find("p", class_="point").string.replace("\n", "")
uranai_gif = (
str(uranai_page.find("p", class_="image").img)
.replace('<img src="', "https://fc.kobayashiaika.jp")
.replace('"/>', "")
)
# 取得占卜語音
account = uranai_page.find("div", class_="voice").video["data-account"]
vid = uranai_page.find("div", class_="voice").video["data-video-id"]
uranai_voice = get_media_url(account, vid)
download_m3u8(uranai_voice, "voice.mp4")
# 傳送訊息(telegram)占卜點數
send_telegram(point, "text")
# 傳送訊息(telegram)占卜語音
send_telegram("voice.mp4", "voice")
# 傳送訊息(telegram)占卜gif
open("uranai.gif", "wb").write(requests.get(uranai_gif).content)
send_telegram("uranai.gif", "animation")
# 傳送訊息(telegram)文字
send_telegram(date + "\n" + diary, "text", notification=True)
# 傳送照片或影片
if image_or_movie == "image":
send_telegram(content_url, "photo")
elif image_or_movie == "movie":
parse = urlparse(content_url)
if parse.path[parse.path.find(".") + 1 :] == "m3u8":
download_m3u8(content_url, "video.mp4")
send_telegram("video.mp4", "video_file")
else:
# cleanup url
content_url = re.sub(r"\?pubId=\d*&videoId=\d*", "", content_url)
send_telegram(content_url, "video_url")
# 返回結果(網頁)
return date + "\n" + diary + "\n" + content_url
@app.route("/radio")
def radio():
# login
session = requests.Session()
session.post(
"https://fc.kobayashiaika.jp/s/n85/login",
data={"idpwLgid": env.get("email"), "idpwLgpw": env.get("pw"), "mode": "LOGIN"},
)
r = session.get("https://fc.kobayashiaika.jp/s/n85/diary/fc_radioand/list")
# parse html
radio_soup = BeautifulSoup(r.text, "html.parser")
object_ = radio_soup.find("ul", class_="radioList").find_all("li")[0]
# get title
name = (
object_.find_all("div")[1].p.string.replace(" ", "").replace("\n", "")
+ " "
+ object_.find_all("div")[1]
.find_all("p")[1]
.string.replace(" ", "")
.replace("\n", "")
)
# get video url
account = object_.find("video")["data-account"]
vid = object_.find("video")["data-video-id"]
radio_url = get_media_url(account, vid)
# download video
parse = urlparse(radio_url)
if parse.path[parse.path.find(".") + 1 :] == "m3u8":
download_m3u8(radio_url, "video.mp4")
else:
file = requests.get(radio_url)
open("./video.mp4", "wb").write(file.content)
# send message
send_telegram(name + " #radio_and", "text")
sendVideo = send_telegram("video.mp4", "video_file")
# if video size is too large, send url instead
try:
if json.loads(sendVideo.content)["description"] == "Request Entity Too Large":
size = str(round(os.stat("./video.mp4").st_size / (1024 * 1024), 2)) + "MB"
send_telegram(size, "text")
send_telegram(radio_url, "text")
except:
pass
return name + "\n" + radio_url
@app.route("/")
def twitter():
# 取得推文
tweet = request.args.get("tweet")
# check if debugging
global debugging
try:
debugging = True if request.args.get("debugging") == "true" else False
except TypeError:
debugging = False
# 根據推文重新導向網頁
if "『1日1愛香』更新いたしました" in tweet:
return redirect(url_for("_1nichi1aika"))
elif "RADIO AND 更新" in tweet:
return redirect(url_for("radio"))
else:
send_telegram(tweet, "text", env.get("telegram_bot_token_tweet"))
return "ok"
@app.route("/line")
def line():
env = os.environ
my_data = {"idpwLgid": env.get("email"), "idpwLgpw": env.get("pw"), "mode": "LOGIN"}
s = requests.session()
s.post("https://fc.kobayashiaika.jp/s/n85/login", data=my_data)
r = s.get("https://fc.kobayashiaika.jp/s/n85/diary/fc_1nichi1aika/list")
soup = BeautifulSoup(r.text, "html.parser")
date = (
soup.find("div", class_="textBox")
.find_all("p")[0]
.string.replace(" ", "")
.replace("\n", "")
)
content = (
soup.find("div", class_="textBox")
.find_all("p")[1]
.string.replace(" ", "")
.replace("\n", "")
)
image = (
str(soup.find("li", class_="item").find("div", class_="image").img)
.replace('<img src="', "https://fc.kobayashiaika.jp")
.replace('"/>', "")
)
return date + "\n" + content + "\n" + image
if __name__ == "__main__":
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get("PORT", 5000))
if env.get("MODE") != "production":
app.run(host="0.0.0.0", port=port)
else:
from waitress import serve
serve(app, host="0.0.0.0", port=port)