-
Notifications
You must be signed in to change notification settings - Fork 0
/
BusInfomationCrawler.py
132 lines (117 loc) ยท 5.27 KB
/
BusInfomationCrawler.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
import datetime
from collections import OrderedDict
tag = '#intercity-timetable > div > table:nth-child(1) > tbody > tr:nth-child({}) > td:nth-child(1)'
# ํ์ ์ด๋ฏธ์ง https://medibang.com/picture/d11801050948336930001071938/
class BusInformation :
def __init__(self, soup) :
self.soup = soup
self.busTimeTable = []
self.selector = None
self.strNow = None
self.target = -1
# ๋ฒ์ค ์๊ฐํ๋ฅผ ์ฝ์ด์ด
def crawling(self) :
for i in range(16) :
selector = self.soup.select(tag.format(i+1))
self.busTimeTable.append(selector[0].text)
return self
# ๊ฐ์ฅ ๋น ๋ฅด๊ฒ ์ถ๋ฐํ๋ ๋ฒ์ค๋ฅผ ์๋ณํจ,
def getNextBus(self) :
if self.selector == None :
self.crawling()
self.strNow = datetime.datetime.now().strftime('%H:%M')
timeNow = datetime.datetime.strptime(self.strNow, '%H:%M')
self.target = -1
for i in range(len(self.busTimeTable)) :
time = datetime.datetime.strptime(self.busTimeTable[i], '%H:%M')
if time > timeNow :
self.target = i
break
if self.busTimeTable[i] == self.busTimeTable[-1] :
self.target = 0
return self.busTimeTable[self.target]
def convertTimetoEmoji(self) :
numberEmoji = [':zero:',':one:',':two:',':three:',':four:',':five:',':six:',':seven:',':eight:',':nine:']
emoji = ''
for i in self.busTimeTable[self.target] :
emoji += ' : ' if i == ':' else numberEmoji[int(i)] + ' '
return emoji
def getOtherBus(self) :
return self.busTimeTable[(self.target+1)%len(self.busTimeTable)],self.busTimeTable[(self.target+2)%len(self.busTimeTable)]
# ๋์ฐฉํ๊ธฐ ๊น์ง ๋จ์ ์๊ฐ
def getTimeToArrive(self) :
if self.strNow == None :
self.getNextBus()
nowHour = int(self.strNow.split(':')[0])
nowMinute = int(self.strNow.split(':')[1])
targetHour = int(self.busTimeTable[self.target].split(':')[0])
targetMinute = int(self.busTimeTable[self.target].split(':')[1])
totalMinute = (targetHour*60+targetMinute) - (nowHour*60+nowMinute) if nowHour <= targetHour else ((24+targetHour)*60+targetMinute) - (nowHour*60+nowMinute)
if totalMinute > 59 :
return str(totalMinute // 60) + '์๊ฐ' + str(totalMinute % 60) +'๋ถ'
else :
return str(totalMinute) + '๋ถ'
def getAnswer(self) :
self.getNextBus()
jsonData = OrderedDict()
jsonData['color'] = '#2398cf'
jsonData['blocks'] = []
jsonData['blocks'].append({'type' : 'section','text' : {"type": "mrkdwn","text": "*[๋์ฑ๊ณ ์] ๋ถ๋ฆ๋ถ๋ฆ ๋ฒ์ค ๊ณง ์ถ๋ฐํฉ๋๋ค. :oncoming_bus:*\n *๊ณง ์ถ๋ฐํ๋ ๋ฒ์ค ์๊ฐ์* {}".format(self.convertTimetoEmoji())}})
jsonData['blocks'].append({"type": "divider"})
field = []
field.append({"type": "mrkdwn","text": "*์ด๋๋ก?*\n`ํ๊ธฐ๋ -> ์ผ์ฐ๋ฆฌ`"})
field.append({"type": "mrkdwn","text": "*๋จ์ ์๊ฐ*\n `{}`".format(self.getTimeToArrive())})
field.append({"type": "mrkdwn","text": "*๋ค์ ๋ฒ์ค*\n _{}_".format(self.getOtherBus()[0])})
field.append({"type": "mrkdwn","text": "*๊ทธ ๋ค์ ๋ฒ์ค*\n _{}_".format(self.getOtherBus()[1])})
field.append({"type": "mrkdwn","text": '*์ ์ฒด ๋ฒ์ค ์๊ฐํ*\n<{}|{}>'.format('https://hantalk.io/bus','๋ณด๊ธฐ')})
jsonData['blocks'].append({'type' : 'section','fields' : field})
#jsonData['blocks'].append({'type' : 'section','fields' : field, 'accessory' : {"type": "image","image_url": "https://raw.githubusercontent.com/leejaeyeong/ZimaBlue/master/reading_room_status.png","alt_text": "emotion"}})
return jsonData
# businfo json example
"""
bus = {
"color" : '#2398cf',
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*[๋์ฑ๊ณ ์] ๋ถ๋ฆ๋ถ๋ฆ ๋ฒ์ค ๊ณง ์ถ๋ฐํฉ๋๋ค. :oncoming_bus:*"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*์ด๋๋ก?*\n`ํ๊ธฐ๋ -> ์ผ์ฐ๋ฆฌ`"
},
{
"type": "mrkdwn",
"text": "*๋จ์ ์๊ฐ*\n `{}` ๋ถ".format(50)
},
{
"type": "mrkdwn",
"text": "*๋ค์ ๋ฒ์ค*\n{}".format('_07:00_')
},
{
"type": "mrkdwn",
"text": "*๊ทธ ๋ค์ ๋ฒ์ค*\n{}".format('_08:20_')
},
{
"type": "mrkdwn",
"text": '*์ ์ฒด ๋ฒ์ค ์๊ฐํ*\n<{}|{}>'.format('https://hantalk.io/bus','๋ณด๊ธฐ')
}
],
"accessory": {
"type": "image",
"image_url": "https://raw.githubusercontent.com/leejaeyeong/ZimaBlue/master/reading_room_status.png"
"alt_text": "Haunted hotel image"
}
}
]
}
"""