-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
84 lines (62 loc) · 1.9 KB
/
main.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
#!/usr/bin/env python
from getter import *
from tweet import *
from slack import *
from calAdd import *
import sys
import time
# Open and Read file
# Return object(dict/list)
def read_json_file(open_file):
# Read json file
with open(open_file, 'r') as f:
# Convert json -> list&dict
posts = json.loads(f.read())
# for o in posts:
# print(o)
return posts
# Search new-posts on target from base
# Retrun object(dict/list)
def find_new_posts(target, base):
diff=[]
for n in target:
for o in base:
# Exist post
if(n==o):
break
else:
# New post found
# print(n)
diff.append(n)
# print(diff)
return diff
# Save to file
# Return -
def save2file(save_posts, save_file):
# Convert list&dict -> json
dumped_posts = json.dumps(save_posts, ensure_ascii=True, indent=4)
# Store json file
with open(save_file, 'w') as f:
f.write( dumped_posts )
def main():
JSON_FILE = 'cancel.json'
old_posts = read_json_file(open_file=JSON_FILE)
new_posts = get_updates(BASE_URL='https://inside.teu.ac.jp/hachiouji/hachioji_common/cancel/page/')
different_posts = find_new_posts(target=new_posts, base=old_posts)
calAdd(posts=different_posts)
'''
for p in different_posts:
try:
dept = ', '.join(p['dept'])
message = p['day'] + " " + p['time'] + "限 " + p['course'] + "(" + p['teacher'] + ", " + dept + ", " + p['grade'] + ", クラス:" + p['class'] + ")"
print(message)
tweet(message)
slack(message)
except Exception as e:
print(e)
time.sleep(3)
'''
save2file(save_posts=new_posts, save_file=JSON_FILE)
# 'day', 'time', 'course', 'teacher', 'dept', 'grade', 'class', 'misc', 'update'
if __name__ == '__main__':
main()