Skip to content

Instantly share code, notes, and snippets.

@thai-dn
Created April 12, 2017 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thai-dn/e9cd21419a3291eb002c88405d417f04 to your computer and use it in GitHub Desktop.
Save thai-dn/e9cd21419a3291eb002c88405d417f04 to your computer and use it in GitHub Desktop.
Thong ke thoi gian dung facebook
#caculator time use facebook
#demo
import sys # Python Standard Library System Module
import subprocess
import time
line='\n\n------------------------------------------------------------\n'
class Spinner:
# Constructor
def __init__(self):
self.symbols1 ='#'
def Spin(self):
sys.stdout.write("%s" % self.symbols1)
def getdate():
t=time.ctime().split()
date='\nToday:'+'['+t[0]+'-'+t[1]+'-'+t[2]+']\n'
return date
def write_file(value):
f=open('time_use_fb.txt','a')
f.write(value)
f.close()
def time_caculator_value(time_start,time_end,timedelay=0):
time_start=time_start.split(':')
time_end=time_end.split(':')
h_value=int(time_end[0])-int(time_start[0])
m_value=int(time_end[1])-int(time_start[1])
s_value=int(time_end[2])-int(time_start[2])
totaltime=h_value*3600+m_value*60+s_value+timedelay
if totaltime>0:
time_list.append(totaltime)
return time_caculator(totaltime)
return 0
def time_caculator(time):
result=''
h=time/3600
time%=3600
m=time/60
s=time%60
if h >0:
result+=str(h)+':'
if m>0:
result+=str(m)
else:
result+='00'
if s>0:
result+=':'+str(s)
else:
result+=':00'
#print result
return result
def ping(host):
command='ping -n 1 -a '+host
result=subprocess.call(command, shell=True)
return result
def find_fb_address():
p = subprocess.Popen("netstat -an|findstr 443 | FINDSTR ESTABLISH", stdout=subprocess.PIPE, shell=True)
result=p.communicate()[0].strip().replace('\r','').split('\n')
if len(result)>1:
for i in result:
r=i.split()
ip=r[2].split(':')[0].strip()
command='ping -n 1 -a '+ip
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
result=p.communicate()[0]
if 'facebook' in result:
return ip
return False
def check_online(ip):
command='ping -n 1 -a '+ip
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
result=p.communicate()[0]
if 'facebook' in result:
return True
return False
########################################################################
write_file(line)
def check_new_date():
if getdate() not in get_current_day:
return 1
return 0
def check_write_date():
ff=open('time_use_fb.txt')
f=ff.read()
ff.close()
if getdate().strip() in f:
return True
return False
check_onl=False # check if online, use for count time online
#list_time=[] # get verbose online time
time_list=[] # count online time value
starttime=time.time()
cc=0
off_status = 'Offline'
on_status = 'Online'
timedelay=-30 # time delay (s)
first_check_online=True # online time show
first_check_offline=True # offline time show
ss=Spinner() # show status
get_current_day=[]
get_current_day.append(getdate())
print getdate() # show date
banner="""
########################################################################
# #
# SCRIPT THONG KE THOI GIAN SU DUNG FACEBOOK TRONG 1 NGAY #
# --v1.11.2016 #
# --by thai Hands0m3 #
# #
########################################################################
"""
print banner
########################################################################
c=0
while(1):
if c%60==0:
ss.Spin()
#-------------------------------------------------------check new day
#get_time=time.ctime().split()[3].split(':')
#if (get_time[0]=='00' and get_time[1] =='01' and int(get_time[2])<25) or :
if check_new_date():
get_current_day.append(getdate()) # add new day
sumtime=0
for i in time_list:
sumtime+=i
sumtime=str(time_caculator(sumtime))
show='\n\n\t\t------------------------Total time:'+sumtime
print '\n',show
write_file(show)
write_file(line)
print 'Value',time_list
time_list=[]
print '\n-------------------------------------------------------------------'
print getdate() # show date
#-------------------------------------------------------------------
try:
ip_fb=find_fb_address()
if ip_fb:
ip=ip_fb
check_onl=True # check if online, value is True
current_time_=time.ctime().split()[3] # get online time
range_t=time.time()-starttime
if first_check_online:
time_online=time.time() # get online time value
start_time_online=time.ctime().split()[3] # get online time
print '\n[',current_time_,']',on_status, ip_fb
first_check_online=False
#write content to file
else:
time_now=time.ctime().split()[3] # get now time
if check_onl:
if not check_write_date():
write_file('\n-------------------------------------------------------------------')
write_file(getdate())
count_time_online=time_caculator_value(start_time_online,time_now,timedelay)
if count_time_online>0:
status='\n'+on_status+'|\t'+start_time_online+' - '+time_now+'|\ttime use: '+str(count_time_online)+' | Delay '+str(timedelay)+'s | '+ip
write_file(status)
#list_time.append([on_status,start_time_online,time_now,count_time_online])
print status
check_onl=False # check if online, use for count time online
first_check_offline=True #reset show offline
else:
if first_check_offline:
print '\n[',time_now,']',off_status
first_check_offline=False
first_check_online=True #reset get online time value
time.sleep(20)
except (KeyboardInterrupt, SystemExit):
#print list_time
sumtime=0
for i in time_list:
sumtime+=i
sumtime=str(time_caculator(sumtime))
show='\n\n\t\t------------------------Total time:'+sumtime
print '\n',show
write_file(show)
break
c+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment