Gmailの新着メールを通知するApricot用のスクリプト

Gmailに新着メッセージを通知するApricot用のスクリプトを書いてみました。
おまけで、メールの内容にキャラクターが反応するようになっています。

ちなみに、エントリの内容を見てキャラクターを反応させるには、下記のような感じで出来ます。

isActivated = False
for entry in entryList:
	wordList = List[String]()
	for word in Script.Words:
		if Regex.IsMatch(entry.Title, Regex.Escape(word.Name), RegexOptions.IgnoreCase) and wordList.Contains(word.Name) == False:
			wordList.Add(word.Name)
			if wordList.Count > 0 and isActivated == False:
				isActivated = Script.TryEnqueue(Script.Prepare(entry, wordList, None, True))

よろしければ、どうぞー


下記のコードを拡張子をpy(例:Gmail.py)にしてScriptsフォルダに保存すると、ApricotにGmailの新着通知を追加できます。
usernameにユーザ名、passowrdにパスワードを設定してください。

# -*- encoding: utf-8 -*-
# Gmail.py
# Copyright © Masaaki Kawata All rights reserved.

import clr
clr.AddReferenceByPartialName("System.Xml")
clr.AddReferenceByPartialName("Apricot")

from System import String, Uri, DateTime, TimeSpan
from System.IO import Stream, StreamReader
from System.Collections.Generic import List
from System.Globalization import CultureInfo, DateTimeStyles
from System.Text.RegularExpressions import Regex, RegexOptions, Match
from System.Timers import Timer
from System.Net import NetworkCredential, WebRequest, WebResponse
from System.Net.NetworkInformation import NetworkInterface
from System.Xml import XmlDocument
from Apricot import Entry

username = ""
password = ""

def update():
	try:
		global username, password, dateTime
		
		if not NetworkInterface.GetIsNetworkAvailable():
			return

		request = WebRequest.Create("https://mail.google.com/mail/feed/atom")
		request.Credentials = NetworkCredential(username, password)
		response = request.GetResponse()
		s = response.GetResponseStream()
		doc = XmlDocument()
		doc.Load(s)
		
		entryList = List[Entry]()

		for entryXmlNode in doc.GetElementsByTagName("entry"):
			newEntry = Entry()
			
			for xmlNode in entryXmlNode.ChildNodes:
				if xmlNode.Name.Equals("title"):
					newEntry.Title = xmlNode.InnerText
				elif xmlNode.Name.Equals("issued"):
					newEntry.Created = DateTime.Parse(xmlNode.InnerText)
				elif xmlNode.Name.Equals("modified"):
					newEntry.Modified = DateTime.Parse(xmlNode.InnerText)
				elif xmlNode.Name.Equals("link"):
					for childXmlNode in xmlNode.ChildNodes:
						if childXmlNode.Name.Equals("rel"):
							newEntry.Resource = Uri(childXmlNode.InnerText)
				elif xmlNode.Name.Equals("author"):
					for childXmlNode in xmlNode.ChildNodes:
						if childXmlNode.Name.Equals("name"):
							newEntry.Author = childXmlNode.InnerText
			
			if newEntry.Modified > dateTime:
				newEntry.ImageUri = Uri("http://www.google.co.jp/options/icons/gmail.gif")
				newEntry.BaseUri = Uri("http://www.gmail.com/")
				newEntry.BaseTitle = "Gmail"
				entryList.Add(newEntry)
		
		if entryList.Count > 0:
			Script.Alert(entryList)
			dateTime = DateTime.Now
			
			isActivated = False
			for entry in entryList:
				wordList = List[String]()
				for word in Script.Words:
					if Regex.IsMatch(entry.Title, Regex.Escape(word.Name), RegexOptions.IgnoreCase) and wordList.Contains(word.Name) == False:
						wordList.Add(word.Name)
				if wordList.Count > 0 and isActivated == False:
					isActivated = Script.TryEnqueue(Script.Prepare(entry, wordList, None, True))
		
		if s != None:
			s.Close()
			
		if response != None:
			response.Close()

	except:
		return

def onTimedEvent(timer, e):
	update()
			
	timer.Interval = 1000 * 60 * 3
	timer.AutoReset = True
	timer.Start()

def onStart(s, e):
	global timer
	timer.Interval = 1000 * 60
	timer.AutoReset = False
	timer.Start()

def onStop(s, e):
	global timer
	timer.Stop()

if not (String.IsNullOrEmpty(username) or String.IsNullOrEmpty(password)):
	dateTime = DateTime.Now - TimeSpan(12, 0, 0)
	timer = Timer()
	timer.Elapsed += onTimedEvent
	timer.Enabled = False
	Script.Start += onStart
	Script.Stop += onStop