forked from shbhrsaha/printers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.py
More file actions
95 lines (61 loc) · 2.21 KB
/
Copy pathbackground.py
File metadata and controls
95 lines (61 loc) · 2.21 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
# THIS SCRIPT RUNS IN THE BACKGROUND
# AND COLLECTS DATA IN MONGODB
import datetime
import urllib, datetime, time
from bs4 import BeautifulSoup
import pymongo
from pymongo import MongoClient
# ESTABLISHED DATABASE CONNECTION
connection = MongoClient()
printerDb = connection.printer
recordsCollec = printerDb.records
# Define MONGODB entry writing
def addEntry(entryDateTime, line):
# create the object
new_entry = {
'datetime' : entryDateTime,
'location' : line[0],
'building' : line[1],
'room' : line[2],
'status' : line[3],
}
recordsCollec.insert(new_entry)
url = "http://clusters-lamp.princeton.edu/cgi-bin/clusterinfo.pl"
while True:
print "Pausing for 10 minutes"
time.sleep(600)
# Get current DateTime
now = datetime.datetime.now()
output = ""
print "Fetching at " + str(now)
# Fetch HTML
f = urllib.urlopen(url)
html = f.read()
# Get BeautifulSoup Object
soup = BeautifulSoup(html)
tr_list = soup.find_all('tr')
length = len(tr_list)
for n in range(1, length):
tr = tr_list[n]
line = list()
for td in tr.find_all('td'):
line.append(td.get_text().strip())
if (line[0] != "1937**" and line[0] != "Forbes**"):
#output += str(now) + ',' + ','.join(line) + '\n'
addEntry(now, line)
else:
# need to split Printers A & B into two cases
divided = line[-1].split("Printer")
originalTitle = line[0]
# do Printer A
line[0] = originalTitle + "A"
line[-1] = divided[1].strip()[3:]
#output += str(now) + ',' + ','.join(line) + '\n'
addEntry(now, line)
# do Printer B
line[0] = originalTitle + "B"
line[-1] = divided[2].strip()[3:]
#output += str(now) + ',' + ','.join(line) + '\n'
addEntry(now, line)
#with open("output.csv", "a") as outputFile:
#outputFile.write(output)