-
Notifications
You must be signed in to change notification settings - Fork 149
/
app.py
630 lines (581 loc) · 22.1 KB
/
app.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
import requests
import re
import random
import configparser
from bs4 import BeautifulSoup
from flask import Flask, request, abort
from imgurpython import ImgurClient
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
app = Flask(__name__)
config = configparser.ConfigParser()
config.read("config.ini")
line_bot_api = LineBotApi(config['line_bot']['Channel_Access_Token'])
handler = WebhookHandler(config['line_bot']['Channel_Secret'])
client_id = config['imgur_api']['Client_ID']
client_secret = config['imgur_api']['Client_Secret']
album_id = config['imgur_api']['Album_ID']
API_Get_Image = config['other_api']['API_Get_Image']
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
# print("body:",body)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'ok'
def pattern_mega(text):
patterns = [
'mega', 'mg', 'mu', 'MEGA', 'ME', 'MU',
'me', 'mu', 'mega', 'GD', 'MG', 'google',
]
for pattern in patterns:
if re.search(pattern, text, re.IGNORECASE):
return True
def eyny_movie():
target_url = 'http://www.eyny.com/forum-205-1.html'
print('Start parsing eynyMovie....')
rs = requests.session()
res = rs.get(target_url, verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
content = ''
for titleURL in soup.select('.bm_c tbody .xst'):
if pattern_mega(titleURL.text):
title = titleURL.text
if '11379780-1-3' in titleURL['href']:
continue
link = 'http://www.eyny.com/' + titleURL['href']
data = '{}\n{}\n\n'.format(title, link)
content += data
return content
def apple_news():
target_url = 'https://tw.appledaily.com/new/realtime'
print('Start parsing appleNews....')
rs = requests.session()
res = rs.get(target_url, verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
content = ""
for index, data in enumerate(soup.select('.rtddt a'), 0):
if index == 5:
return content
link = data['href']
content += '{}\n\n'.format(link)
return content
def get_page_number(content):
start_index = content.find('index')
end_index = content.find('.html')
page_number = content[start_index + 5: end_index]
return int(page_number) + 1
def craw_page(res, push_rate):
soup_ = BeautifulSoup(res.text, 'html.parser')
article_seq = []
for r_ent in soup_.find_all(class_="r-ent"):
try:
# 先得到每篇文章的篇url
link = r_ent.find('a')['href']
if link:
# 確定得到url再去抓 標題 以及 推文數
title = r_ent.find(class_="title").text.strip()
rate = r_ent.find(class_="nrec").text
url = 'https://www.ptt.cc' + link
if rate:
rate = 100 if rate.startswith('爆') else rate
rate = -1 * int(rate[1]) if rate.startswith('X') else rate
else:
rate = 0
# 比對推文數
if int(rate) >= push_rate:
article_seq.append({
'title': title,
'url': url,
'rate': rate,
})
except Exception as e:
# print('crawPage function error:',r_ent.find(class_="title").text.strip())
print('本文已被刪除', e)
return article_seq
def crawl_page_gossiping(res):
soup = BeautifulSoup(res.text, 'html.parser')
article_gossiping_seq = []
for r_ent in soup.find_all(class_="r-ent"):
try:
# 先得到每篇文章的篇url
link = r_ent.find('a')['href']
if link:
# 確定得到url再去抓 標題 以及 推文數
title = r_ent.find(class_="title").text.strip()
url_link = 'https://www.ptt.cc' + link
article_gossiping_seq.append({
'url_link': url_link,
'title': title
})
except Exception as e:
# print u'crawPage function error:',r_ent.find(class_="title").text.strip()
# print('本文已被刪除')
print('delete', e)
return article_gossiping_seq
def ptt_gossiping():
rs = requests.session()
load = {
'from': '/bbs/Gossiping/index.html',
'yes': 'yes'
}
res = rs.post('https://www.ptt.cc/ask/over18', verify=False, data=load)
soup = BeautifulSoup(res.text, 'html.parser')
all_page_url = soup.select('.btn.wide')[1]['href']
start_page = get_page_number(all_page_url)
index_list = []
article_gossiping = []
for page in range(start_page, start_page - 2, -1):
page_url = 'https://www.ptt.cc/bbs/Gossiping/index{}.html'.format(page)
index_list.append(page_url)
# 抓取 文章標題 網址 推文數
while index_list:
index = index_list.pop(0)
res = rs.get(index, verify=False)
# 如網頁忙線中,則先將網頁加入 index_list 並休息1秒後再連接
if res.status_code != 200:
index_list.append(index)
# print u'error_URL:',index
# time.sleep(1)
else:
article_gossiping = crawl_page_gossiping(res)
# print u'OK_URL:', index
# time.sleep(0.05)
content = ''
for index, article in enumerate(article_gossiping, 0):
if index == 15:
return content
data = '{}\n{}\n\n'.format(article.get('title', None), article.get('url_link', None))
content += data
return content
def ptt_beauty():
rs = requests.session()
res = rs.get('https://www.ptt.cc/bbs/Beauty/index.html', verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
all_page_url = soup.select('.btn.wide')[1]['href']
start_page = get_page_number(all_page_url)
page_term = 2 # crawler count
push_rate = 10 # 推文
index_list = []
article_list = []
for page in range(start_page, start_page - page_term, -1):
page_url = 'https://www.ptt.cc/bbs/Beauty/index{}.html'.format(page)
index_list.append(page_url)
# 抓取 文章標題 網址 推文數
while index_list:
index = index_list.pop(0)
res = rs.get(index, verify=False)
# 如網頁忙線中,則先將網頁加入 index_list 並休息1秒後再連接
if res.status_code != 200:
index_list.append(index)
# print u'error_URL:',index
# time.sleep(1)
else:
article_list = craw_page(res, push_rate)
# print u'OK_URL:', index
# time.sleep(0.05)
content = ''
for article in article_list:
data = '[{} push] {}\n{}\n\n'.format(article.get('rate', None), article.get('title', None),
article.get('url', None))
content += data
return content
def ptt_hot():
target_url = 'http://disp.cc/b/PttHot'
print('Start parsing pttHot....')
rs = requests.session()
res = rs.get(target_url, verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
content = ""
for data in soup.select('#list div.row2 div span.listTitle'):
title = data.text
link = "http://disp.cc/b/" + data.find('a')['href']
if data.find('a')['href'] == "796-59l9":
break
content += '{}\n{}\n\n'.format(title, link)
return content
def movie():
target_url = 'http://www.atmovies.com.tw/movie/next/0/'
print('Start parsing movie ...')
rs = requests.session()
res = rs.get(target_url, verify=False)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
content = ""
for index, data in enumerate(soup.select('ul.filmNextListAll a')):
if index == 20:
return content
title = data.text.replace('\t', '').replace('\r', '')
link = "http://www.atmovies.com.tw" + data['href']
content += '{}\n{}\n'.format(title, link)
return content
def technews():
target_url = 'https://technews.tw/'
print('Start parsing movie ...')
rs = requests.session()
res = rs.get(target_url, verify=False)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
content = ""
for index, data in enumerate(soup.select('article div h1.entry-title a')):
if index == 12:
return content
title = data.text
link = data['href']
content += '{}\n{}\n\n'.format(title, link)
return content
def panx():
target_url = 'https://panx.asia/'
print('Start parsing ptt hot....')
rs = requests.session()
res = rs.get(target_url, verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
content = ""
for data in soup.select('div.container div.row div.desc_wrap h2 a'):
title = data.text
link = data['href']
content += '{}\n{}\n\n'.format(title, link)
return content
def oil_price():
target_url = 'https://gas.goodlife.tw/'
rs = requests.session()
res = rs.get(target_url, verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
title = soup.select('#main')[0].text.replace('\n', '').split('(')[0]
gas_price = soup.select('#gas-price')[0].text.replace('\n\n\n', '').replace(' ', '')
cpc = soup.select('#cpc')[0].text.replace(' ', '')
content = '{}\n{}{}'.format(title, gas_price, cpc)
return content
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
print("event.reply_token:", event.reply_token)
print("event.message.text:", event.message.text)
if event.message.text.lower() == "eyny":
content = eyny_movie()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "蘋果即時新聞":
content = apple_news()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "PTT 表特版 近期大於 10 推的文章":
content = ptt_beauty()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "來張 imgur 正妹圖片":
client = ImgurClient(client_id, client_secret)
images = client.get_album_images(album_id)
index = random.randint(0, len(images) - 1)
url = images[index].link
image_message = ImageSendMessage(
original_content_url=url,
preview_image_url=url
)
line_bot_api.reply_message(
event.reply_token, image_message)
return 0
if event.message.text == "隨便來張正妹圖片":
image = requests.get(API_Get_Image)
url = image.json().get('Url')
image_message = ImageSendMessage(
original_content_url=url,
preview_image_url=url
)
line_bot_api.reply_message(
event.reply_token, image_message)
return 0
if event.message.text == "近期熱門廢文":
content = ptt_hot()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "即時廢文":
content = ptt_gossiping()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "近期上映電影":
content = movie()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "觸電網-youtube":
target_url = 'https://www.youtube.com/user/truemovie1/videos'
rs = requests.session()
res = rs.get(target_url, verify=False)
soup = BeautifulSoup(res.text, 'html.parser')
seqs = ['https://www.youtube.com{}'.format(data.find('a')['href']) for data in soup.select('.yt-lockup-title')]
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(text=seqs[random.randint(0, len(seqs) - 1)]),
TextSendMessage(text=seqs[random.randint(0, len(seqs) - 1)])
])
return 0
if event.message.text == "科技新報":
content = technews()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "PanX泛科技":
content = panx()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "開始玩":
buttons_template = TemplateSendMessage(
alt_text='開始玩 template',
template=ButtonsTemplate(
title='選擇服務',
text='請選擇',
thumbnail_image_url='https://i.imgur.com/xQF5dZT.jpg',
actions=[
MessageTemplateAction(
label='新聞',
text='新聞'
),
MessageTemplateAction(
label='電影',
text='電影'
),
MessageTemplateAction(
label='看廢文',
text='看廢文'
),
MessageTemplateAction(
label='正妹',
text='正妹'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
return 0
if event.message.text == "新聞":
buttons_template = TemplateSendMessage(
alt_text='新聞 template',
template=ButtonsTemplate(
title='新聞類型',
text='請選擇',
thumbnail_image_url='https://i.imgur.com/vkqbLnz.png',
actions=[
MessageTemplateAction(
label='蘋果即時新聞',
text='蘋果即時新聞'
),
MessageTemplateAction(
label='科技新報',
text='科技新報'
),
MessageTemplateAction(
label='PanX泛科技',
text='PanX泛科技'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
return 0
if event.message.text == "電影":
buttons_template = TemplateSendMessage(
alt_text='電影 template',
template=ButtonsTemplate(
title='服務類型',
text='請選擇',
thumbnail_image_url='https://i.imgur.com/sbOTJt4.png',
actions=[
MessageTemplateAction(
label='近期上映電影',
text='近期上映電影'
),
MessageTemplateAction(
label='eyny',
text='eyny'
),
MessageTemplateAction(
label='觸電網-youtube',
text='觸電網-youtube'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
return 0
if event.message.text == "看廢文":
buttons_template = TemplateSendMessage(
alt_text='看廢文 template',
template=ButtonsTemplate(
title='你媽知道你在看廢文嗎',
text='請選擇',
thumbnail_image_url='https://i.imgur.com/ocmxAdS.jpg',
actions=[
MessageTemplateAction(
label='近期熱門廢文',
text='近期熱門廢文'
),
MessageTemplateAction(
label='即時廢文',
text='即時廢文'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
return 0
if event.message.text == "正妹":
buttons_template = TemplateSendMessage(
alt_text='正妹 template',
template=ButtonsTemplate(
title='選擇服務',
text='請選擇',
thumbnail_image_url='https://i.imgur.com/qKkE2bj.jpg',
actions=[
MessageTemplateAction(
label='PTT 表特版 近期大於 10 推的文章',
text='PTT 表特版 近期大於 10 推的文章'
),
MessageTemplateAction(
label='來張 imgur 正妹圖片',
text='來張 imgur 正妹圖片'
),
MessageTemplateAction(
label='隨便來張正妹圖片',
text='隨便來張正妹圖片'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
return 0
if event.message.text == "imgur bot":
carousel_template_message = TemplateSendMessage(
alt_text='ImageCarousel template',
template=ImageCarouselTemplate(
columns=[
ImageCarouselColumn(
image_url='https://i.imgur.com/g8zAYMq.jpg',
action=URIAction(
label='加我好友試玩',
uri='https://line.me/R/ti/p/%40gmy1077x'
),
),
]
)
)
line_bot_api.reply_message(
event.reply_token,
carousel_template_message)
return 0
if event.message.text == "油價查詢":
content = oil_price()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
carousel_template_message = TemplateSendMessage(
alt_text='目錄 template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url='https://i.imgur.com/kzi5kKy.jpg',
title='選擇服務',
text='請選擇',
actions=[
MessageAction(
label='開始玩',
text='開始玩'
),
URIAction(
label='影片介紹 阿肥bot',
uri='https://youtu.be/1IxtWgWxtlE'
),
URIAction(
label='如何建立自己的 Line Bot',
uri='https://github.com/twtrubiks/line-bot-tutorial'
)
]
),
CarouselColumn(
thumbnail_image_url='https://i.imgur.com/DrsmtKS.jpg',
title='選擇服務',
text='請選擇',
actions=[
MessageAction(
label='other bot',
text='imgur bot'
),
MessageAction(
label='油價查詢',
text='油價查詢'
),
URIAction(
label='聯絡作者',
uri='https://www.facebook.com/TWTRubiks?ref=bookmarks'
)
]
),
CarouselColumn(
thumbnail_image_url='https://i.imgur.com/h4UzRit.jpg',
title='選擇服務',
text='請選擇',
actions=[
URIAction(
label='分享 bot',
uri='https://line.me/R/nv/recommendOA/@vbi2716y'
),
URIAction(
label='PTT正妹網',
uri='https://ptt-beauty-infinite-scroll.herokuapp.com/'
),
URIAction(
label='youtube 程式教學分享頻道',
uri='https://www.youtube.com/channel/UCPhn2rCqhu0HdktsFjixahA'
)
]
)
]
)
)
line_bot_api.reply_message(event.reply_token, carousel_template_message)
@handler.add(MessageEvent, message=StickerMessage)
def handle_sticker_message(event):
print("package_id:", event.message.package_id)
print("sticker_id:", event.message.sticker_id)
# ref. https://developers.line.me/media/messaging-api/sticker_list.pdf
sticker_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 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, 401, 402]
index_id = random.randint(0, len(sticker_ids) - 1)
sticker_id = str(sticker_ids[index_id])
print(index_id)
sticker_message = StickerSendMessage(
package_id='1',
sticker_id=sticker_id
)
line_bot_api.reply_message(
event.reply_token,
sticker_message)
if __name__ == '__main__':
app.run()