teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2017/03/14 08:09

投稿

techno
techno

スコア22

title CHANGED
File without changes
body CHANGED
@@ -93,4 +93,121 @@
93
93
  Raspberry Pi3 Model Bです
94
94
  Open Jtalkはインストール済みでコマンドを入力すればその言葉を喋ってくれるところまでは出来ました。
95
95
 
96
- こういう質問サイトで質問するのは初めてなのでまだまだ言葉足らずやそのほか至らぬ点が多々あると思います。藁をもつかむ思いです、何かアドバイス等頂けらたと思います。よろしくお願いいたします。
96
+ こういう質問サイトで質問するのは初めてなのでまだまだ言葉足らずやそのほか至らぬ点が多々あると思います。藁をもつかむ思いです、何かアドバイス等頂けらたと思います。よろしくお願いいたします。
97
+
98
+
99
+
100
+
101
+ ***追記
102
+
103
+ can110 さんにご指摘頂きjsayのプログラムを確認した所「#!/bin/sh」が記述されていませんでした。早速記述し実行した所次のようなエラーが出ました。
104
+
105
+ ↓エラー
106
+ ```python
107
+
108
+ jsay 3月14日、17時0分6秒
109
+ Traceback (most recent call last):
110
+ File "tenki2.py", line 52, in <module>
111
+ main()
112
+ File "tenki2.py", line 11, in main
113
+ say_weather()
114
+ File "tenki2.py", line 35, in say_weather
115
+ today_t_txt = temperature_text % (cast['dateLabel'], temperature['max']['celsius'], temperature['min']['celsius'])
116
+ TypeError: 'NoneType' object has no attribute '__getitem__'
117
+
118
+ ```
119
+ です。
120
+
121
+ 更にcan110 さんに助言して頂きプログラムを追加致しました
122
+ ↓追加
123
+
124
+ ```python
125
+
126
+ #!/usr/bin/env python
127
+ # -*- coding:utf-8 -*-
128
+
129
+ import shlex
130
+ import subprocess
131
+ from datetime import datetime
132
+ import urllib2
133
+ import json
134
+
135
+ CMD_SAY = 'jsay'
136
+ def main():
137
+ say_datetime()
138
+ say_weather()
139
+ return
140
+ def say_datetime():
141
+ d = datetime.now()
142
+ text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second)
143
+ text = CMD_SAY + ' ' + text
144
+ print text
145
+ proc = subprocess.Popen(shlex.split(text))
146
+ proc.communicate()
147
+ return
148
+ def say_weather():
149
+ city = '130010'; # Tokyo 他の地域の方は、番号を変えてください。
150
+ json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' #API URL
151
+ weather_text = u'%sの天気は%sです。'
152
+ temperature_text = u'%sの予想最高気温、%s度、予想最低気温、%s度です。'
153
+ try:
154
+ jsonStr = unicode(r.read())
155
+ print(jsonStr) # 確認表示
156
+ obj = json.loads( jsonStr )
157
+ print(obj) # 確認表示
158
+ title = obj['title']
159
+ forecasts = obj['forecasts']
160
+ # TODAY
161
+ cast = forecasts[0]
162
+ temperature = cast['temperature']
163
+ today_w_txt = weather_text % (cast['dateLabel'], cast['telop'])
164
+ # 最高、最低気温を取得。データが存在しない場合もある
165
+ temp_max = ""
166
+ temp_min = ""
167
+ if temperature['max'] is not None:
168
+ temp_max = temperature['max']['celsius']
169
+ if temperature['min'] is not None:
170
+ temp_max = temperature['min']['celsius']
171
+
172
+ today_t_txt = temperature_text % (cast['dateLabel'], temp_max, temp_min)
173
+ # TOMMOROW
174
+ cast = forecasts[1]
175
+ temperature = cast['temperature']
176
+ tommorow_w_txt = weather_text % (cast['dateLabel'], cast['telop'])
177
+ # SAY
178
+ weather_str = title + ' ' + today_w_txt + ' ' + today_t_txt + ' ' + tommorow_w_txt
179
+ weather_str = weather_str.encode('utf-8')
180
+ text = '''%s '%s' ''' % (CMD_SAY, weather_str)
181
+ print text
182
+ proc = subprocess.Popen(shlex.split(text,))
183
+ proc.communicate()
184
+ finally:
185
+ r.close()
186
+ return
187
+ ### Execute
188
+ if __name__ == "__main__":
189
+ main()
190
+
191
+ ```
192
+
193
+
194
+ と記述
195
+ 結果
196
+
197
+
198
+
199
+ ```python
200
+
201
+ jsay 3月14日、17時4分36秒
202
+ Traceback (most recent call last):
203
+ File "tenki.py", line 64, in <module>
204
+ main()
205
+ File "tenki.py", line 13, in main
206
+ say_weather()
207
+ File "tenki.py", line 60, in say_weather
208
+ r.close()
209
+ NameError: global name 'r' is not defined
210
+
211
+ ```
212
+
213
+ となりました。