Python Coroutines and Twitter


Reading and thought this was a natural for a twitter client. Here is a pretty simple version that just prints the public timeline every 60 seconds. Next, up removing the time.sleep and scheduling the followStatus function as a task so I can follow more than one stream at a time.

    #!/usr/bin/env python
    # encoding: utf-8
    import time
    import twitter

    def coroutine(func):
        A decorator function that takes care of starting a coroutine
        automatically on call.

        def start(*args,**kwargs):
            cr = func(*args,**kwargs)
            return cr
        return start

    def statusPrinter():
        Just prints twitter status messages to the screen
        while True:
             status = (yield)
             print,, status.text

    def followStatus(twitterGetter, target, timeout = 60):
        Follows a twitter status message that takes a since_id
        since_id = None
        while True:
            statuses = twitterGetter(since_id=since_id)
            if statuses:
                # pretty sure these are always in order
                since_id = statuses[0]
                for status in statuses:
            # twitter caches for 60 seconds anyway

    def main():
        api = twitter.Api()
        followStatus(api.GetPublicTimeline, statusPrinter())

    if __name__ == '__main__':