-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKensModule.py
More file actions
269 lines (176 loc) · 7.04 KB
/
Copy pathKensModule.py
File metadata and controls
269 lines (176 loc) · 7.04 KB
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
# [Project KENS]KU EE Notice Scraper
# 0.1.1va, 19.08.21. First tested, last update 19.08.23.
# written by acoustikue(SukJoon Oh)
# __ _ __
# ____ __________ __ _______/ /_(_) /____ _____
# / __ `/ ___/ __ \/ / / / ___/ __/ / //_/ / / / _ \
# / /_/ / /__/ /_/ / /_/ (__ ) /_/ / ,< / /_/ / __/
# \__,_/\___/\____/\__,_/____/\__/_/_/|_|\__,_/\___/
#
# Visual Studio 2017 Professional Blank Python Project
# KensModule.py
# Vers 1, selenium type
# 08.22. Seems unnecessary.
# from selenium import webdriver
# Vers 2, bs4 type
import requests
from bs4 import BeautifulSoup
import copy # Deep cpy
# regex, common
# import re # Well?
# import time
import os
project_banner = r'''[Project KENS] KU EE Notice Scraper
0.1.0va, 19.08.21. First tested, last update 19.08.17.
written by acoustikue(SukJoon Oh)
__
/ /____ __ ___ ___ ___________________ _____ ___ _____
/ //_/ / / / / _ \/ _ \ / ___/ ___/ ___/ __ `/ __ \/ _ \/ ___/
/ ,< / /_/ / / __/ __/ (__ ) /__/ / / /_/ / /_/ / __/ /
/_/|_|\__,_/ \___/\___/ /____/\___/_/ \__,_/ .___/\___/_/
_ __ __ /_/
____ _________ (_)__ _____/ /_ / /_____ ____ _____
/ __ \/ ___/ __ \ / / _ \/ ___/ __/ / //_/ _ \/ __ \/ ___/
/ /_/ / / / /_/ / / / __/ /__/ /_ / ,< / __/ / / (__ )
/ .___/_/ \____/_/ /\___/\___/\__/ /_/|_|\___/_/ /_/____/
/_/ /___/
Visual Studio 2017 Professional Blank Python Project
'''
# added for linux system, for cron scheduler command
system_path_windows = os.getcwd() + '\\'
system_path_linux = os.getcwd() + '/'
system_path = system_path_windows
ku_ee_reqeust_url = 'http://ee.konkuk.ac.kr/noticeList.do?siteId=EE&boardSeq=424&menuSeq=2837'
ku_ee_exported_filename = system_path + 'notice.kens'
ku_ee_telegram_user_filename = system_path + 'user_telegram.kens'
# Getting response module
# Parameter: string(url)
# Returns: Response object from reqeust.get()
# Author: acoustikue
def KensGetHtmlText(url):
print('[console] Sending requests...', end='')
# Getting texts
ku_ee_res = requests.get(url)
print('Done, status code: ' + str(ku_ee_res.status_code))
return ku_ee_res
#
# deprecated
def KensParseNoticeTitle(response):
# Ready for parsing?
print('[console] Parsing notice title...', end='')
ku_soup = BeautifulSoup(response.text, 'html.parser')
print(' Done. Printing title(list) of the first page.')
link_list = ku_soup.find_all('a', class_='subject_click')
index = 1
for notice in link_list:
print(' Index[' + str(index) + ']: ' + str(notice.text.lstrip()))
index += 1
return link_list
#
# deprecated
def KensParseNoticeDate(response):
# Ready for parsing?
print('[console] Parsing written dates...', end='')
ku_soup = BeautifulSoup(response.text, 'html.parser')
print(' Done. Printing date(list) of the first page.')
date_list = ku_soup.find_all('td')
index = 1
for notice in date_list:
print(' Index[' + str(index) + ']: ' + str(notice.text.lstrip()))
index += 1
return date_list
#
# main interface
# Parameter: Response object
# Returns: Dictionary type of parsed data.
# Author: acoustikue
def KensParseNotice(response):
# Ready for parsing?
print('[console] Parsing information...', end='')
ku_soup = BeautifulSoup(response.text, 'html.parser')
ku_soup = BeautifulSoup(str(ku_soup.find('table', class_='grid')), 'html.parser')
print(' Done. Printing infos(list) of the first page.')
list = ku_soup.find_all('td')
#link = ku_soup.find_all('a')
#print(link)
# literally works as a structure.
# Deep copy necessary.
structure = {
'NUMBER': '1',
'TITLE': 'None',
'AUTHOR': 'None',
'DATE': 'None',
'SEEN': '0'
}
notice_info = []
index = 0
for info in list:
if(index == 5): index = 0
if index is 0: structure['NUMBER'] = str(info.text.lstrip())
if index is 1:
structure['TITLE'] = str(info.text.lstrip())
# structure['TITLE'] = str(info.text.lstrip().replace('\n', ''))
# Memo: if parsed, there will be always '\n' at the end of every title.
if index is 2: structure['AUTHOR'] = str(info.text.lstrip())
if index is 3: structure['DATE'] = str(info.text.lstrip())
if index is 4:
structure['SEEN'] = info.text.lstrip()
notice_info.append(copy.deepcopy(structure))
index += 1
return notice_info
# Prerequisite for comparing function!
# Parameter: Dictionary type, parsed information.
# Returns: Array type of converted dictionary data.
# Author: acoustikue
def KensConvertDictToArrary(parsed_info):
print('[console] Converting dictionary to array... ', end='')
info_array = []
for info in parsed_info:
info_array.append(str(info['NUMBER']) + '\t' + str(info['DATE']) + '\t' + str(info['TITLE'].rstrip()))
print(' Done converting.')
return info_array
# tested.
# Parameter: Dictionary type of parsed data.
# Returns: -
# Author: acoustikue
def KensWriteInfo(info_dict):
# saves information as texts
with open(ku_ee_exported_filename, "w") as save_file:
print('[console] Writing parsed information...', end='')
for info in info_dict:
save_file.write(str(info['NUMBER']) + '\t' + str(info['DATE']) + '\t' + str(info['TITLE']))
print(' Done exporting.')
# tested.
# Parameter: File address, will be declrared in global scope
# Returns: Array type.
# Author: acoustikue
def KensReadInfo(file_addr):
with open(file_addr, "r") as saved_file:
print('[console] Reading previous information...')
saved_info = saved_file.read().splitlines()
# Removing '\n'
for info in saved_info:
print('\t ' + info)
print('[console] Done reading.')
return saved_info
# Comparing function
# Parameter: Array, Array
# Returns: Array of updated data.
# Author: acoustikue
def KensCompare(prev_notice, new_notice):
is_in_list_flag = False
updated_notice = [] # This is where different string will be stored
# Double for scope will be fine,
# since there is not much information.
for new in new_notice:
is_in_list_flag = False # set this first to False
for prev in prev_notice:
# if prev is same with new,
if prev == new:
is_in_list_flag = True
# it is in the list, so set the flag to True
break
if is_in_list_flag is False: # if not found, then the string is updated string.
print('[console] New notice updated: ' + new)
updated_notice.append(new)
return updated_notice