Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #modules we use throughout this script
  2. import csv
  3. import mysql.connector
  4. import fileinput
  5. import urllib
  6. import sys
  7.  
  8.  
  9. #connection info for the database
  10. cnx = mysql.connector.connect(host="localhost", user="test", password="test!", database="test")
  11. query = cnx.cursor(prepared=True)
  12.  
  13.  
  14. #Here we download the Robocallers .csv file, the string at the end of the URL are filters passed to the API to only pull down the data we need
  15. download = urllib.URLopener()
  16. download.retrieve("https://opendata.fcc.gov/resource/sr6c-syda.csv?$$app_token=someparamshere", "RoboCaller.csv")
  17. #add test to see if file downloaded or not, if file did not then sys.exit()
  18. #Here build a filterd .csv to prevent any duplicate records as well as skip the first line of the .csv file as it contains field names
  19. map = {}
  20. row_count = 0
  21. with open(r'RoboCaller.csv', 'r') as infile:
  22. reader = csv.reader(infile)
  23. for row in reader:
  24. (caller_id_number, issue, issue_date) = row
  25. row_count = row_count + 1
  26. if row_count == 1:
  27. continue
  28. if map.has_key(caller_id_number):
  29. continue
  30. else:
  31. map[caller_id_number] = (issue, issue_date)
  32.  
  33. #at this point we count our Filtered CSV file to see if its massivly truncated.
  34. #we check against a threshhold to see if the amount of records are less than it.
  35. #if they are we close the program and throw an error if not we continue.
  36. if row_count < 80000:
  37. print('this list is far to truncated, terminating program')
  38. sys.exit()
  39.  
  40. #Here we check the count of the table and store it
  41. query.execute("Select count(*) from Numbers")
  42. oldcount = query.fetchall()
  43. print ("The old count is:" + str(oldcount))
  44.  
  45. #Here we query the db to build the whitelist as well as exclude numbers we own
  46. query.execute("Select whitelist FROM whitelist")
  47. whitelist = [entry for (entry,) in query.fetchall()]
  48. #print("the numbers" + str(whitelist) + "are on the white list and will be skipped")
  49.  
  50. query.execute("Select TN from DID_Numbers")
  51. didnumbers = [entry for (entry,) in query.fetchall()]
  52. #print("the numbers" + str(didnumbers) + "are apart of the numbers we own and will be omitted from the black list")
  53.  
  54. #here were Purge the current value in the mysql DB table, so we dont build duplicates
  55. query.execute("TRUNCATE TABLE Numbers")
  56.  
  57.  
  58. #here we check every Key in the Dict(Caller_Id_Number is our Key) as well as the tuples (v) accociated each key (the tuples are issue & issue_date)
  59. for caller_id_number, v in map.iteritems():
  60. caller_id_number = caller_id_number[:3] + caller_id_number[4:7] + caller_id_number[8:12]
  61. if caller_id_number in whitelist:
  62. print("Phone Number " + str(caller_id_number) + " is in the whitelist and will be ignored")
  63. continue
  64. elif caller_id_number in didnumbers:
  65. print("Phone Number " + str(caller_id_number) + " is a number SimplyBits Owns and will be ignored")
  66. continue
  67. else:
  68. query.execute("INSERT INTO Numbers ( Numbers, issue, date ) VALUES(%s,%s,%s)", (caller_id_number, v[0], v[1][:10])) #sql query to insert numbers into black list
  69.  
  70. #Here we check the new count
  71. query.execute("Select count(*) from Numbers")
  72. newcount = query.fetchall()
  73. print ("The new Count is:" + str(newcount))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement