I've wrote some simple code to load a JSON file from the Twitter Streaming API, parse the data, and create relationships between users based on who mentions or retweets who.  My ultimate goal is to have a file that I can load into a network analysis program to analyze.I wrote the code to first look for a retweet, and if present, create a relationship between the author and the user retweeted.  Then look for any mentions, and if present, create a relationship between the author and any users mentioned.  This part can have multiple relationships for a single tweet, as you can mention multiple people in one tweet.  Then if neither a retweet or a mention is present, print the author and the tweet.  End goal of this is to simple parse a JSON file and return a CSV file that I can then analyze.  I will not be loading this into any type of database.import jsonimport sysimport timedef main():    for line in sys.stdin:        line = line.strip()        data = []        try:            data.append(json.loads(line))        except ValueError as detail:            continue        for tweet in data:            ## deletes any rate limited data            if tweet.has_key('limit'):                pass            else:                tweet_time = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))                ## if the key 'retweeted_status' is in the dict, print                if tweet.has_key('retweeted_status'):                    print \t.join([                    tweet_time,                    tweet['id_str'],                    tweet['user']['screen_name'],                    tweet['retweeted_status']['user']['screen_name'],                    RETWEET,                    tweet['text']                    ]).encode('utf8')                ## if there is a mention in the dict, print                elif 'entities' in tweet and len(tweet['entities']['user_mentions']) > 0:                    for u2 in tweet['entities']['user_mentions']:                        print \t.join([                        tweet_time,                        tweet['id_str'],                        tweet['user']['screen_name'],                        u2['screen_name'],                        MENTION,                        tweet['text']                        ]).encode('utf8')                ## if there is no retweet and no mention, print                else:                    print \t.join([                    tweet_time,                    tweet['id_str'],                    tweet['user']['screen_name'],                    \t,                    TWEET,                    tweet['text']                    ]).encode('utf8')if __name__ == '__main__':    main()
Print Tweets/Mentions/Retweets
python;twitter