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

import tweepy #https://github.com/tweepy/tweepy
import json
import sys
import twitter
import klout
from datetime import datetime
from difflib import SequenceMatcher

created_at_format = '%a %b %d %H:%M:%S +0000 %Y'

#Twitter API credentials
consumer_key = "ZdCIeH1wekGEA6YdLcKw9WiHs"
consumer_secret = "OrmIPhqC9MeDZ5J9lcSBNTXvranazCsGsVKPrWD6sAKup3pHsH"
access_key = "238085527-vpA5OdS3WJyp6psQCS5Rj5FSfOtoDMKVdUPrMoD7"
access_secret = "9G735d1HzdTK72gdWnEGGGfTbEZNw9EBuEWQsd4d1M7Q0"

#-----------------------------------------------------------------------
# create twitter API object
#-----------------------------------------------------------------------
twitter = Twitter(auth = OAuth(access_key, access_secret, consumer_key, consumer_secret))

#-----------------------------------------------------------------------
# this is the user we're going to query.
#-----------------------------------------------------------------------
class Result():
		
		screen_name = username
	#-----------------------------------------------------------------------
	# query the user timeline.
	# twitter API docs:
	# https://dev.twitter.com/rest/reference/get/statuses/user_timeline
	#-----------------------------------------------------------------------
		results = twitter.statuses.user_timeline(screen_name = screen_name, count=50, since_id=11, include_rts= False, exclude_replies= True)
	#-----------------------------------------------------------------------
	# loop through each status item, and print its content.
	#-----------------------------------------------------------------------
		for status in results:
			print ("(%s) %s" % (status["created_at"], status["text"].encode("ascii", "ignore")))

	#---------------------------- TWEET DELAY ------------------------------
	#-----------------------------------------------------------------------
	# 1st and 2nd latest tweet
	#-----------------------------------------------------------------------
		first_timestamp = datetime.strptime(results[0]["created_at"], created_at_format)
		second_timestamp = datetime.strptime(results[1]["created_at"], created_at_format)
		diff_dt1 = (first_timestamp - second_timestamp).total_seconds()/60

	#-----------------------------------------------------------------------
	# 2nd and 3rd latest tweet
	#-----------------------------------------------------------------------
		second_timestamp = datetime.strptime(results[1]["created_at"], created_at_format)
		third_timestamp = datetime.strptime(results[2]["created_at"], created_at_format)
		diff_dt2 = (second_timestamp - third_timestamp).total_seconds()/60

	#-----------------------------------------------------------------------
	# 3rd and 4th latest tweet
	#-----------------------------------------------------------------------
		third_timestamp = datetime.strptime(results[2]["created_at"], created_at_format)
		forth_timestamp = datetime.strptime(results[3]["created_at"], created_at_format)
		diff_dt3 = (third_timestamp - forth_timestamp).total_seconds()/60

	#-----------------------------------------------------------------------
	# print the differences
	#-----------------------------------------------------------------------

		total_dt = diff_dt1+diff_dt2+diff_dt3
		percent1 = (diff_dt1/total_dt)*100
		percent2 = (diff_dt2/total_dt)*100
		percent3 = (diff_dt3/total_dt)*100

		mean_dt = total_dt/3
		total_percent =  percent1+percent2+percent3
		delay_percent =  total_percent/3

	#------------------------ DUPLICATE TWEET ------------------------------
	#-----------------------------------------------------------------------
	# 1st and 2nd latest tweet
	#-----------------------------------------------------------------------
		tweet1 = (results[0]["text"])
		tweet2 = (results[1]["text"])
		s = SequenceMatcher (None, tweet1, tweet2)
		d1 = s.ratio()*100

	#-----------------------------------------------------------------------
	# 2nd and 3rd latest tweet
	#-----------------------------------------------------------------------
		tweet2 = (results[1]["text"])
		tweet3 = (results[2]["text"])
		s = SequenceMatcher (None, tweet2, tweet3)
		d2 = s.ratio()*100

	#-----------------------------------------------------------------------
	# 3rd and 4th latest tweet
	#-----------------------------------------------------------------------
		tweet3 = (results[2]["text"])
		tweet4 = (results[3]["text"])
		s = SequenceMatcher (None, tweet3, tweet4)
		d3 = s.ratio()*100

		

	#---------------------------- KLOUT SCORE ------------------------------
		from klout import *

		# Make the Klout object
		k = Klout('8mn8zzyn7jssra56pphkdwff')

		# Get kloutId of the user by inputting a twitter screenName
		screenName = screen_name
		kloutId = k.identity.klout(screenName=screenName).get('id')

		# By default all communication is not secure (HTTP). An optional secure parameter
		# can be sepcified for secure (HTTPS) communication
		k = Klout('8mn8zzyn7jssra56pphkdwff', secure=True)

		# Optionally a timeout parameter (seconds) can also be sent with all calls
		score = k.user.score(kloutId=kloutId, timeout=5).get('score')

		#--------------------- GET_SCORE ------------------------#

		# Get klout score of the user
		score = k.user.score(kloutId=kloutId).get('score')

		#-----------------------------------------------------------------------
		# RESULT
		#-----------------------------------------------------------------------

		print("") 
		if diff_dt1 == diff_dt2:
				print ("Inter-tweet delay = Fix (%.1f minutes)"% (mean_dt))
				
		if diff_dt2 == diff_dt3:
				print ("Inter-tweet delay = Fix (%.1f minutes)"% (mean_dt))
				
		else: print ("Inter-tweet delay = Random (%.1f minutes)"% (mean_dt))

		print("Tweet delay   = %.1f%%" %(delay_percent))
		print("Duplicate tweet   = %.1f%%" %(average))
		print ("Klout score = %.1f%% "%(score))

		botlike=(score+average+delay_percent)/300*100
		print("Bot-like percentage = %.1f%% "%(botlike))