# -*- coding:utf-8 -*- """ # PyStnPollen # SYMBIAN_UID = 0x # web = "http://masaland.cocolog-nifty.com/" # This program is free software.This program is distributed WITHOUT ANY WARRANTY.You should use it on your own risk. You can modify/redistribute it under the terms of the GNU General Public License version 2 or later. """ __scriptname__ = 'PyStnPollen' __version__ = '0.03.1' __author__ = 'masa (http://masaland.cocolog-nifty.com/)' __copyright__ = '(c) masa 2014-17' #-------iniファイルで設定可能な変数---- VIEW_QUERY = 1 #PyStnPollen.html をみるか否かのクエリーを出す LOCID = '14/14211/' #神奈川(秦野) VIEW_ALT_TEXT = 1 #花粉情報画像のALTテキストを表示させるか否か #-------web URLを設定する変数----------- NEWS_URL_BASE = 'http://kafun.yahoo.co.jp/weather/' #-------global変数の確保----------------- #内部処理で設定するので、設定不要 internal_image_file = 0 #---------------------------------------- Debug_in = 0 #* Debug_out = 0 #* # Debug output file # E:/Data/Pollentest0.htm import sys sys.setdefaultencoding('utf-8') sys.path.insert(0, 'E:\\Python\\lib') import e32 import os import os.path import socket import urllib import appuifw import re import time try: import ConfigParser except: appuifw.note(unicode('ConfigParserモジュールが見つかりません。'), 'error') raise try: import appswitch ISAPPSWITCH = 1 def switch_to_fg(): appswitch.switch_to_fg(u'PyStnPollen') def switch_to_bg(): appswitch.switch_to_bg(u'PyStnPollen') except ImportError: ISAPPSWITCH = 0 try: import waplib ISWAPLIB = 1 except ImportError: ISWAPLIB = 0 #try: # import japanese #except ImportError: # appuifw.note(unicode('JapaneseCodecsが見つかりません。'), 'error', 1) # raise class ConfigError(Exception): """設定エラー""" def __init__(self): pass def __str__(self): return repr(u'Configuration is incomplete.') # データフォルダ DATA_PATH = u'E:\\Data\\Hiisi\\PyStation' if not os.path.exists(DATA_PATH): DATA_PATH = u'C:\\Data\\Hiisi\\PyStation' if not os.path.isdir(DATA_PATH): # データ保存に利用するドライブを選択 drives = [] if u'C:' in e32.drive_list(): drives.append(u'C:\\Data\\Hiisi\\PyStation') if u'E:' in e32.drive_list(): drives.append(u'E:\\Data\\Hiisi\\PyStation') drive = appuifw.popup_menu(drives, unicode('設定データ保存場所')) if drive == 0: DATA_PATH = u'C:\\Data\\Hiisi\\PyStation' elif drive == 1: DATA_PATH = u'E:\\Data\\Hiisi\\PyStation' else: appuifw.note(unicode('設定ファイルを作成できません。'), 'error') raise ConfigError try: os.makedirs(DATA_PATH) except OSError: appuifw.note(unicode('設定ファイルを作成できません。'), 'error') raise # 設定ファイル CFG_FILE = os.path.join(DATA_PATH, u'PyStation.cfg') if os.path.isfile(CFG_FILE): cfg = ConfigParser.ConfigParser() cfg.read(CFG_FILE) write_f = '' try: APID = int(cfg.get('PyStation', 'APID')) except (ConfigParser.NoSectionError): APID = socket.select_access_point() if APID: cfg.add_section('PyStation') cfg.set('PyStation', 'APID', APID) write_f = 1 else: raise ConfigError except (ConfigParser.NoOptionError): APID = socket.select_access_point() if APID: cfg.set('PyStation', 'APID', APID) write_f = 1 else: raise ConfigError except: raise for n in range(3): try: if n==0: VIEW_QUERY = int(cfg.get('PyStation Pollen', 'VIEW_QUERY')) elif n==1: LOCID = cfg.get('PyStation Pollen', 'LOCID') elif n==2: VIEW_ALT_TEXT = int(cfg.get('PyStation Pollen', 'VIEW_ALT_TEXT')) except (ConfigParser.NoSectionError): cfg.add_section('PyStation Pollen') cfg.set('PyStation Pollen', 'VIEW_QUERY', VIEW_QUERY) cfg.set('PyStation Pollen', 'LOCID', LOCID) cfg.set('PyStation Pollen', 'VIEW_ALT_TEXT', VIEW_ALT_TEXT) write_f = 1 break except (ConfigParser.NoOptionError): continue except: raise if write_f: f = open(CFG_FILE, 'w') cfg.write(f) f.close() else: try: APID = socket.select_access_point() if APID: cfg = ConfigParser.ConfigParser() cfg.add_section('PyStation') cfg.set('PyStation', 'APID', APID) cfg.add_section('PyStation Pollen') cfg.set('PyStation Pollen', 'VIEW_QUERY', VIEW_QUERY) cfg.set('PyStation Pollen', 'LOCID', LOCID) cfg.set('PyStation Pollen', 'VIEW_ALT_TEXT', VIEW_ALT_TEXT) f = open(CFG_FILE, 'w') cfg.write(f) f.close() else: raise ConfigError except: raise # バックグラウンドに移動 if ISAPPSWITCH: switch_to_bg() # 出力HTMLファイル OUT_FILE = os.path.join(DATA_PATH, u'%s.html' % __scriptname__) #if os.path.exists(OUT_FILE): if 1: try: f = open(OUT_FILE, 'w') f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('PyStation Pollen\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('

花粉予報

\r\n')) f.write(unicode('

接続エラー!もう一度接続してください。

\r\n')) f.write(unicode('

