-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchartstep.py
More file actions
105 lines (75 loc) · 2.23 KB
/
chartstep.py
File metadata and controls
105 lines (75 loc) · 2.23 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
#imports
from __future__ import division
import InstrumentControl
import urllib2
import json
import time
import signal
import sys
#ENTER API KEY HERE#
APIkey = ""
#initiate Arduino connection
Stepper=InstrumentControl.StepperMotor('/dev/tty.usbmodem1411') #change this to your Arduino address
#speed settings
fast = 200
slow = 9000
### PROGRAM LOOP ###
while 1:
Stepper.SetDelay(fast)
Stepper.SetPosition(1,1,0)
print("")
print("What domain?")
print("(Input 'cbtotal' for Chartbeat stats)")
print("")
domain = raw_input()
if domain == 'cbtotal':
domain = 'status.chartbeat.net'
print("")
print("Got it.... " + domain)
print("")
print("Running....")
print("Press Control-C to Stop")
print("")
#getSteps for domain, print Angle#
def getSteps(domain):
timestamp = int(time.time())
if domain == 'status.chartbeat.net':
#cbtotal API
APIquickstat = "http://chartbeat.com/api/cbtotal"
json1 = urllib2.urlopen(APIquickstat).read()
quickstat = json.loads(json1)
current = quickstat['total']
else:
# Chartbeat API URLs
APIquickstat = "http://api.chartbeat.com/live/quickstats/v3/?apikey=" + APIkey + "&host=" + domain
json1 = urllib2.urlopen(APIquickstat).read()
quickstat = json.loads(json1)
current = quickstat['people']
APIhistorical = "http://api.chartbeat.com/historical/traffic/stats/?apikey=" + APIkey + "&host=" + domain + "&end=" + str(timestamp) + "&properties=max,min"
# Reading JSON data
json2 = urllib2.urlopen(APIhistorical).read()
historical = json.loads(json2)
max = historical['data'][domain]['people']['max']
min = historical['data'][domain]['people']['min']
#angle formula
angle = ((current / max) * 120)
#print "30 day max is: " + str(max)
#print "30 day min is: " + str(min)
#print "currently: " + str(current)
steps = (angle * 533.333333) // 120
print str(angle) + " degrees"
#print steps
return steps
### API LIVE LOOP ###
try:
goSteps = getSteps(domain)+4800
Stepper.SetDelay(fast)
Stepper.SetPosition(1,1,4800)
Stepper.SetDelay(slow)
Stepper.SetPosition(1,1,goSteps)
while 1:
time.sleep(5)
Stepper.SetPosition(1,1,getSteps(domain) + 4800)
except: KeyboardInterrupt
print("Quiting...")
print("")