5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

botで天気予報をtweet

Last updated at Posted at 2013-09-05

新しくその2の記事を起こしました。
http://qiita.com/clarinet758/items/1e084ce44c04aade1c2e

最新情報:2014年10月ころから私の環境では動かなくなり以下の記事の内容は現在は動作することを保証しません。

八王子、仙台、熊本、京都が選ばれているのはtwitter上の友人に合わせてです。JAXX修正してyahoo!weatherから取得できれば何処の地域でもアレです。
http://99blues.dyndns.org/blog/2011/01/lets_get_weather_6/
上記サイト様は天気予報を取得するコードを頂く以外にも大変参考にさせて頂きました。
鎮守府とかのはただなんとなく娘たちが雨の中戦っていたりとか大丈夫か心配だったので。
新設サーバーの地域の追加に追いついておりません。
トラック泊地もアレでいいのか調べきっておりません。
同秒でtweetぶん投げすぎると何かのペナルティを恐れて、予報の際には4秒おきです。天気予報ですから。
ただこの方法だと最後に投稿した後も4秒待ってから終了という、、、

yohou.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-

import pywapi
from urllib import urlencode
from oauth2 import Client, Consumer, Token
from prettyprint import pp, pp_str
import oat
import datetime
import time
con = datetime.datetime.now()


class Today():
    form = u"%sの現在から今夜くらいの天候は%sで気温は最高%s度くらいで最低が%s度くらい"

    @classmethod
    def noww(self):
        yaho = pywapi.get_weather_from_yahoo
        hachi, sendai = yaho('JAXX0013', 'metric'), yaho('JAXX0104', 'metric')
        kuma, kyoto = yaho('JAXX0043', 'metric'), yaho('JAXX0047', 'metric')
        ha, sn = hachi['forecasts'][0], sendai['forecasts'][0]
        ku, ky = kuma['forecasts'][0], kyoto['forecasts'][0]
        hac = Today.form % (u"八王子", ha['text'], ha['high'], ha['low'])
        sen = Today.form % (u"仙台", sn['text'], sn['high'], sn['low'])
        kum = Today.form % (u"熊本", ku['text'], ku['high'], ku['low'])
        kyo = Today.form % (u"京都", ky['text'], ky['high'], ky['low'])
        hachi, sendai = hac.encode("utf-8"), sen.encode("utf-8")
        kumamoto, kyouto = kum.encode("utf-8"), kyo.encode("utf-8")
        for x in sendai, hachi, kumamoto, kyouto:
            oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': x}))
            time.sleep(4)

    @classmethod
    def tinj(self):
        yaho = pywapi.get_weather_from_yahoo
        yoko, kure, sase, maid = yaho('JAXX0099', 'metric'), yaho('JAXX0045', 'metric'), yaho('JAXX0079', 'metric'), yaho('JAXX0047', 'metric')
        oomin, chuuku = yaho('JAXX0004', 'metric'), yaho('JAXX0045', 'metric')
        yo, ku = yoko['forecasts'][0], kure['forecasts'][0]
        sa, ma = sase['forecasts'][0], maid['forecasts'][0]
        oo, ch = oomin['forecasts'][0], chuuku['forecasts'][0]
        yok = Today.form % (u"横須賀鎮守府", yo['text'], yo['high'], yo['low'])
        kur = Today.form % (u"呉鎮守府", ku['text'], ku['high'], ku['low'])
        sas = Today.form % (u"佐世保鎮守府", sa['text'], sa['high'], sa['low'])
        mai = Today.form % (u"舞鶴鎮守府", ma['text'], ma['high'], ma['low'])
        oom = Today.form % (u"大湊警備府", oo['text'], oo['high'], oo['low'])
        chu = Today.form % (u"トラック泊地", ch['text'], ch['high'], ch['low'])
        yokosuka, kure = yok.encode("utf-8"), kur.encode("utf-8")
        sasebo, maiduru = sas.encode("utf-8"), mai.encode("utf-8")
        oominato, chuuk = oom.encode("utf-8"), chu.encode("utf-8")
        for x in yokosuka, kure, sasebo, maiduru, oominato, chuuk:
            oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': x}))
            time.sleep(4)


if con.minute == 46:
    Today.noww()
elif con.minute == 10:
    Today.tinj()
else:
    pass

無理にclass作成したのですがこういう使い方ではないような。。

5
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?