#!/usr/bin/env python # encoding: utf-8 """ This script will poll a twitter account for status updates or replies and create OmniFocus inbox tasks from their contents. Was originally conceived to be used with vlingo iPhone app and a "private" twitter account. Will strip the @user leader of replies, and will flag inbox items that contain the word "important" or "flag" (removing them from the task). Designed to be run every 2-5 minutes via launchd (lingon recommended) Requires: http://appscript.sourceforge.net/py-appscript/ http://mike.verdone.ca/twitter/ see also: http://www.vlingo.com/ http://sourceforge.net/projects/lingon/ todo: move fetch replies pref into plist? Created by Preston Holmes on 2009-03-08. Copyright (c) 2009 Preston Holmes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import sys import os from twitter.api import Twitter, TwitterError from plistlib import readPlist,writePlist from datetime import datetime from appscript import * import re #------------- Configuration user = 'username' pw = 'password' #fetching replies doubles the api calls fetch_replies = True #---------------------------- since_id_file = os.path.expanduser('~/Library/Preferences/com.ptone.oftwitter.plist') def main(): t = Twitter(user,pw) lastid = 0 if os.path.exists(since_id_file): lastid = long(readPlist(since_id_file)['lastid']) statuses = [] #twitter search is OK with since_id=0 but not rest api if lastid: msgs = t.statuses.user_timeline(since_id = lastid) else: msgs = t.statuses.user_timeline(count = 10) replies = None if fetch_replies: if lastid: replies = t.statuses.replies(since_id = lastid) else: replies = t.statuses.replies(count = 10) if msgs: statuses.extend(msgs) lastid = msgs[0]['id'] if replies: if replies[0]['id'] > lastid: lastid = replies[0]['id'] statuses.extend(replies) if statuses: of = app('omnifocus') flag_pat = re.compile('important|flag',re.I) #reverse so most recent item is on bottom of inbox statuses.reverse() for item in statuses: #strip the leading @user address of a reply/msg text = item['text'].replace('@%s ' % user,'') #sample created_at': 'Sun Mar 08 18:42:24 +0000 2009' d = datetime.strptime(item['created_at'],'%a %b %d %H:%M:%S +0000 %Y') if flag_pat.search(text): flag = True text = flag_pat.sub('',text) else: flag = False task = of.documents[0].make( new=k.inbox_task, with_properties={ k.name:text, k.creation_date:d, k.flagged:flag, }) # plist lib does not handle long ints!! writePlist({'lastid':str(lastid)},since_id_file) print '%s tasks added' % len(statuses) if __name__ == '__main__': main()