Guest User

Untitled

a guest
Aug 19th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. def get_read_connection():
  2. connection = None
  3. read_host = 'localhost'
  4. try:
  5. if sys.platform != 'darwin':
  6. read_host = 'remote_rds_host.com'
  7.  
  8. read_c = pymysql.connect(
  9. host=read_host,
  10. user=user,
  11. password=password,
  12. db=db_name,
  13. autocommit=False,
  14. cursorclass=pymysql.cursors.DictCursor)
  15. print('Read Connected')
  16. except Exception as ex:
  17. print('Exception in Read Connection')
  18. print(str(ex))
  19. finally:
  20. return read_c
  21.  
  22.  
  23. def get_connection():
  24. connection = None
  25. try:
  26. connection = pymysql.connect(host=host,
  27. user=user,
  28. password=password,
  29. db=db_name,
  30. cursorclass=pymysql.cursors.DictCursor)
  31. print('Connected')
  32. except Exception as ex:
  33. print(str(ex))
  34. finally:
  35. return connection
  36.  
  37. def get_links(size=4):
  38. total_links = []
  39. sql = None
  40.  
  41. try:
  42. if connection is not None:
  43. # connection_read.ping(reconnect=True)
  44. now = datetime.datetime.now()
  45. # print("Current date and time using strftime:")
  46. # print(now.strftime("%Y-%m-%d %H:%M"))
  47. with connection.cursor() as cursor:
  48. # sql = ' SELECT * from zillow_links_master where status = 0 LIMIT ' + str(size)
  49. sql = " select license_id from " + TABLE_NAME + " WHERE status = 0 ORDER BY id LIMIT " + str(size)
  50. print(sql)
  51. cursor.execute(sql)
  52. links = cursor.fetchall()
  53.  
  54. for link in links:
  55. total_links.append(link['license_id'])
  56.  
  57. print(tuple(total_links))
  58. print('======================================')
  59. if len(total_links) > 0:
  60. if connection is not None:
  61. now = datetime.datetime.now()
  62. # print("Current date and time using strftime:")
  63. # print(now.strftime("%Y-%m-%d %H:%M"))
  64. with connection.cursor() as cursor:
  65. format_strings = ','.join(['%s'] * len(total_links))
  66. # sql = " UPDATE " + TABLE_NAME + " set is_processed = 4 WHERE url IN (%s)" % format_strings,
  67. sql = " UPDATE " + TABLE_NAME + " set status = 1 WHERE license_id IN ({})".format(
  68. format_strings)
  69. cursor.execute(sql, tuple(total_links))
  70. connection.commit()
  71. print(cursor.rowcount)
  72. except Exception as ex:
  73. print('Exception in get_links')
  74. crash_date = time.strftime("%Y-%m-%d %H:%m:%S")
  75. crash_string = "".join(traceback.format_exception(etype=type(ex), value=ex, tb=ex.__traceback__))
  76. exception_string = '[' + crash_date + '] - ' + crash_string + 'n'
  77. print(exception_string)
  78. print(sql)
  79. if 'lost' in exception_string:
  80. print('Connection lost')
  81. # connection_read.ping()
  82. finally:
  83. return total_links
  84.  
  85. while True:
  86. print('Block Begin')
  87. now = datetime.datetime.now()
  88. # print("Current date and time using strftime:")
  89. # print(now.strftime("%Y-%m-%d %H:%M"))
  90. p_links = get_links(LIMIT)
  91. for p in p_links:
  92. l_id = p
  93. result = parse(l_id)
  94. store_lic_info(l_id, result)
  95. sleep(2)
  96. connection.commit()
  97.  
  98. print('Block End')
  99. sleep(2)
Add Comment
Please, Sign In to add comment