通信環境、設定、接続先サイトの仕様変更などについてチェックして下さい。

\r\n')) f.write(unicode('
\r\n')) f.write(unicode('

戻る

\r\n')) f.write(unicode('\r\n')) f.write(unicode('')) f.close() except IOError: pass NEWS_URL = "%s%s" % (NEWS_URL_BASE, LOCID) image_path = os.path.join(DATA_PATH, u'pollen') if os.path.exists(image_path): internal_image_file = 1 def fetch(): """ニュースを取得する""" if ISWAPLIB and APID == waplib.APID_WAP: waplib.wap_connection({'Accept-Charset': 'Shift_JIS'}) ap = socket.access_point(APID) socket.set_default_access_point(ap) try: if not Debug_in: f = urllib.urlopen(NEWS_URL) datawk = f.read() f.close() data = datawk.decode('UTF-8') else: F = open('E:\\Data\\Pollentest0.htm') datawk = F.read() F.close() data = datawk.decode('UTF-8') if Debug_out and not Debug_in: F = open('E:\\Data\\Pollentest0.htm', 'w') F.write(datawk) F.close() return data except: # raise return None def parse(data): """予報を取り出す""" datawk = re.sub(u'\t', u'', data) """切り出しの位置を探して、花粉情報テーブルを切り出す""" pattern1 = re.compile(unicode('

.*?の花粉情報

'), re.I) pattern2 = re.compile(unicode('
'), re.I) pattern3 = re.compile(unicode('週間予報'), re.I) pattern4 = re.compile(unicode('.*?のその他地域'), re.I) find_pos1 = pattern1.search(datawk, 0) find_pos2 = pattern2.search(datawk, 0) find_pos3 = pattern3.search(datawk, 0) find_pos4 = pattern4.search(datawk, 0) newsdata = unicode('花粉情報データがありません') #初期値 if find_pos1 and find_pos2 and find_pos3 and find_pos4: r_1 = find_pos1.start() r_2 = find_pos2.start() r_3 = find_pos3.start() r_4 = find_pos4.start() newsdata = (datawk[r_1 : r_2] + datawk[r_3 : r_4]) newsdata = re.sub(u'https://', u'http://' , newsdata) newsdata = re.sub(u'', u'' , newsdata) newsdata = re.sub(u'', u'' , newsdata) newsdata = re.sub(u'', u'' , newsdata) newsdata = re.sub(u'', u'' , newsdata) newsdata = re.sub(u'', u'' , newsdata) pattern1 = unicode(' ') newsdata = re.sub(u'', pattern1 , newsdata) newsdata = re.sub(u'', u' ' , newsdata) newsdata = re.sub(u'', u'' , newsdata) newsdata = re.sub(u'', u'' , newsdata) newsdata = re.sub(u'
', u'
' , newsdata) #日付文字を大きく newsdata = re.sub(u'
', u'' , newsdata) newsdata = re.sub(u'', \ u'' , newsdata) #花粉情報文字大 newsdata = re.sub(u'', u'' , newsdata) #花粉情報文字大 # if os.path.exists(OUT_FILE): if 1: try: f = open(OUT_FILE, 'w') f.write(unicode('\r\n')) f.write(unicode('')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('PyStn Pollen\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write(unicode('\r\n')) f.write('%s\r\n' % newsdata) f.write(unicode('\r\n')) f.write(unicode('
\r\n')) f.write(unicode('

')) f.write(unicode('戻る
\r\n')) f.write(unicode('' % NEWS_URL)) f.write(unicode('Yahoo 花粉情報')) f.write(unicode('\r\n')) f.write(unicode('')) f.close() except IOError: pass except: f.close() raise return newsdata def check_sig(): """電波状況の確認""" import sysinfo try: sig = sysinfo.signal_bars() except: return 0 else: if sig: return 1 else: return 0 def get_time(): """現在時刻の取得""" year = int(time.strftime('%Y', time.localtime())) month = int(time.strftime('%m', time.localtime())) day = int(time.strftime('%d', time.localtime())) hour = int(time.strftime('%H', time.localtime())) minute = int(time.strftime('%M', time.localtime())) sec = int(time.strftime('%S', time.localtime())) return (year, month, day, hour, minute, sec) def tomorrow(today): """翌日の日付""" year = int(today[:4]) month = int(today[4:6]) day = int(today[6:]) if day == 31 and month == 12: year += 1 month = 1 day = 1 elif day == 31 and month in [1, 3, 5, 7, 8, 10]: month += 1 day = 1 elif day == 30 and month in [4, 6, 9, 11]: month += 1 day = 1 elif day == 29 and month == 2: month += 1 day = 1 elif day == 28 and month == 2 and not (year % 4) and (year % 100) or \ not (year % 400): day += 1 elif day == 28 and month == 2: month += 1 day = 1 else: day += 1 return '%4d%02d%02d' % (year, month, day) def view_data(): if VIEW_QUERY: # フォアグラウンドに移動 if ISAPPSWITCH: switch_to_fg() import globalui q = globalui.global_query(unicode('PyStnPollen \n花粉情報\n確認しますか?')) if q: try: e32.start_exe('Browser.exe', ' "4 file://E:/Data/Hiisi/PyStation/PyStnPollen.html"') except SymbianError: e32.start_exe('BrowserNG.exe', ' "4 file://E:/Data/Hiisi/PyStation/PyStnPollen.html"') def main(): """メインの関数""" old_title = appuifw.app.title appuifw.app.title = unicode(__scriptname__) if check_sig(): try: data = fetch() if data: news = parse(data) if news: view_data() except: raise appuifw.app.title = old_title if __name__ == '__main__': main() # appuifw.app.set_exit() # sys.exit()