-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRT_Monitor.py
112 lines (90 loc) · 3.36 KB
/
PRT_Monitor.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
# -*- coding: utf-8 -*-
""" PRT Monitor
Twitter bot 🤖 written in Python 🐍 that monitors WVU's PRT.
Copyright 2017, Ricky Deal, All rights reserved.
"""
import time, urllib, json, csv, os, unicodedata
from collections import deque
from twython import Twython
from auth import (
consumer_key,
consumer_secret,
access_token_key,
access_token_secret)
""" Creates and updates the CSV file
Converts the JSON data to CSV and saves it to 'monitor.csv'
"""
def toCSV(data):
# if CSV file exists then just write to it
if os.path.isfile('monitor.csv') == True:
csvData = open('monitor.csv', 'a')
csvWriter = csv.writer(csvData) # Create the CSV writer object
csvWriter.writerow(data.values())
# else create the CSV file first, then write to it
else:
csvData = open('monitor.csv', 'a')
csvWriter = csv.writer(csvData) # Create the CSV writer object
csvWriter.writerow(data.keys())
csvWriter.writerow(data.values())
csvData.close()
""" Returns lastRow
Return the last status recorded in 'monitor.csv'
"""
def _get_last_row(csv_filename):
with open(csv_filename, 'r') as f:
try:
lastRow = deque(csv.reader(f), 1)[0]
except IndexError: # empty file
lastRow = None
return lastRow
""" Tweet the PRT Status
Formats the tweet in a user friendly way and tweets.
(note: Only tweets first sentence of data[message].)
"""
def tweetStatus(data):
Twitter = Twython(
consumer_key,
consumer_secret,
access_token_key,
access_token_secret)
# Choose an emoji based on the 'status'
if data['status'] == '1': # Normal
e = '🙌'
elif data['status'] == '2': # Down between 2 stations
e = '💩'
elif data['status'] == '3': # Down at all stations
e = '🔥'
elif data['status'] == '4': # ??
e = '👻'
elif data['status'] == '5': # Down at 1 station
e = '💩'
elif data['status'] == '6': # Closed on Sunday
e = '😴'
elif data['status'] == '7': # Closed
e = '😴'
elif data['status'] == '8': # Down at 3 stations
e = '💩'
mess = data['message'] # Message raw
message_s = unicodedata.normalize('NFKD', mess).encode('ascii','ignore') # Message string
message_f = "%s%s (%s). #wvuprt #wvu #prt" %(message_s.split(".", 1)[0], e, time.ctime(int(data['timestamp']))) # Message formatted
#Twitter.update_status(status=message_f)
print(message_f)
# ********************************* START *********************************
oldTimestamp = ""
timestamp = int(time.time())
url = "http://prtstatus.wvu.edu/api/%s/?format=json" %timestamp
# Get PRT JSON Data
response = urllib.urlopen(url)
data = json.load(response)
try:
oldTimestamp = _get_last_row('monitor.csv')[2]
except IOError:
print "monitor.csv is empty."
if oldTimestamp!=data['timestamp']:
toCSV(data)
print "New data detected for %s...\nUploading..." %time.ctime(int(data['timestamp']))
print json.dumps(data, indent=4, sort_keys=True)
tweetStatus(data)
else:
print "Status has not changed since: %s" %time.ctime(int(oldTimestamp))
# ********************************** END **********************************