#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import urllib
import unicodedata
class ScheduleScraping:
def dataScraping(self, u):
obj = urllib.urlopen(u)
tempList = []
for i in obj:
if i.find('
') != -1:
pass
elif i.find('\x83p\x81E\x83\x8a\x81[\x83O') != -1 or i.find('\x83Z\x81E\x83\x8a\x81[\x83O') != -1:
break
elif i.find('') != -1:
tempList.append(i[i.find('')+4:i.find('')])
# 不要なデータのインデックスを除去リストに追加
remList = []
for j in range(len(tempList)):
# 半角換算で2文字以下は不要なので除去
if len(tempList[j]) <= 2:
remList.append(j);
# その他の不要データも除去
elif tempList[j] == '\x8c\x8e\x93\xfa' or tempList[j] == '\x83J\x81[\x83h' or tempList[j] == '\x8b\x85\x8f\xea' or tempList[j] == '\x8e\x9e\x8a\xd4':
remList.append(j);
# フォントタグや「オールスターゲーム」があったら除去
elif tempList[j].find('') != -1 or tempList[j].find('\x83I\x81[\x83\x8b\x83X\x83^\x81[\x83Q\x81[\x83\x80') != -1:
remList.append(j);
# ひっくり返す
remList.reverse();
print("find: " + str(len(tempList)))
print("useless: " + str(len(remList)))
# 除去
for k in range(len(remList)):
del tempList[remList[k]]
# データを加工
# 初期化
date = []
card = 0
location = 0
time = 0
cnt = 0
eventList = []
yobiCheck = 0
for l in range(len(tempList)):
# まずは日付が含まれていないかチェック
if tempList[l].find('\x8c\x8e') != -1:
month = tempList[l][0:tempList[l].find('\x8c\x8e')]
day = tempList[l][tempList[l].find('\x8c\x8e')+2:-2]
# 一桁なら0を付与
if len(month) == 1:
month = "0" + str(month)
elif len(day) == 1:
day = "0" + str(day)
date = [month, day]
# カウントが0のとき(日付を除く最初のデータ)は組み合わせ
elif cnt == 0:
card = tempList[l]
# unicode に変換
card = unicode(card, 'Shift_JIS')
# ノーマライズ(半角に)
card = unicodedata.normalize('NFKC', card)
# utf-8に
card = card.encode('utf-8')
# 予備日
if card.find('(') != -1:
card = card.replace('(', '')
if card.find(')') != -1:
card = card.replace(')', '')
yobiCheck = 1
# ハイフンを挿入
# Bs/YBが含まれてる場合
if len(card) <= 3:
# 一旦置換
card = card.replace('Bs', '1')
card = card.replace('YB', '2')
# ハイフン追加
card = card[0] + '-' + card[1]
# 再置換
card = card.replace('1', 'Bs')
card = card.replace('2', 'YB')
# Bsが含まれていない場合
else:
card = card[0] + '-' + card[1]
# 予備日の場合は注釈
if yobiCheck == 1:
card = card + '(予備日)'
yobiCheck = 0
cnt += 1
# カウントが1のときは場所
elif cnt == 1:
location = tempList[l]
# utf-8に
location = unicode(location, 'Shift_JIS').encode('utf-8')
cnt += 1
# カウントが2のときは時間
elif cnt == 2:
time = tempList[l]
time = time.replace(':', '')
# 標準時+24時間表記に
time = int(time) + 300
# 終了時間も(+三時間)
endTime = int(time) + 300
# 3桁の場合先頭に0
if len(str(time)) < 4:
time = "0" + str(time)
if len(str(endTime)) < 4:
endTime = "0" + str(endTime)
# 全部のデータが揃ったらまとめてリストに追加
if date and card and location and time:
# 年月日と時間のフォーマットを変更
timeLine = "2010" + date[0] + date[1] + "T" + time + "00Z"
endTimeLine = "2010" + date[0] + date[1] + "T" + str(endTime) + "00Z"
eventList.append([timeLine, card, location, endTimeLine])
#日付以外はリセット
card = 0
location = 0
time = 0
# カウントリセット
cnt = 0
return eventList
def __init__(self):
fileName = raw_input("File Name: ")
n = str(fileName) + ".ics"
print("create a file...\n\n")
# ヘッダ書き込み
f = open(n, "w")
f.write("BEGIN:VCALENDAR\n")
f.write("VERSION:2.0\n")
f.write("PRODID: \n")
f.close
print("file created.\n")
while(1):
u = raw_input("Source (\"end\" to finish): ")
if u == "end":
# フッタ書き込み
f = open(n, "a")
f.write("END:VCALENDAR")
f.close
break
else:
list = self.dataScraping(u)
#イベント書き込み
f = open(n, "a")
for i in range(len(list)):
f.write("BEGIN:VEVENT\n")
f.write("DTSTART:" + list[i][0] + "\n")
f.write("DTEND:" + list[i][3] + "\n")
f.write("LOCATION:" + list[i][2] + "\n")
f.write("SUMMARY:" + list[i][1] + "\n")
f.write("END:VEVENT\n")
f.close
print("finished.")
print("\nComplete!")
app = ScheduleScraping()
app