添付ファイル 'wikirss.py'

ダウンロード

   1 #!/usr/bin/env python2.7
   2 
   3 import xml.etree.ElementTree as ET
   4 from datetime import datetime, tzinfo, timedelta
   5 import time
   6 import cookielib
   7 import urllib
   8 import urllib2
   9 import sys
  10 import os
  11 
  12 COOKIE_FILE = os.path.expanduser('~/.is2013wiki-cookie')
  13 ACCESS_TIME_FILE = os.path.expanduser('~/.is2013wiki-accesstime')
  14 INTERVAL_SEC = 300
  15 
  16 class LocalTime(tzinfo):
  17 	def utcoffset(self, dt):
  18 		return timedelta(hours=9)
  19 
  20 	def tzname(self, dt):
  21 		return "JST"
  22 
  23 	def dst(self, dt):
  24 		return timedelta(0)
  25 
  26 class UTC(tzinfo):
  27 	def utcoffset(self, dt):
  28 		return timedelta(0)
  29 
  30 	def tzname(self, dt):
  31 		return "UTC"
  32 
  33 	def dst(self, dt):
  34 		return timedelta(0)
  35 
  36 def extract_history(xml_str):
  37 	xml = ET.fromstring(xml_str)
  38 
  39 	history = {}
  40 	for item in xml.findall('./{http://purl.org/rss/1.0/}item'):
  41 		title = item.find('./{http://purl.org/rss/1.0/}title').text
  42 		raw_date = item.find('./{http://purl.org/dc/elements/1.1/}date').text
  43 		date = datetime.strptime(raw_date, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo = UTC()).astimezone(LocalTime())
  44 		history[date] = title
  45 
  46 	return history
  47 
  48 def print_history_from(history, from_time):
  49 	for date in sorted([date for date in history.keys() if date > from_time], reverse = True):
  50 		print('%(title)s -- %(date)s' % {
  51 			'date': date.ctime(),
  52 			'title': history[date]})
  53 
  54 def create_cookie(name, password):
  55 	cookie = cookielib.LWPCookieJar()
  56 	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
  57 	payload = {'action':'login', 'name':name, 'password':password, 'login':'login'}
  58 	payload_encoded = urllib.urlencode(payload)
  59 	response = opener.open('http://is2013.grafi.jp/', payload_encoded)
  60 	if response.read().find('class="error"') < 0:
  61 		return cookie
  62 	else:
  63 		return None
  64 
  65 def get_rss(cookie):
  66 	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
  67 	response = opener.open('http://is2013.grafi.jp/?action=rss_rc&unique=1')
  68 	return response.read()
  69 
  70 if (len(sys.argv) >= 2 and sys.argv[1] == 'cookie'):
  71 	name = raw_input('type your username: ')
  72 	password = raw_input('type your password: ')
  73 	cookie = create_cookie(name, password)
  74 	if cookie:
  75 		sys.stderr.write("success\n")
  76 		cookie.save(COOKIE_FILE, True)
  77 		os.chmod(COOKIE_FILE, 0600)
  78 		sys.exit()
  79 	else:
  80 		sys.stderr.write("Acquiring cookie failed. Are username and password correct?\n")
  81 		sys.exit(1)
  82 
  83 last_time = datetime.fromtimestamp(0, LocalTime())
  84 if os.path.exists(ACCESS_TIME_FILE):
  85 	with open(ACCESS_TIME_FILE, 'r') as f:
  86 		last_time = datetime.fromtimestamp(float(f.read()), LocalTime())
  87 
  88 current_time = datetime.now(LocalTime())
  89 with open(ACCESS_TIME_FILE, 'w') as f:
  90 	f.write(str(time.mktime(current_time.timetuple())))
  91 
  92 if timedelta(seconds = INTERVAL_SEC) > current_time - last_time:
  93 	sys.exit()
  94 
  95 cookie = cookielib.LWPCookieJar()
  96 cookie.load(COOKIE_FILE, True)
  97 rss = get_rss(cookie)
  98 history = extract_history(rss)
  99 
 100 print_history_from(history, last_time)

添付ファイル

添付ファイルを参照するには、(下のファイル一覧にあるように)attachment:filenameと記述します。 [get]リンクのURLは変更される可能性が高いので、利用しないでください。
 All files | Selected Files: delete move to page copy to page

ファイルを添付する権限がありません。