Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. import pickle
  2. import time
  3. from datetime import datetime, timedelta
  4. import ftplib
  5. import MySQLdb
  6. import sys
  7. from sys import argv
  8. import os
  9. import urllib
  10. import subprocess
  11. from ftplib import FTP
  12. from time import sleep
  13. import requests
  14. from rets import Session
  15. import re
  16.  
  17. db2 = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost
  18. charset='utf8',
  19. user="", # your username
  20. passwd="", # your password
  21. db="") # name of the data base
  22.  
  23. cur2 = db2.cursor()
  24.  
  25. cur2.execute('SET NAMES utf8;')
  26. cur2.execute('SET CHARACTER SET utf8;')
  27. cur2.execute('SET character_set_connection=utf8;')
  28.  
  29. cur2.execute("SELECT MAX(lastupdate) FROM updates where name = 'media'")
  30. max_time_string = cur2.fetchone()[0]
  31. max_time_string = str(max_time_string)
  32. max_datetime = datetime.strptime(max_time_string, "%Y-%m-%d %H:%M:%S")
  33. search_datetime = max_datetime - timedelta(minutes=15)
  34. search_time_string = datetime.strftime(search_datetime, "%Y-%m-%dT%H:%M:%S")
  35.  
  36. login_url = ''
  37. username = ''
  38. password = ''
  39. rets_client = Session(login_url, username, password)
  40. rets_client.login()
  41. system_data = rets_client.get_system_metadata()
  42. system_data
  43. resources = rets_client.get_resource_metadata(resource='Agent')
  44. resources
  45.  
  46. while True:
  47. try:
  48. search_results = rets_client.search(resource='media', resource_class='media', dmql_query='(Modified='+search_time_string+'+)') #change search query here
  49. print "Inserting", len(search_results), "records now"
  50. break
  51. except:
  52. time.sleep(15)
  53.  
  54. chunk_size = 40000
  55. chunks = []
  56. chunk_size = max(1, chunk_size)
  57. for i in range(0, len(search_results), chunk_size):
  58. chunk = search_results[i:i+chunk_size]
  59. chunks.append(chunk)
  60.  
  61. for chunk in chunks:
  62.  
  63. placeholders = ', '.join(['%s'] * len(chunk[0]))
  64. columns = "`%s`" % '`,`'.join(chunk[0].keys())
  65. update_string = ""
  66. for key in chunk[0]:
  67. update_string = update_string + "`" + key + "`=VALUES(`" + key + "`),"
  68. update_string = update_string[:-1]
  69.  
  70. sql = "INSERT IGNORE INTO media ( %s ) VALUES ( %s ) ON DUPLICATE KEY UPDATE %s" % (columns, placeholders, update_string) #change table here
  71.  
  72. chunk_values = []
  73. for result in chunk:
  74. values = result.values()
  75. values_fixed = []
  76. for val in values:
  77. is_datetime = re.match("(\d{4}-[01]\d-[0-3]\d)T([0-2]\d:[0-5]\d:[0-5]\d)", val)
  78. if is_datetime:
  79. values_fixed.append(is_datetime.group(1) + " " + is_datetime.group(2))
  80. else:
  81. values_fixed.append(val)
  82. chunk_values.append(tuple(values_fixed))
  83.  
  84. success = False
  85.  
  86. try:
  87. cur2.executemany(sql, chunk_values)
  88. db2.commit()
  89. print ("Records inserted")
  90. success = True
  91. except:
  92. db2.rollback()
  93.  
  94. if not success:
  95.  
  96. for result in chunk:
  97.  
  98. placeholders = ', '.join(['%s'] * len(result))
  99. columns = "`%s`" % '`,`'.join(result.keys())
  100. update_string = ""
  101. for key in result:
  102. update_string = update_string + "`" + key + "`=VALUES(`" + key + "`),"
  103. update_string = update_string[:-1]
  104.  
  105. sql = "INSERT INTO media ( %s ) VALUES ( %s ) ON DUPLICATE KEY UPDATE %s" % (columns, placeholders, update_string) #change table here
  106.  
  107. values = result.values()
  108. values_fixed = []
  109. for val in values:
  110. is_datetime = re.match("(\d{4}-[01]\d-[0-3]\d)T([0-2]\d:[0-5]\d:[0-5]\d)", val)
  111. if is_datetime:
  112. values_fixed.append(is_datetime.group(1) + " " + is_datetime.group(2))
  113. else:
  114. values_fixed.append(val)
  115.  
  116. print (sql)
  117. cur2.execute(sql, tuple(values_fixed))
  118. print ("Record inserted")
  119. db2.commit()
  120. print "Processed", len(search_results), "results, quitting."
  121. print "Processed", len(search_results), "results, quitting."
  122. cur2.execute("INSERT INTO updates (name, lastupdatecount) VALUES (%s, %s)",("media", len(search_results)))
  123. db2.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement