#!/usr/bin/python

# twitter_public_timeline.py
#       --copyright--                   Copyright 2009 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       April 1, 2009           bar
#       June 5, 2009            bar     able to be imported without any json lib (for tz_lib_test.py purposes - get_json_to_file() will except ImportError if json can't be imported)
#       July 26, 2009           bar     double the poll time if there's a failure
#                                       use url_getter for timeout
#       February 26, 2011       bar     make work with json module, itself (read() routine)
#       March 9, 2011           bar     experiment with gc.collect()
#       August 24, 2011         bar     be more graceful about not getting any json
#       November 12, 2011       bar     take the s out of the url
#       November 29, 2011       bar     pyflake cleanup
#       --eodstamps--
##      \file
#
#
#       Get twitter public timeline json every 50 seconds.
#
#

import  gc
import  httplib
import  os
import  socket
import  sys
import  time
import  urllib2

try :
    import  json                            # python 2.6 has this?
    if  not hasattr(json, 'read') :
        json.read   = json.loads
    pass
except ImportError :
    try :
        import  jsonlib                     # well, otherwise, I have jsonlib installed
        json    = jsonlib
    except ImportError :
        try     :
            import simplejson as json
            json.read   = json.loads
            json.write  = json.dumps
        except  :
            json        = None
    pass


import  tzlib
import  replace_file
import  url_getter


WAIT_TIME       = 50

URL             = "http://twitter.com/statuses/public_timeline.json"




def get_json() :
    url         = URL

    if  False   :
        s           = ""
        try         :
            getter  = urllib2.urlopen(url)              # urllib2 seems to pick up on 404's, which urllib doesn't appear to do
            s       = getter.read()
            getter  = None
        except socket.error :
            pass
        except socket.herror :
            pass
        except socket.gaierror :
            pass
        except         OSError, (errno, strerror) :
            pass
        except         IOError :                        # HTTPError is raised by urllib2 on 404's. But it doesn't come with info - and somehow IOError gets triggered ok
            pass
        except     MemoryError :                        # we've run out of memory during the read or what?
            pass
        except  AttributeError :                        # something is very wrong with the server
            pass
        except httplib.HTTPException :
            pass
        pass
    else    :
        s   = url_getter.url_open_read_with_timeout(url)

    return(s)



def get_json_to_file(outdir = None) :
    outdir      = outdir or "."

    s           = get_json()

    if  s :
        if  not json :
            raise(ImportError, "No json lib imported")

        j       = json.read(s)

        fn      = os.path.join(outdir, "ptl_" + str(j[-1]['id']) + "-" + str(j[0]['id']) + ".json")
        tfn     = fn + ".tmp"
        tzlib.write_whole_binary_file(tfn, s)
        replace_file.replace_file(fn, tfn, fn + ".bak")

    return(s)




help_str    = """
%s (options)
Options:
    --pretty                    Print the json in a pretty format.
    --tweets                    Print the tweets, themselves.

I will get the Twitter public timeline every 50 seconds and put the json results in a file.
"""

if  __name__ == '__main__' :

    import  TZCommandLineAtFile


    program_name    = sys.argv.pop(0)
    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)


    help_str            = help_str % ( os.path.basename(program_name) )


    print_pretty        = False
    print_tweets        = False
    verbose             = False


    while True :
        oi  = tzlib.array_find(sys.argv, [ "--help", "-h", "-?", "/?", "/h", "/H" ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        print help_str % ( os.path.basename(program_name) )

        sys.exit(254)


    while True :
        oi  = tzlib.array_find(sys.argv, [ "--pretty", "-p", ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        print_pretty    = True

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--tweets", "-t", ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        print_tweets    = True

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--verbose", "-v", ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        verbose         = True




    while True :

        outdir  = time.strftime("%Y_%m_%d", time.localtime())
        if  not os.path.isdir(outdir) :
            os.makedirs(outdir)

        s       = get_json_to_file(outdir)
        if  s   :
            j   = json.read(s)

            if  print_pretty :
                print
                print json.write(j, indent = "    ")


            if  print_tweets :
                print
                for tw in j :
                    print tzlib.printable_str(tw['text'])
                pass

            if  verbose :
                print len(j), j[-1]['id'], j[0]['id']

            pass


        gc.collect()
        if  (not s) or (not j) :
            time.sleep(WAIT_TIME * 2)
        else :
            time.sleep(WAIT_TIME)
        pass

    pass

#
#
# eof

