-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNewsVFS.py
executable file
·100 lines (83 loc) · 3.34 KB
/
NewsVFS.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
#!/usr/bin/env python
import os
import time
import json
import requests
import subprocess
from playsound import playsound
from urllib.parse import urlencode, quote_plus
from datetime import datetime
class NewsVFS:
# default constructor
def __init__(self, params):
self.params = params
def get_response(self, params):
resp = requests.get(params["url"], headers=params["headers"])
if not 200 == resp.status_code:
return False
try:
resp = resp.json()
except ValueError as e:
return False
return resp
def get_total(self, resp, key):
if key in resp:
return resp[key]
return False
def check_by_total(self, current, post_date):
if key in resp:
return resp[key]
return False
def intialize(self):
print("""
██ ██ ███████ ███████ ███ ██ ███████ ██ ██ ███████
██ ██ ██ ██ ████ ██ ██ ██ ██ ██
██ ██ █████ ███████ ██ ██ ██ █████ ██ █ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
████ ██ ███████ ██ ████ ███████ ███ ███ ███████ ██ ██ ██""")
print("\n")
print("Watching VFS news API for update...")
print("\n")
count = 0
while True:
# Putting the script to sleep for the delay
count += 1
resp = self.get_response(self.params)
print(".", end="", flush=True),
if False == resp:
continue
total = self.get_total(resp, "total")
if total > self.params["max_num"]:
break
time.sleep(self.params["delay"])
print("\n")
print("The total news count is", end =": ")
print(total)
subprocess.call([
"/usr/bin/notify-send",
"VFS Notice Update!",
"New article published at VFS notice borad."
])
print("\n")
for i in resp["items"]:
print("-", end =" ")
print(i["fields"]["date"], end =" --- ")
print(i["fields"]["body"]["content"][0]["content"][0]["value"])
print(">>", end =" ")
print(i["fields"]["body"]["content"][1]["content"][0]["value"])
print("\n")
playsound(self.params["sound"])
def main(params):
if not os.path.isfile(params):
return False
path = os.path.realpath(params)
read = open(path, "r")
params = json.loads(read.read())
read.close()
params["url"] = params["url"] \
+ "?" \
+ urlencode(params["urlparams"], quote_via=quote_plus)
resp = NewsVFS(params)
resp.intialize()
if __name__ == "__main__":
main("./news_creds.json")