-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
741 lines (665 loc) · 23.9 KB
/
utils.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
import os
from linebot import LineBotApi, WebhookParser
from linebot.models import MessageEvent, TextMessage, TextSendMessage
import configparser
import requests
from bs4 import BeautifulSoup
import time
import json
import re
import numpy as np
import pandas as pd
from datetime import date
from opencc import OpenCC
from linebot.models import (
ConfirmTemplate,
MessageEvent,
TextSendMessage,
TemplateSendMessage,
ButtonsTemplate,
MessageTemplateAction,
CarouselTemplate,
CarouselColumn,URIAction,
PostbackAction,
PostbackTemplateAction,
MessageAction
)
config = configparser.ConfigParser()
config.read("config.ini")
# channel_access_token = os.getenv("LINE_CHANNEL_ACCESS_TOKEN", None)
channel_access_token = config['line_bot']['Channel_Access_Token']
id = config['line_bot']['User_id']
def send_text_message(reply_token, text):
line_bot_api = LineBotApi(channel_access_token)
line_bot_api.push_message(id, TextSendMessage(text=text))
return "OK"
def send_text_button(reply_token, text):
print('i am in send_text button')
line_bot_api = LineBotApi(channel_access_token)
Carousel_template = TemplateSendMessage(
alt_text='Carousel template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url='https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2875643522.webp',
title='戲劇',
text='請點選以下功能',
actions=[
MessageTemplateAction(
label='熱門戲劇',
text='熱門戲劇'
),
MessageTemplateAction(
label='熱門綜藝',
text='熱門綜藝'
),
MessageTemplateAction(
label='各國熱門戲劇',
text='各國熱門戲劇'
)
]
),
CarouselColumn(
thumbnail_image_url='https://p2.bahamut.com.tw/B/ACG/c/55/0000072755.JPG',
title='動漫',
text='請點選以下功能',
actions=[
MessageTemplateAction(
label='最近新作',
text='最近新作'
),
MessageTemplateAction(
label='本月熱門',
text='本月熱門'
),
MessageTemplateAction(
label='搜尋動畫',
text='搜尋動畫'
)
]
),
CarouselColumn(
thumbnail_image_url='https://movies.yahoo.com.tw/i/o/production/movies/March2022/IPaDhsb4uOa5QvdXGh3G-1080x1601.jpg',
title='Yahoo電影',
text='請選擇以下功能',
actions=[
MessageTemplateAction(
label='台灣票房',
text='台灣票房'
),
MessageTemplateAction(
label='美國票房',
text='美國票房'
),
MessageTemplateAction(
label='本周上映',
text='本周上映'
)
]
)
]
)
)
line_bot_api.reply_message(reply_token,Carousel_template)
return "OK"
def send_text_drama_button(reply_token):
print('i am in send_text button')
line_bot_api = LineBotApi(channel_access_token)
Carousel_template = TemplateSendMessage(
alt_text='Carousel template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url='https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2586800409.jpg',
title='戲劇',
text='請點選以下功能',
actions=[
MessageTemplateAction(
label='熱門日劇',
text='熱門日劇'
),
MessageTemplateAction(
label='熱門韓劇',
text='熱門韓劇'
),
MessageTemplateAction(
label='熱門陸劇',
text='熱門陸劇'
)
]
),
CarouselColumn(
thumbnail_image_url='https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2535085957.jpg',
title='戲劇',
text='請點選以下功能',
actions=[
MessageTemplateAction(
label='熱門美劇',
text='熱門美劇'
),
MessageTemplateAction(
label='熱門英劇',
text='熱門英劇'
),
MessageTemplateAction(
label='熱門港劇',
text='熱門港劇'
)
]
)
]
)
)
line_bot_api.reply_message(reply_token,Carousel_template)
return "OK"
def send_hot(reply_token):
line_bot_api = LineBotApi(channel_access_token)
title = []
images = []
view = []
scores = []
types = []
times = []
address = []
url = "https://ani.gamer.com.tw/animeList.php?c=All&sort=2"
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"})
soup = BeautifulSoup(r.text,"html.parser")
theme = soup.find('div', 'theme-list-block')
list1 = theme.find_all('a', 'theme-list-main')
infoblock = theme.find_all('p', 'theme-name')
# title
for i in infoblock:
x = i.getText()
title.append(x)
#images
for i in list1:
part = i.find('img')
images.append(part.get("src"))
# total views
for b in list1:
view.append(b.find('p').getText())
# address
for a in list1:
address.append("https://ani.gamer.com.tw/" + a['href'])
for des_url in address:
# print(des_url)
r = requests.get(des_url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"})
soup1 = BeautifulSoup(r.text,"html.parser")
# scores
score = soup1.find('div', 'score-overall-number').getText()
scores.append("評分 " + score)
#types
datatype = soup1.find('ul', 'data_type')
category = datatype.find('li').getText()
x = datatype.find('li').getText()
x = x[4:]
types.append("作品類型 " + x)
# times
time = soup1.find('div', 'anime_info_detail')
time = time.find('p').text
time = time[5:15]
times.append(time)
carousel_group = []
details = []
for i in range(10):
detail = times[i] + " " + scores[i] +"\n" + types[i] + "\n" + "月觀看數 " + view[i] + "\n"
details.append(detail)
carousel_data = CarouselColumn(
thumbnail_image_url = images[i],
title = title[i],
text = details[i],
actions=[
URIAction(label='詳細內容', uri = address[i]),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text='CarouselTemplate',
template=CarouselTemplate(
columns=carousel_group,
image_size='cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
def send_new(reply_token):
line_bot_api = LineBotApi(channel_access_token)
title = []
images = []
view = []
scores = []
types = []
times = []
address = []
url = "https://ani.gamer.com.tw/animeList.php"
print(url)
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"})
soup = BeautifulSoup(r.text,"html.parser")
theme = soup.find('div', 'theme-list-block')
list1 = theme.find_all('a', 'theme-list-main')
infoblock = theme.find_all('p', 'theme-name')
# title
for i in infoblock:
x = i.getText()
title.append(x)
#images
for i in list1:
part = i.find('img')
images.append(part.get("src"))
# total views
for b in list1:
view.append(b.find('p').getText())
# address
for a in list1:
address.append("https://ani.gamer.com.tw/" + a['href'])
for des_url in address:
r = requests.get(des_url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"})
soup1 = BeautifulSoup(r.text,"html.parser")
# scores
score = soup1.find('div', 'score-overall-number').getText()
scores.append("評分 " + score)
#types
datatype = soup1.find('ul', 'data_type')
category = datatype.find('li').getText()
x = datatype.find('li').getText()
x = x[4:]
types.append("作品類型 " + x)
# times
time = soup1.find('div', 'anime_info_detail')
time = time.find('p').text
time = time[5:15]
times.append(time)
carousel_group = []
details = []
for i in range(10):
detail = times[i] +"\t\t" + scores[i] + "\n" + types[i] + "\n" + "總觀看數 " + view[i] + "\n"
details.append(detail)
carousel_data = CarouselColumn(
thumbnail_image_url = images[i],
title = title[i],
text = details[i],
actions=[
URIAction(label='詳細內容', uri = address[i]),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text='CarouselTemplate',
template=CarouselTemplate(
columns=carousel_group,
image_size='cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
# search name
def send_search(reply_token, text):
line_bot_api = LineBotApi(channel_access_token)
# line_bot_api.push_message(reply_token, TextSendMessage(text=text))
name = "You want to search " + text
print(type(text))
print(name)
# list of all details
title = []
images = []
view = []
scores = []
types = []
times = []
address = []
url = "https://ani.gamer.com.tw/search.php?keyword=" + text
# url2 = "https://ani.gamer.com.tw/search.php?keyword=" + text
print(url)
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"})
soup = BeautifulSoup(r.text,"html.parser")
theme = soup.find('div', 'theme-list-block')
list1 = theme.find_all('a', 'theme-list-main')
# print(list1)
# print(len)
if len(list1) == 0:
error_msg = "找不到此動畫,請輸入其他動畫名稱"
line_bot_api.push_message(id, TextSendMessage(text = error_msg))
return "OK"
else:
infoblock = theme.find_all('p', 'theme-name')
# title
for i in infoblock:
x = i.getText()
title.append(x)
#images
for i in list1:
part = i.find('img')
images.append(part.get("src"))
# total views
for b in list1:
view.append(b.find('p').getText())
# address
for a in list1:
address.append("https://ani.gamer.com.tw/" + a['href'])
for des_url in address:
# print(des_url)
r = requests.get(des_url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"})
soup1 = BeautifulSoup(r.text,"html.parser")
# scores
score = soup1.find('div', 'score-overall-number').getText()
scores.append("評分 " + score)
#types
datatype = soup1.find('ul', 'data_type')
category = datatype.find('li').getText()
x = datatype.find('li').getText()
x = x[4:]
types.append("作品類型 " + x)
# times
time = soup1.find('div', 'anime_info_detail')
time = time.find('p').text
time = time[5:15]
times.append(time)
carousel_group = []
details = []
num = min(len(list1), 10)
for i in range(num):
detail = times[i] + " " + scores[i] + "\n" + types[i] + "\n" + "總觀看數 " + view[i] + "\n"
details.append(detail)
carousel_data = CarouselColumn(
thumbnail_image_url = images[i],
title = title[i],
text = details[i],
actions=[
URIAction(label='詳細內容', uri = address[i]),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text='CarouselTemplate',
template=CarouselTemplate(
columns=carousel_group,
image_size='cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
# send type of drama
def send_drama(reply_token, text):
print('i am in send drama')
line_bot_api = LineBotApi(channel_access_token)
if text == 'hot':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=热门&sort=recommend&page_limit=20'
elif text == 'china':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=国产剧&sort=recommend&page_limit=20'
elif text == 'japan':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=日剧&sort=recommend&page_limit=20'
elif text == 'korea':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=韩剧&sort=recommend&page_limit=20'
elif text == 'america':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=美剧&sort=recommend&page_limit=20'
elif text == 'england':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=英剧&sort=recommend&page_limit=20'
elif text == 'hongkong':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=港剧&sort=recommend&page_limit=20'
elif text == 'entertain':
url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=综艺&sort=recommend&page_limit=20'
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
}
r = requests.get(url, headers=headers)
json_str = r.content.decode()
dict_ret = json.loads(json_str)
content_list = dict_ret["subjects"]
content_list = content_list[:10]
cc = OpenCC('s2t')
for i in content_list:
text = i['title']
text = cc.convert(text)
i['title'] = text
if i['rate'] == '':
i['rate'] = '暫無評分'
carousel_group = []
details = []
for i in range(10):
detail = '評分:'+ content_list[i]['rate'] + "\n"
details.append(detail)
carousel_data = CarouselColumn(
thumbnail_image_url = content_list[i]['cover'],
title = content_list[i]['title'],
text = details[i],
actions=[
URIAction(label='詳細內容', uri = content_list[i]['url']),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text = 'CarouselTemplate',
template = CarouselTemplate(
columns = carousel_group,
image_size = 'cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
# send new movie
def send_new_movie(reply_token):
print('i am in send new movie')
line_bot_api = LineBotApi(channel_access_token)
url = 'https://movies.yahoo.com.tw/movie_thisweek.html'
response = requests.get(url=url)
soup = BeautifulSoup(response.text, 'lxml')
imgs = []
info_items = soup.find_all('div', 'release_info')
names = []
english_names = []
dates = []
levels = []
address = []
imdbs = []
times = []
for item in info_items:
name = item.find('div', 'release_movie_name').a.text.strip()
names.append(name)
english_name = item.find('div', 'en').a.text.strip()
english_names.append(english_name)
release_time = item.find('div', 'release_movie_time').text.split(':')[-1].strip()
dates.append(release_time)
level = item.find('div', 'leveltext').span.text.strip()
levels.append(level)
fotos = soup.find_all('div', 'release_foto')
for i in fotos:
img = i.find('img').get('data-src')
imgs.append(i.find('img').get('data-src'))
address.append(i.a.get('href'))
for i in address:
response2 = requests.get(url=i)
soup2 = BeautifulSoup(response2.text, 'lxml')
table2 = soup2.find('div', 'movie_intro_info_r')
attr = table2.find_all('span')
imdbs.append(attr[3].text)
time = attr[1].text
time = time.split(':')[1]
times.append(time)
# make carousal button
carousel_group = []
details = []
for i in range(8):
detail = dates[i]
carousel_data = CarouselColumn(
thumbnail_image_url = imgs[i],
title = names[i],
text = detail,
actions=[
URIAction(label='詳細內容', uri = address[i]),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text = 'CarouselTemplate',
template = CarouselTemplate(
columns = carousel_group,
image_size = 'cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
# send_usa_hot_movie 美國票房
def send_usa_hot_movie(reply_token):
print('i am in send drama')
line_bot_api = LineBotApi(channel_access_token)
url2 = 'https://movies.yahoo.com.tw/chart.html?cate=us'
response = requests.get(url=url2)
soup = BeautifulSoup(response.text, 'lxml')
names = []
english_names = []
dates = []
levels = []
address = []
imdbs = []
times = []
imgs = []
movies = soup.find('div', 'rank_list table rankstyle1')
movies_list = movies.find_all('div', 'tr')
del(movies_list[0])
# movies_list = movies_list[:5]
address.clear()
for i in movies_list:
try:
tmp = i.find('a').get('href')
except Exception as ee:
print('cannot find address')
if(tmp != ""):
address.append(tmp)
# print('len of address : ' + len(address))
address = list(dict.fromkeys(address))
print(len(address))
for i in address:
response2 = requests.get(url=i)
soup2 = BeautifulSoup(response2.text, 'lxml')
# names
table = soup2.find('div', 'movie_intro_info_r')
name = table.find('h1').text
names.append(name)
english_name = table.find('h3').text
english_names.append(english_name)
# img
table = soup2.find('div', 'movie_intro_info_l')
img = table.find('img').get('src')
imgs.append(img)
# imdb
table2 = soup2.find('div', 'movie_intro_info_r')
attr = table2.find_all('span')
imdb = attr[3].text
if imdb[0] == 'I':
imdbs.append(imdb)
else:
imdbs.append('no imdb')
time = attr[1].text
time = time.split(':')[1]
times.append(time)
dates.append(attr[0].text)
# make button
carousel_group = []
details = []
for i in range(5):
detail = dates[i] + '\n' + imdbs[i]
carousel_data = CarouselColumn(
thumbnail_image_url = imgs[i],
title = names[i],
text = detail,
actions=[
URIAction(label='詳細內容', uri = address[i]),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text = 'CarouselTemplate',
template = CarouselTemplate(
columns = carousel_group,
image_size = 'cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
# send_taiwan_hot_movie 台灣票房
def send_taiwan_hot_movie(reply_token):
print('i am in send drama')
line_bot_api = LineBotApi(channel_access_token)
url = 'https://movies.yahoo.com.tw/chart.html'
response = requests.get(url=url)
soup = BeautifulSoup(response.text, 'lxml')
names = []
english_names = []
dates = []
levels = []
address = []
imdbs = []
times = []
imgs = []
movies = soup.find('div', 'rank_list table rankstyle1')
movies_list = movies.find_all('div', 'tr')
del(movies_list[0])
movies_list = movies_list[:10]
for i in movies_list:
address.append(i.find('a').get('href'))
for i in address:
response2 = requests.get(url=i)
soup2 = BeautifulSoup(response2.text, 'lxml')
# names
table = soup2.find('div', 'movie_intro_info_r')
name = table.find('h1').text
names.append(name)
english_name = table.find('h3').text
english_names.append(english_name)
# img
table = soup2.find('div', 'movie_intro_info_l')
img = table.find('img').get('src')
imgs.append(img)
# imdb
table2 = soup2.find('div', 'movie_intro_info_r')
attr = table2.find_all('span')
imdb = attr[3].text
if imdb[0] == 'I':
imdbs.append(imdb)
else:
imdbs.append('no imdb')
time = attr[1].text
time = time.split(':')[1]
times.append(time)
dates.append(attr[0].text)
carousel_group = []
details = []
for i in range(10):
detail = dates[i] + '\n' + imdbs[i]
carousel_data = CarouselColumn(
thumbnail_image_url = imgs[i],
title = names[i],
text = detail,
actions=[
URIAction(label='詳細內容', uri = address[i]),
]
)
carousel_group.append(carousel_data)
buttons_template_message = TemplateSendMessage(
alt_text = 'CarouselTemplate',
template = CarouselTemplate(
columns = carousel_group,
image_size = 'cover'
)
)
line_bot_api.reply_message(reply_token, buttons_template_message)
return "OK"
def send_back(reply_token):
line_bot_api = LineBotApi(channel_access_token)
line_bot_api.push_message(id, TemplateSendMessage(
alt_text='ConfirmTemplate',
template=ConfirmTemplate(
text='回到首頁? 回前一步?',
actions=[
MessageAction(
label='回到首頁',
text='回到首頁'
),
MessageAction(
label='回前一步',
text='回前一步'
)
]
)
))
return "OK"