質問編集履歴

1

追記

2017/03/14 08:09

投稿

techno
techno

スコア22

test CHANGED
File without changes
test CHANGED
@@ -189,3 +189,237 @@
189
189
 
190
190
 
191
191
  こういう質問サイトで質問するのは初めてなのでまだまだ言葉足らずやそのほか至らぬ点が多々あると思います。藁をもつかむ思いです、何かアドバイス等頂けらたと思います。よろしくお願いいたします。
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+ ***追記
202
+
203
+
204
+
205
+ can110 さんにご指摘頂きjsayのプログラムを確認した所「#!/bin/sh」が記述されていませんでした。早速記述し実行した所次のようなエラーが出ました。
206
+
207
+
208
+
209
+ ↓エラー
210
+
211
+ ```python
212
+
213
+
214
+
215
+ jsay 3月14日、17時0分6秒
216
+
217
+ Traceback (most recent call last):
218
+
219
+ File "tenki2.py", line 52, in <module>
220
+
221
+ main()
222
+
223
+ File "tenki2.py", line 11, in main
224
+
225
+ say_weather()
226
+
227
+ File "tenki2.py", line 35, in say_weather
228
+
229
+ today_t_txt = temperature_text % (cast['dateLabel'], temperature['max']['celsius'], temperature['min']['celsius'])
230
+
231
+ TypeError: 'NoneType' object has no attribute '__getitem__'
232
+
233
+
234
+
235
+ ```
236
+
237
+ です。
238
+
239
+
240
+
241
+ 更にcan110 さんに助言して頂きプログラムを追加致しました
242
+
243
+ ↓追加
244
+
245
+
246
+
247
+ ```python
248
+
249
+
250
+
251
+ #!/usr/bin/env python
252
+
253
+ # -*- coding:utf-8 -*-
254
+
255
+
256
+
257
+ import shlex
258
+
259
+ import subprocess
260
+
261
+ from datetime import datetime
262
+
263
+ import urllib2
264
+
265
+ import json
266
+
267
+
268
+
269
+ CMD_SAY = 'jsay'
270
+
271
+ def main():
272
+
273
+ say_datetime()
274
+
275
+ say_weather()
276
+
277
+ return
278
+
279
+ def say_datetime():
280
+
281
+ d = datetime.now()
282
+
283
+ text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second)
284
+
285
+ text = CMD_SAY + ' ' + text
286
+
287
+ print text
288
+
289
+ proc = subprocess.Popen(shlex.split(text))
290
+
291
+ proc.communicate()
292
+
293
+ return
294
+
295
+ def say_weather():
296
+
297
+ city = '130010'; # Tokyo 他の地域の方は、番号を変えてください。
298
+
299
+ json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' #API URL
300
+
301
+ weather_text = u'%sの天気は%sです。'
302
+
303
+ temperature_text = u'%sの予想最高気温、%s度、予想最低気温、%s度です。'
304
+
305
+ try:
306
+
307
+ jsonStr = unicode(r.read())
308
+
309
+ print(jsonStr) # 確認表示
310
+
311
+ obj = json.loads( jsonStr )
312
+
313
+ print(obj) # 確認表示
314
+
315
+ title = obj['title']
316
+
317
+ forecasts = obj['forecasts']
318
+
319
+ # TODAY
320
+
321
+ cast = forecasts[0]
322
+
323
+ temperature = cast['temperature']
324
+
325
+ today_w_txt = weather_text % (cast['dateLabel'], cast['telop'])
326
+
327
+ # 最高、最低気温を取得。データが存在しない場合もある
328
+
329
+ temp_max = ""
330
+
331
+ temp_min = ""
332
+
333
+ if temperature['max'] is not None:
334
+
335
+ temp_max = temperature['max']['celsius']
336
+
337
+ if temperature['min'] is not None:
338
+
339
+ temp_max = temperature['min']['celsius']
340
+
341
+
342
+
343
+ today_t_txt = temperature_text % (cast['dateLabel'], temp_max, temp_min)
344
+
345
+ # TOMMOROW
346
+
347
+ cast = forecasts[1]
348
+
349
+ temperature = cast['temperature']
350
+
351
+ tommorow_w_txt = weather_text % (cast['dateLabel'], cast['telop'])
352
+
353
+ # SAY
354
+
355
+ weather_str = title + ' ' + today_w_txt + ' ' + today_t_txt + ' ' + tommorow_w_txt
356
+
357
+ weather_str = weather_str.encode('utf-8')
358
+
359
+ text = '''%s '%s' ''' % (CMD_SAY, weather_str)
360
+
361
+ print text
362
+
363
+ proc = subprocess.Popen(shlex.split(text,))
364
+
365
+ proc.communicate()
366
+
367
+ finally:
368
+
369
+ r.close()
370
+
371
+ return
372
+
373
+ ### Execute
374
+
375
+ if __name__ == "__main__":
376
+
377
+ main()
378
+
379
+
380
+
381
+ ```
382
+
383
+
384
+
385
+
386
+
387
+ と記述
388
+
389
+ 結果
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+ ```python
398
+
399
+
400
+
401
+ jsay 3月14日、17時4分36秒
402
+
403
+ Traceback (most recent call last):
404
+
405
+ File "tenki.py", line 64, in <module>
406
+
407
+ main()
408
+
409
+ File "tenki.py", line 13, in main
410
+
411
+ say_weather()
412
+
413
+ File "tenki.py", line 60, in say_weather
414
+
415
+ r.close()
416
+
417
+ NameError: global name 'r' is not defined
418
+
419
+
420
+
421
+ ```
422
+
423
+
424
+
425
+ となりました。