botの一部の機能として天気予報(雨など)をDMで告知
python -c "import sqlite3; db=sqlite3.connect('rain.db'); db.text_factory=str; db.execute('create table tweet(locate, now, tomorrow)'); db.execute('''insert into tweet values ('ichikawa', 'ダミー', 'ダミー')'''); db.commit(); db.close()"
何故かワンライナーで天気予報の内容を保存するdbファイルを作成。定期稼働させつつ前回と予報内容が同じ場合は送信の必要が無さそうなので記録が必要に。。。。
データベースのファイル名がrain.db、テーブル名がtweet。そして場所、nowの天気、tomorrowの天気を記録することを想定しています。
yohou.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import pywapi
from urllib import urlencode
#oatはウチのOAuthトークン収納の独自のものです。。。
import oat
import sqlite3
result = pywapi.get_weather_from_yahoo('JAXX0011','metric')
twit = u"市川の現在から今夜くらいの天候は" + result['forecasts'][0]['text'] + u" 気温は最高" + result['forecasts'][0]['high'] + u"で最低が" + result['forecasts'][0]['low'] + u"くらい。" + " #fkdr "
alert = ['Rain', 'Storm', 'Thunder', 'Thunderstorm', 'Snow']
word = result['forecasts'][0]['text']
weth = twit.encode("utf-8")
con = sqlite3.connect("rain.db")
con.text_factory=str;
log = con.cursor()
#今回は今日の天気を設定しています。帰り時間の雨など告知のために。
log.execute(u"select now from tweet")
for row in log:
pass
#前回と違う天気でかつ警戒対象なRainなどか判定
if row[0] != weth and word in alert:
dm = "D スクリーンネーム? " + weth
oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status':dm}))
else:
pass
#sql文の箇所には変数名を入力で内容を保存というのが出来ないように
#なっているようです。
con.execute(u"update tweet set now=?", (weth,))
con.commit()
log.close()
con.close()
Author And Source
この問題について(botの一部の機能として天気予報(雨など)をDMで告知), 我々は、より多くの情報をここで見つけました https://qiita.com/clarinet758/items/eb608622fa62bd70bc96著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .