visitor.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import config
  2. import pymysql
  3. from db import mysql
  4. from flask import request, session
  5. def track_visitor():
  6. if not config.is_tracking_allowed():
  7. return
  8. else:
  9. ip_address = request.remote_addr
  10. requested_url = request.url
  11. referer_page = request.referrer
  12. page_name = request.path
  13. query_string = request.query_string
  14. user_agent = request.user_agent.string
  15. if config.track_session():
  16. log_id = session['log_id'] if 'log_id' in session else 0
  17. no_of_visits = session['no_of_visits']
  18. current_page = request.url
  19. previous_page = session['current_page'] if 'current_page' in session else ''
  20. if previous_page != current_page:
  21. log_visitor(ip_address, requested_url, referer_page, page_name, query_string, user_agent, no_of_visits)
  22. else:
  23. conn = None
  24. cursor = None
  25. session.modified = True
  26. try:
  27. conn = mysql.connect()
  28. cursor = conn.cursor()
  29. log_id = log_visitor(ip_address, requested_url, referer_page, page_name, query_string, user_agent)
  30. #print('log_id', log_id)
  31. if log_id > 0:
  32. sql = 'select max(no_of_visits) as next from visits_log limit 1'
  33. conn = mysql.connect()
  34. cursor = conn.cursor(pymysql.cursors.DictCursor)
  35. cursor.execute(sql)
  36. row = cursor.fetchone()
  37. count = 0
  38. if row['next']:
  39. count += 1
  40. else:
  41. count = 1
  42. sql = 'UPDATE visits_log set no_of_visits = %s WHERE log_id = %s'
  43. data = (count, log_id,)
  44. cursor.execute(sql, data)
  45. conn.commit()
  46. session['track_session'] = True
  47. session['no_of_visits'] = count
  48. session['current_page'] = requested_url
  49. else:
  50. session['track_session'] = False
  51. except Exception as e:
  52. print(e)
  53. session['track_session'] = False
  54. finally:
  55. cursor.close()
  56. conn.close()
  57. def log_visitor(ip_address, requested_url, referer_page, page_name, query_string, user_agent, no_of_visits=None):
  58. sql = None
  59. data = None
  60. conn = None
  61. cursor = None
  62. log_id = 0
  63. if no_of_visits == None:
  64. sql = "INSERT INTO visits_log(no_of_visits, ip_address, requested_url, referer_page, page_name, query_string, user_agent) VALUES(%s, %s, %s, %s, %s, %s, %s)"
  65. data = (no_of_visits, ip_address, requested_url, referer_page, page_name, query_string, user_agent,)
  66. else:
  67. sql = "INSERT INTO visits_log(ip_address, requested_url, referer_page, page_name, query_string, user_agent) VALUES(%s, %s, %s, %s, %s, %s)"
  68. data = (ip_address, requested_url, referer_page, page_name, query_string, user_agent,)
  69. try:
  70. conn = mysql.connect()
  71. cursor = conn.cursor()
  72. cursor.execute(sql, data)
  73. conn.commit()
  74. log_id = cursor.lastrowid
  75. return log_id
  76. except Exception as e:
  77. print(e)
  78. finally:
  79. cursor.close()
  80. conn.close()