Skip to content

Instantly share code, notes, and snippets.

@yosida95
Created July 21, 2013 16:05
Show Gist options
  • Save yosida95/6048959 to your computer and use it in GitHub Desktop.
Save yosida95/6048959 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
import re
import time
import email
from getpass import getpass
from threading import (
Thread,
Event
)
import imaplib2
from gntp import notifier as growl
class IMAPIdle(object):
def __init__(self, connection, on_message):
self.connection = connection
self.on_message = on_message
self.thread = Thread(target=self.idle)
self.event = Event()
def start(self):
self.thread.start()
def stop(self):
self.event.set()
def join(self):
self.thread.join()
def idle(self):
is_continue = True
while is_continue:
self.needsync = False
def callback(args):
if not self.event.is_set():
self.needsync = True
self.event.set()
self.connection.idle(callback=callback)
self.event.wait()
if self.needsync is True:
self.event.clear()
self.dosync()
# self.stop が呼び出された
if self.event.is_set():
is_continue = False
def seen(self, message_number):
typ, data = self.connection.store(message_number, '+FLAGS', '\\Seen')
if typ == u'OK':
return True
raise Exception
def get_unseen_messages(self):
typ, data = self.connection.search(None, 'UNSEEN')
if typ == u'OK':
return data[0].split()
raise Exception
def get_message(self, message_number):
typ, data = self.connection.fetch(message_number, 'RFC822')
if typ == u'OK':
return email.message_from_string(data[0][1])
raise Exception
def dosync(self):
for message_number in self.get_unseen_messages():
try:
self.on_message(self.get_message(message_number))
self.seen(message_number)
except Exception:
raise
class CombConfAttendeeNotificator(object):
def __init__(self, host, account, box=u'INBOX'):
self.connection = imaplib2.IMAP4_SSL(host)
self.connection.login(account, getpass())
self.connection.select(u'INBOX')
self.imap = IMAPIdle(self.connection, self.on_message)
self.growl = growl.GrowlNotifier(
applicationName=u'CombConf',
notifications=[u'新規参加者'],
defaultNotifications=[u'新規参加者']
)
self.growl.register()
def start(self):
self.imap.start()
while True:
try:
time.sleep(60)
except Exception:
continue
except:
self.stop()
break
def stop(self):
self.imap.stop()
try:
self.connection.close()
except BaseException:
pass
finally:
self.connection.logout()
print u'Bye!'
def on_message(self, message):
subject = email.Header.decode_header(message.get(u'Subject', u''))[0]
subject = subject[0].decode(subject[1] or u'ISO-2022-JP')
matches = re.match(ur'(.+?)さんがCombConf( 懇親会)?に参加登録しました。',
subject)
if matches is None:
return
if matches.group(2) is None:
self.notify(matches.group(1), False)
else:
self.notify(matches.group(1), True)
def notify(self, attendee_name, is_reunion=False):
self.growl.notify(
noteType=u'新規参加者',
title=u'CombConf%s新規参加者' % (u'懇親会' if is_reunion else u''),
description=u'%sさんがCombConf%sに参加しました!\nありがとうございます!' % (
attendee_name, (u'懇親会' if is_reunion else u'')
),
icon='http://img.yosida95.com/combconf.png',
sticky=True,
priority=2
)
def main():
notifier = CombConfAttendeeNotificator(IMAP_SERVER, LOGIN_NAME)
notifier.start()
if __name__ == u'__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment