# Path: jd/models.py
# class JDVisit(models.Model):
#     id = models.AutoField(primary_key = True, name = 'id')
#     jd_url = models.CharField(max_length = 200, name = 'jd_url', verbose_name = '京东商城商品的 url 链接', default = '')
#     ip = models.CharField(max_length = 20, name = 'ip', verbose_name = '访问者的 IP 地址')
#     ip_address = models.CharField(max_length = 200, name = 'ip_address', verbose_name = 'IP 对应的地址', default = None)
#     visit_time = models.DateTimeField(name = 'visit_time', verbose_name = '访问的时间')
#     user_agent = models.TextField(max_length = 1000, name = 'user_agent', verbose_name = '访问者的 HTTP_USER_AGENT',
#                                   default = '')
# 
#     ip_hight_success = models.CharField(max_length = 10, name = 'ip_hight_success', verbose_name = '查询 IP 高精度定位是否成功',
#                                         default = '')
#     ip_hight_address = models.CharField(max_length = 200, name = 'ip_hight_address', verbose_name = 'IP 对应的高精度地址',
#                                         default = '')
#     ip_confidence = models.FloatField(name = 'ip_confidence', verbose_name = 'IP 高精度查询结果的可信度', default = 0)
#     ip_hight_radius = models.IntegerField(name = 'ip_hight_radius', verbose_name = 'IP 高精度查询结果的偏移半径', default = -1)
#     ip_hight_lat = models.FloatField(name = 'ip_hight_lat', verbose_name = 'IP 高精度查询经度', default = -1)
#     ip_hight_long = models.FloatField(name = 'ip_hight_long', verbose_name = 'IP 高精度查询纬度', default = -1)
# 
#     class Meta:
#         db_table = 'jd_visit'


import datetime
import utils
from django.utils.deprecation import MiddlewareMixin
from jd.models import JDVisit
#-*- coding: utf-8 -*-




class JDVisitMiddleware(MiddlewareMixin):
    def process_request(self, request):
        page = request.path
        if 'runspider' in page and request.method == 'POST':
            ip = utils.get_visiter_ip(request)
            user_agent = request.META.get('HTTP_USER_AGENT', '')
            vt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            jd_url = request.POST.get('url')
            visit = JDVisit(id = None, ip = ip, ip_address = '', visit_time = vt, user_agent = user_agent,
                            jd_url = jd_url, ip_hight_success = '', ip_hight_address = '')

            visit.save()
        elif 'randitem' in page and request.method == 'POST':
            ip = utils.get_visiter_ip(request)
            user_agent = request.META.get('HTTP_USER_AGENT', '')
            vt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            jd_url = 'randitem'

# Path: jd/models.py
# class JDVisit(models.Model):
#     id = models.AutoField(primary_key = True, name = 'id')
#     jd_url = models.CharField(max_length = 200, name = 'jd_url', verbose_name = '京东商城商品的 url 链接', default = '')
#     ip = models.CharField(max_length = 20, name = 'ip', verbose_name = '访问者的 IP 地址')
#     ip_address = models.CharField(max_length = 200, name = 'ip_address', verbose_name = 'IP 对应的地址', default = None)
#     visit_time = models.DateTimeField(name = 'visit_time', verbose_name = '访问的时间')
#     user_agent = models.TextField(max_length = 1000, name = 'user_agent', verbose_name = '访问者的 HTTP_USER_AGENT',
#                                   default = '')
# 
#     ip_hight_success = models.CharField(max_length = 10, name = 'ip_hight_success', verbose_name = '查询 IP 高精度定位是否成功',
#                                         default = '')
#     ip_hight_address = models.CharField(max_length = 200, name = 'ip_hight_address', verbose_name = 'IP 对应的高精度地址',
#                                         default = '')
#     ip_confidence = models.FloatField(name = 'ip_confidence', verbose_name = 'IP 高精度查询结果的可信度', default = 0)
#     ip_hight_radius = models.IntegerField(name = 'ip_hight_radius', verbose_name = 'IP 高精度查询结果的偏移半径', default = -1)
#     ip_hight_lat = models.FloatField(name = 'ip_hight_lat', verbose_name = 'IP 高精度查询经度', default = -1)
#     ip_hight_long = models.FloatField(name = 'ip_hight_long', verbose_name = 'IP 高精度查询纬度', default = -1)
# 
#     class Meta:
#         db_table = 'jd_visit'


# Path: jd/middleware.py
import datetime
import utils
from django.utils.deprecation import MiddlewareMixin
from jd.models import JDVisit

#-*- coding: utf-8 -*-




class JDVisitMiddleware(MiddlewareMixin):
    def process_request(self, request):
        page = request.path
        if 'runspider' in page and request.method == 'POST':
            ip = utils.get_visiter_ip(request)
            user_agent = request.META.get('HTTP_USER_AGENT', '')
            vt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            jd_url = request.POST.get('url')
            visit = JDVisit(id = None, ip = ip, ip_address = '', visit_time = vt, user_agent = user_agent,
                            jd_url = jd_url, ip_hight_success = '', ip_hight_address = '')

            visit.save()
        elif 'randitem' in page and request.method == 'POST':
            ip = utils.get_visiter_ip(request)
            user_agent = request.META.get('HTTP_USER_AGENT', '')
            vt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            jd_url = 'randitem'

            visit = JDVisit(id = None, ip = ip, ip_address = '', visit_time = vt, user_agent = user_agent,