khalfella

ashraf_script

Feb 12th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. #!/usr/bin/python
  2. import re,os,urllib,urllib2,sys,csv,time
  3.  
  4.  
  5. def dedup():
  6. import os,time
  7. today_var=time.strftime("%Y%m%d")
  8. for file in os.listdir('/home/cdrlookup/fraud_calls/cleanlog/'):
  9. file_str=''
  10. file_str+=str(file)
  11. os.system('sort -r --field-separator=, --key=2,3 --unique < /home/cdrlookup/fraud_calls/cleanlog/'+file_str+' --output=/home/cdrlookup/fraud_calls/cleanlog/'+file_str+'_dedup')
  12. with open('/home/cdrlookup/fraud_calls/cleanlog/'+file_str+'_dedup') as f:
  13. next(f)
  14. for line in (f):
  15. line =line.strip()
  16. data=line.split(",")
  17. try:
  18. #report_time = data[0]
  19. caller = data[1]
  20. called = data[2]
  21. date = data[3]
  22. time = data[4]
  23. line=(caller +',' + called + ',' + date +','+ time)
  24. print line
  25. except IndexError:
  26. continue
  27. #line=(caller +',' + called + ',' + date +','+ time)
  28. #print line
  29.  
  30. def updtcommas():
  31. today_var=time.strftime("%Y%m%d")
  32. working_dic = '/home/cdrlookup/fraud_calls/processed_dir/'
  33. for file in os.listdir('/home/cdrlookup/fraud_calls/callslog/'):
  34. file_str='/home/cdrlookup/fraud_calls/callslog/'
  35. file_str+=str(file)
  36. #print file_str
  37. with open ('/home/cdrlookup/fraud_calls/processed_dir/'+file+'_processed',"w") as processed_file:
  38. #processed_file = open('/home/cdrlookup/fraud_calls/processed_dir/'+file_str+'_processed_file','w')
  39. with open ('/home/cdrlookup/fraud_calls/callslog/'+file) as f:
  40. #with open(file_str) as f:
  41. #print processed_file#file_str
  42. while True:
  43. c = f.read(1)
  44. if not c:
  45. # print ("End of file")
  46. break
  47. # print ("Read a character:", c)
  48.  
  49.  
  50. if c=='"':
  51. processed_file.write(c)
  52. c = f.read(1)
  53. while c != '"':
  54. if c== ',':
  55. c= ';'
  56. processed_file.write(c)
  57. c = f.read(1)
  58.  
  59.  
  60. processed_file.write(c)
  61. continue
  62. #processed_file.close()
  63. def getSPID(input):
  64. url = 'http://pears.web.att.com/PEARS/services/getPortableData'
  65. values = {'method' : 'searchByMDN','phonenumbers' : input}
  66. try:
  67.  
  68. data = urllib.urlencode(values)
  69. req = urllib2.Request(url, data)
  70. response = urllib2.urlopen(req)
  71. salida=response.read().replace('\r\n','').replace('\t','')
  72. lrn=salida.split("'LRNs','width=400px,height=200px,resize=1,center=1'); return false")[1].split("</a>")[0][2:]
  73. spid=salida.split('<td align="middle">')
  74. area=spid[-1].split('"display:none">')[1].split('</td>')[0]
  75. spid=[m for m in spid if m.find(' </td> ')!=-1][0].split('</td>')[0].replace(' ',' ').replace(' ',' ').replace(' ',' ')
  76. m=salida.split('<td align="middle">')
  77. geocode=[p for p in m if p.find('</td> ')>-1 and len(p.split('</td>')[0])==3][1][0:3]
  78. return spid
  79. except IndexError:
  80. pass
  81.  
  82. def main():
  83. data = []
  84. new_line=0
  85. header =("report_time,caller,called,date,time,ampm,timezone,call_type,goal,ctanumber,url,comments,captcha,client_ip,remote_client_ip,Carrier")
  86. start_regex=re.compile(r'[^"]?\d{2}/\d{2}/\d{2}[?"]?')
  87. regex=re.compile(r'^(\d{10})(?:\s|$)')
  88. regex1=re.compile(r'^(\d)\1{5,}')
  89. # calling updatecommas method to update every comma in files...
  90. #file_updt=updtcommas()
  91. #today_var=time.strftime("%Y%m%d")
  92. #for file in os.listdir('/home/cdrlookup/fraud_calls/processed_dir/'):
  93. for file in os.listdir('/home/cdrlookup/fraud_calls/callslog/'):
  94. file_str=''
  95. file_str+=str(file)
  96. #ROBO = open('/home/cdrlookup/fraud_calls/processed_dir/'+file_str)
  97. ROBO = open('/home/cdrlookup/fraud_calls/callslog/'+file_str)
  98. #ROBO = open('/home/cdrlookup/fraud_calls/calls_'+today_var+'.log')
  99. clean_file=open('/home/cdrlookup/fraud_calls/cleanlog/'+file_str+'_clean','w')
  100. bad_file=open('/home/cdrlookup/fraud_calls/badlog/'+file_str+'_bad','w')
  101. next(ROBO)
  102. clean_file.write(header+ "\n")
  103. for line in (ROBO):
  104. line =line.strip()
  105. data=line.split(",")
  106. try:
  107. report_time = data[0]
  108. caller = data[1]
  109. called = data[2]
  110. date = data[3]
  111. time = data[4]
  112. ampm = data[5]
  113. timezone = data[6]
  114. call_type = data[7]
  115. goal = data[8]
  116. ctanumber = data[9]
  117. url = data[10]
  118. comments = data[11]
  119. captcha = data[12]
  120. client_ip = data[13]
  121. remote_client_ip =data[14]
  122. except IndexError:
  123. continue
  124. tn_caller=re.sub("[^0-9]","",caller)
  125. tn_caller=tn_caller.lstrip("+")
  126. tn_caller=tn_caller.lstrip("0")
  127. tn_caller=tn_caller.lstrip("1")
  128. tn_called=re.sub("[^0-9]","",called)
  129. tn_called=tn_called.lstrip("+")
  130. tn_called=tn_called.lstrip("0")
  131. tn_called=tn_called.lstrip("1")
  132. mo1 =(regex.search(tn_caller))
  133. mo2=(regex.search(tn_called))
  134. mo3=(regex1.search(tn_caller))
  135. mo4=(regex1.search(tn_called))
  136. new_line =(report_time+','+caller+','+called+','+date+','+time+','+ampm+','+timezone+','+call_type+','+goal+','+ctanumber+','+url+','+comments+','+captcha+','+client_ip+','+remote_client_ip)
  137. # Check if the line started correctely with report_time
  138. if not start_regex.search(report_time):
  139. bad_file.write((new_line) + "\n")
  140. # check if it's 10 digits for the caller and called numbers
  141. elif not(mo1 and mo2):
  142. bad_file.write((new_line) + "\n")
  143. #and not more than 5 times repeated for both caller and called numbers
  144. elif (mo3 and mo4):
  145. bad_file.write((new_line) + "\n")
  146. else:
  147. carr=getSPID(tn_caller)
  148. if carr is None:
  149. clean_file.write((new_line) + ",\"Unknown Carrier\"\n")
  150. else:
  151. clean_file.write((new_line) + ",\"" + carr + "\"\n")
  152. continue
  153. bad_file.close()
  154. ROBO.close()
  155. clean_file.close()
  156. updtcommas()
  157. main()
  158. dedup()
Add Comment
Please, Sign In to add comment