/
fireside.py
258 lines (198 loc) · 9.63 KB
/
fireside.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
## Author: Brad Shepard
## Version: Dec 7 2014
## Edited initial commit with more comments
#####Imports and Setup#####
import time
import json #for wundeground api
from time import sleep
import os
import subprocess
import urllib2 #for reading webform settings
from datetime import datetime, timedelta #for the time on the rpi end
from ConfigParser import SafeConfigParser #used to parse config file
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
import sys
import gspread #Google spreadsheet Python API by burnash
#from twython import Twython
#from apscheduler.scheduler import Scheduler #this will let us check the calender on a regular interval; may not need; 3.0 is diff API
state = 0 #state 0 = idle; state 1=play
button = True
currentChannel=1
os.system("sudo mpc clear") #Clear mpc playlist
parser = SafeConfigParser() # initiate Parser and read the configuration file
parser.read('sb.cfg')
#************************************************************************************#
#**** Global variables: wakeup.cfg file ****#
#************************************************************************************#
device = parser.get('user_settings', 'device') #why use a device #? Allows multiple fireside internet players to access a single Google spreadsheet. The device # is the spreadsheet index key.
email = parser.get('google_credentials', 'email')
password = parser.get('google_credentials', 'password')
#weather
wunderground_api_key = parser.get('wunderground','api')
#twitter
consumer_key = parser.get('twitter','CONSUMER_KEY')
consumer_secret = parser.get('twitter','CONSUMER_SECRET')
access_key = parser.get('twitter','ACCESS_KEY')
access_secret = parser.get('twitter','ACCESS_SECRET')
print "Configuration success"
#************************************************************************************#
#**** User variables through Google spreadsheet ****#
#************************************************************************************#
print email
print password
gc = gspread.login(email,password)
print "gspread success"
sheet=gc.open("Fireside") #open spreadsheet named 'Fireside
mainsheet=sheet.worksheet("Stations") #open specific sheet named 'Stations'
cell=mainsheet.find(device) #find the row referencing the player's device #
#load all stations (if you want more stations, add Station7, Station8, etc.)
Station1=mainsheet.cell(cell.row, cell.col+1).value
print Station1
Station2=mainsheet.cell(cell.row, cell.col+2).value
print Station2
Station3=mainsheet.cell(cell.row, cell.col+3).value
Station4=mainsheet.cell(cell.row, cell.col+4).value
Station5=mainsheet.cell(cell.row, cell.col+5).value
Station6=mainsheet.cell(cell.row, cell.col+6).value
#************************************************************************************#
#**** Function for Main Loop
#************************************************************************************#
def MainLoop_func(state,button,currentChannel,channelCount):
buttonCount=0
time_loop_start=time.time()
print "currentChannel = "+ str(currentChannel)
print "channelCount = "+ str(channelCount)
while(True):
time_loop_diff = time.time()-time_loop_start
if (time_loop_diff>10):
time_loop_start=time.time()
Twitter_check_func()
if(button==False):
buttonCount = buttonCount+1
print buttonCount
if buttonCount > 10: #monitors time button is held...if more than 10 count, go to shutdown sequence
print "###########Shutdown################"
Shutdown(state)
if(GPIO.input(16)==True):
buttonCount = 0
if(state==0):
Startup(state)
elif(currentChannel<channelCount):
currentChannel = currentChannel + 1
MPC_func(currentChannel)
else:
currentChannel = 1
MPC_func(currentChannel)
button = GPIO.input(16)
print button
sleep(.1)
#************************************************************************************#
#**** Function for startup (first button press)
#************************************************************************************#
def Startup(state):
Time_func()
Weather_func()
currentChannel = 1
# Twitter_func()
MPC_func(currentChannel)
state = 1
MainLoop_func(state,button,currentChannel,channelCount)
#************************************************************************************#
#**** Function for shutdown (buttonheld)
#************************************************************************************#
def Shutdown(state):
state = 0
os.system("sudo mpc stop")
time_spch="./speech.sh The current time is "+(datetime.now()).strftime("%I:%M")
os.system(time_spch)
shutdown_spch="./speech.sh Turning off radio now"
os.system(shutdown_spch)
sleep(5)
MainLoop_func(state,button,currentChannel,channelCount)
#************************************************************************************#
#**** Function to play MPC
#************************************************************************************#
def MPC_func(currentChannel):
print "MPC Function"
os.system("sudo mpc volume 100")
cmd = subprocess.Popen("sudo mpc play " + str(currentChannel),shell=True,stdout = subprocess.PIPE)
print str(currentChannel) + ": " + cmd.stdout.readline()
#************************************************************************************#
#**** Function to play current TIME
#************************************************************************************#
def Time_func():
print "Telling Time"
time_spch="./speech.sh The current time is "+(datetime.now()).strftime("%I:%M")
os.system(time_spch)
#************************************************************************************#
#**** Function to check for Twitter messages
#************************************************************************************#
def Twitter_check_func():
print "Check for Twitter Messages"
#************************************************************************************#
#**** Function to play Twitter messages
#************************************************************************************#
def Twitter_func():
print "Twitter Messages"
api = Twython(consumer_key,consumer_secret,access_key,access_secret)
# user_timeline = api.get_home_timeline(count=2)
search_results = api.search(q ='eleanorgurganus', count=5)
for tweet in search_results['statuses']:
print 'Tweet from @%s Date: %s' % (tweet['user']['screen_name'].encode('utf-8'),tweet['created_at'])
print tweet['id_str']
tweettext = tweet['text'][1:]
print "text is"+tweettext
twitter_spch="./speech.sh New message "
os.system(twitter_spch)
twitter_spch="./speech.sh %s"%(tweettext)
os.system(twitter_spch)
#************************************************************************************#
#**** Function to play current WEATHER ****#
#************************************************************************************#
def Weather_func():
# Wunderground API : Get weather info #
f = urllib2.urlopen('http://api.wunderground.com/api/'+wunderground_api_key+'/geolookup/conditions/q/NC/Wilmington.json')
json_string = f.read()
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
weather =parsed_json['current_observation']['weather']
temp_f = parsed_json['current_observation']['temp_f']
feelslike_f =parsed_json['current_observation']['feelslike_f']
wind_string =parsed_json['current_observation']['wind_string']
f.close()
print "Weather"
temp_spch="./speech.sh The current temperature in %s is %s Fahrenheit" % (location,temp_f)
os.system(temp_spch)
# temp_spch="./speech.sh The weather today will be %s"%(weather)
# os.system(temp_spch)
# temp_spch="./speech.sh Winds are %s"%(wind_string)
# os.system(temp_spch)
#************************************************************************************#
#**** Function to load URLS ****#
#************************************************************************************#
def LoadURL_func():
os.system("sudo mpc add "+Station1)
os.system("sudo mpc add "+Station2)
os.system("sudo mpc add "+Station3)
os.system("sudo mpc add "+Station4)
os.system("sudo mpc add "+Station5)
os.system("sudo mpc add "+Station6)
cmd = subprocess.Popen("sudo mpc playlist",shell=True, stdout=subprocess.PIPE)
stations=cmd.stdout.readlines()
global channelCount
channelCount=len(stations)
print "initial channelCount= " + str(channelCount)
os.system("sudo mpc volume 0")
index = 1
#i dont know why/if this is necessary: seems to help initialize the mpc playlist...
while (index<channelCount):
os.system("sudo mpc play " + str(index))
index = index + 1
sleep(3)
os.system("sudo mpc stop")
#####Schedule start####
LoadURL_func() #Call function to load URLs
MainLoop_func(state,button,currentChannel,channelCount)